首页 > 技术文章 > ssh简易的大致搭建~

yang-32 2016-07-24 16:05 原文

Ssh项目简易框架搭建

 

1、导入所需的jar

 

 

 

 

2、建立resources文件夹

 

 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- /org/springframework/beans/factory/xml/spring-beans-4.3.xsd -->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--//////////////////////////框架配置  -->

<!-- 配置spring的配置文件 -->

<import resource="context/applicationContext-dataSource.xml"/>

<!-- 配置Hibernate的配置文件 -->

<import resource="context/applicationContext-orm.xml"/>

<!-- 配置事务管理文件 -->

<import resource="context/applicationContext-transaction.xml"/>

 

<!--//////////////////////////业务配置  -->

<!-- 模块的配置文件 -->

</beans>

 

applicationContext-dataSource.xml:

 

<?xml version="1.0" encoding="UTF-8"?>

<!-- /org/springframework/beans/factory/xml/spring-beans-4.3.xsd -->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans.xsd">

 

 

<!-- 装配数据库连接的属性文件内容到spring容器中 -->

<!-- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.class -->

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

 

<property name="location" value="classpath:jdbc.properties"></property>

</bean>

 

 

 

<!-- 配置spring容器的数据源 -->

<bean id="mydataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="${connection.driver_class}"></property>

<property name="url" value="${connection.url}"></property>

<property name="username" value="${connection.username}"></property>

<property name="password" value="${connection.password}"></property>

</bean>

 

 

</beans>

applicationContext-orm.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- /org/springframework/beans/factory/xml/spring-beans-4.3.xsd -->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 将hibernatesessionFactory对象整合到spring容器中 -->

 

<!-- org.springframework.orm.hibernate4.LocalSessionFactoryBean.class -->

<bean id="mySessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

 

    <!-- 注入数据源 -->

<property name="dataSource" ref="mydataSource" />

 

<!-- 把ORM映射文件整合到SessionFactory对象中 -->

 

<!--< property name="mappingLocations">

<list>

<value>com/ssh2/demo/entity/hbm/*.hbm.xml</value>

<value>com/ssh2/demo/entity/hbm/UserInfo.hbm.xml</value>

</list>

</property> -->

<property name="mappingLocations" value="classpath:com/ssh2/demo/entity/hbm/*.hbm.xml"/>

<!--配置hibernate的额外属性  -->

<property name="hibernateProperties">

<props>

<!-- 导入hibernatejar包 -->

<prop key="hibernate.dialect">${hibernate.dialect}</prop>

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>

<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

</props>

 

</property>

</bean>

 

</beans>

applicationContext-transaction.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- /org/springframework/beans/factory/xml/spring-beans-4.3.xsd -->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans.xsd

 http://www.springframework.org/schema/aop

 http://www.springframework.org/schema/aop/spring-aop.xsd

 http://www.springframework.org/schema/tx

 http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置事务管理器 -->

<!-- org.springframework.orm.hibernate4.HibernateTransactionManager.class -->

<bean id="myTransaction" class="org.springframework.orm.hibernate4.HibernateTransactionManager"

<!-- 注入spring容器中的sessionFactory对象 -->

<property name="sessionFactory" ref="mySessionFactory"></property>

</bean>

 

<!-- 配置事务管理器监听方法 -->

<tx:advice id="txadvice" transaction-manager="myTransaction">

<tx:attributes >

<tx:method name="add*" propagation="REQUIRED"/>

<tx:method name="init*" propagation="REQUIRED"/>

<tx:method name="register*" propagation="REQUIRED"/>

<tx:method name="insert*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>

<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>

<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>

<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>

<tx:method name="*" propagation="SUPPORTS" read-only="true"/>

 

</tx:attributes>

 

</tx:advice>

<!-- 组装监听和切入点 -->

<aop:config>

<aop:pointcut expression="execution(* com.ssh2.demo.*.service..*.*(..))" id="pcut"/>

<aop:advisor advice-ref="txadvice" pointcut-ref="pcut"/>

</aop:config>

 

 

 

</beans>

 

   Jdbc.properties:

 

connection.driver_class com.mysql.jdbc.Driver

connection.url jdbc:mysql://127.0.0.1:3306/test1

connection.username root

connection.password 123456

 

 

hibernate.dialect org.hibernate.dialect.MySQLDialect

hibernate.show_sql true

hibernate.format_sql true

hibernate.hbm2ddl.auto update

   Struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.action.extension" value="do"/>

<constant name="struts.devMode" value="true"/>

<!-- 把struts2创建action的权利交给spring容器 -->

<constant name="struts.objectFactory" value="spring"/>

</struts>

 

推荐阅读