SSH整合笔记
Spring+Struts2+Hibernate 整合笔记
1.导包
Hibernate导包
hibernate/lib/required/*目录下的全部的jar包
hibernate/lib/jpa | java persist api java的持久化规范(接口)
数据库驱动(MySQL)
Struts2 导包
struts-blank.war/WEB-INF/lib/*目录下的全部的jar包
注意:javassist-3.18.1-GA.jar包与hibernate中的重复,留下高版本
String 整合的插件包
Spring 导包
4个基本包+2个日志包
core|beans|context|expression|logging|log4j
web整合包:
spring-Web
整合aop 4 个:
spring-aop|spring-aspect|aop联盟|aopweaving
整合Hibernate和事务4个
spring-jdbc|spring-tx|c3p0|spring-orm
整合Junit4测试
spring-test
在eclipse下还需导入标签包
standard.jar
jstl-1.2.jar
共计41个包
2.单独配置spring容器
web.xml配置(使Spring随web启动而启动)
1 <!-- 配置Spring的监听器 --> 2 <listener> 3 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 4 </listener> 5 <!-- 为Spring容器创建传递参数 --> 6 <context-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:applicationContext.xml</param-value> 9 </context-param>
单独配置struts2
1.配置Struts2核心过滤器web.xml
1 <!-- struts2核心过滤器 --> 2 <filter> 3 <filter-name>struts2</filter-name> 4 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>struts2</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
Spring整合Struts2配置
配置常量
<constant name="struts.objectFactory" value="spring"></constant>
Spring负责创建Action负责创建
1 <package name="strutsTest" namespace="/" extends="struts-default"> 2 <!-- Class属性上填写Spring中Action对象的BeanName,由Spring来控制action的生命周期, 3 注意:需要手动处理依赖属性--> 4 <action name="strutsTest" class="cn.test.demo.StrutsTest" method="Test"> 5 <result name="success">/success.jsp</result> 6 </action> 7 </package>
1 <!-- action 对象作用范围一定是多例的prototype --> 2 <bean name="strutsTest" class="cn.test.demo.StrutsTest" scope="prototype"> 3 <property name="springTest" ref="springTest"></property> 4 </bean>
Spring 整合Hibernate配置
将sessionFactory交给Spring管理
1 <!-- 配置Hibernate基本信息 --> 2 <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource"></property> 4 <property name="hibernateProperties"> 5 <props> 6 <!-- 必要配置 --> 7 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 8 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> 9 <prop key="hibernate.connection.url">jdbc:mysql:///crm?useSSL=true</prop> 10 <prop key="hibernate.connection.username">root</prop> 11 <prop key="hibernate.connection.password">root</prop> 12 <!-- 可选配置 --> 13 <prop key="hibernate.show_sql">true</prop> 14 <prop key="hibernate.format_sql">true</prop> 15 <prop key="hibernate.hbm2ddl.auto">update</prop> 16 </props> 17 </property> 18 <property name="mappingDirectoryLocations" value="classpath:cn/test/domain"></property> 19 </bean>
1 <!-- 配置核心管理器 --> 2 <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 3 <property name="sessionFactory" ref="sessionFactory"></property> 4 </bean>
1 <!-- 配置事务策略 --> 2 <tx:advice id="txadvice" transaction-manager="transactionManager"> 3 <tx:attributes> 4 <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 5 <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 6 <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 7 <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 8 <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 9 <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> 10 <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> 11 </tx:attributes> 12 </tx:advice> 13 <!-- 配置将通知织入目标 --> 14 <aop:config> 15 <!-- 配置切点 --> 16 <!-- 配置作用那个包下那个类下的的那个方法 --> 17 <aop:pointcut expression="execution(* cn.test.demo.HibernateDemo.*(..))" id="txPc"/> 18 <!-- 配置切面 --> 19 <aop:advisor advice-ref="txadvice" pointcut-ref="txPc"/> 20 </aop:config>
Spring整合C3P0连接池
1 <!-- 读取c3p0配置文件 --> 2 <context:property-placeholder location="classpath:db.properties"/> 3 <!-- c3p0的配置 --> 4 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 5 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> 6 <property name="driverClass" value="${jdbc.driverClass}"></property> 7 <property name="user" value="${jdbc.user}"></property> 8 <property name="password" value="${jdbc.password}"></property> 9 </bean>