1.三大框架整合原理
2.导包(41)
1>hibernate包
hibernate/lib/required下的所有必须包:9个
数据库驱动包:1个
2>struts2包
struts2-blank\WEB-INF\lib所有必须包:13个
struts整合spring插件包:struts2-spring-plugin-2.3.24.jar(这个包导入后在struts2启动时就会寻找spring容器,找不到即有异常)1个
注意:javassist-3.18.1-GA.jar包与hibernate中的重复
3>spring包
基本:4+2
整合web包:1个
整合aop包:spring-aop|spring-aspect|aop联盟|aopweaving 4个
整合hibernate和事务:spring-jdbc|spring-tx|c3p0|spring-orm 4个
测试包:spring-test 1个
4>jstl标签库包
standard.jar、jstl.1.2jar 2个
3.单独配置spring容器
1>书写配置文件(导入约束:4)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
2>配置spring随项目启动
<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
4.单独配置struts2
1>配置struts2主配置文件(导入约束)
<?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>
<package name="crm" namespace="/" extends="struts-default">
<action name="UserAction_*" class="con.imwj.web.action.UserAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
2>配置struts2核心过滤器到web.xml
<!-- struts2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.struts与spring整合
1>配置常量
<!-- struts.xml中:配置常量 -->
<constant name="struts.objectFactory" value="spring"></constant>
2>整合:spring负责创建action以及组装
<!-- application --><!-- action -->
<bean name="userAction" class="com.imwj.web.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
<!-- struts.xmlclass属性上填写spring中action对象的BeanName完全由spring管理action生命周期,包括Action的创建
注意:需要手动组装依赖属性 -->
<package name="crm" namespace="/" extends="struts-default">
<action name="UserAction_*" class="userAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
6.单独配置hibernate
1>导入实体类和orm元数据
2>配置主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库地址和名称 -->
<property name="hibernate.connection.url">jdbc:mysql:///ssh_crm</property>
<!-- 连接名 -->
<property name="hibernate.connection.username">root</property>
<!-- 连接密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- 选择数据库引擎:默认通用的(最短):数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 在控制台显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 按格式显示sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表:按何种方式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定session与当前线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 映射表的路径 -->
<mapping resource="com/imwj/domain/Customer.hbm.xml" />
<mapping resource="com/imwj/domain/Linkman.hbm.xml" />
<mapping resource="com/imwj/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
7.spring整合hibernate
1>整合原理
将sessionFactory对象交给spring容器管理
2>spring中配置sessionFactory
<!-- spring整合hibernate:在spring中放置配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置hibernate的基本信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.url" >jdbc:mysql:///ssh_crm</prop>
<prop key="hibernate.connection.username" >root</prop>
<prop key="hibernate.connection.password" >123456</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 引入orm元数据,指定所在包路径,spring自动读取 -->
<property name="mappingDirectoryLocations" value="classpath:com/imwj/domain"></property>
</bean>
8.spring整合c3p0连接池
1>配置db.properties
jdbc.jdbcUrl=jdbc:mysql:///ssh_crm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
2>引入到spring中
<!-- 读取db.properties配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
3>连接池注入给sessionFactory
<!-- 将c3p0连接池放入sessionfactory,hibernate会通过连接池获取到连接 -->
<property name="dataSource" ref="dataSource" ></property>
9.spring整合hibernate环境操作数据库
1>Dao继承HibernateDaoSupprot
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
2>hibernate模板操作
public User getUserByCode(final String userCode) {
return getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where user_code = ? ";
Query query = session.createQuery(hql);
query.setParameter(0, userCode);
User user = (User) query.uniqueResult();
return user;
}
});
}
3>spring中配置dao
<!-- dao -->
<bean name="userDao" class="com.imwj.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
10.spring中的aop事务
1>核心事务管理器
<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2>配置通知
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性,name:方法名(*通配符,只要是以saver开头的方法...),isolation:隔离级别
propagation:传播行为,read-only:是否只读-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
3>配置织入
<!-- 配置织入 -->
<aop:config >
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.imwj.service.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 : 通知+切点,advice-ref:通知的名称,pointcut-ref:切点的名称 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
11.扩大session范围(解决懒加载no-session问题)
为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围
web.xml中配置filter
<!-- 扩大session作用范围
注意: 任何filter一定要在struts的filter之前调用 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
12.源码
github地址:https://github.com/weixiaojian/ssh