链接: https://pan.baidu.com/s/1QFXZ1oAfUJSHno5wON1Ybg 密码: jdst
注意:javassist-3.18.1-GA.jar包与hibernate中的重复
注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常
core|beans|context|expression|logging|log4j
spring-web
spring-aop|spring-aspect|aop联盟|aopweaving
spring-jdbc|spring-tx|c3p0|spring-orm
spring-test
standard.jar
jstl-1.2.jar
<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 ">
<bean name="userAction" class="cn.zdfy.web.action.UserAction">bean>
beans>
web.xml文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<struts>
<package name="crm" namespace="/" extends="struts-default">
<action name="UserAction_*" class="cn.zdfy.web.action.UserAction"
method="{1}">
<result name="success">/success.jspresult>
action>
package>
struts>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<constant name="struts.objectFactory" value="spring">constant>
<action name="UserAction_*" class="cn.zdfy.web.action.UserAction"
method="{1}">
<result name="success">/success.jspresult>
action>
不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.
applicationContext.xml
<bean name="userAction" class="cn.zdfy.web.action.UserAction">
<property name="userService" ref="userService">property>
bean>
struts.xml
<action name="UserAction_*" class="userAction"
method="{1}">
<result name="success">/success.jspresult>
action>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///crm_32property>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">rootproperty>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="cn/zdfy/domain/Customer.hbm.xml" />
<mapping resource="cn/zdfy/domain/LinkMan.hbm.xml" />
<mapping resource="cn/zdfy/domain/User.hbm.xml" />
session-factory>
hibernate-configuration>
将sessionFactory对象交给spring容器管理
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name="configLocation" value="classpath:hibernate.cfg.xml" >property>
bean>
<bean name="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
prop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
<property name="mappingDirectoryLocations" value="classpath:cn/zdfy/domain">property>
bean>
jdbc.jdbcUrl=jdbc:mysql:///crm_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
<context:property-placeholder location="classpath:db.properties" />
<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>
<property name="dataSource" ref="dataSource">property>
//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao
package cn.zdfy.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateCallback;
import cn.zdfy.dao.UserDao;
import cn.zdfy.domain.User;
//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public User getByUserCode(final String usercode) {
// HQL
return getHibernateTemplate().execute(new HibernateCallback() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where user_code = ?";
Query query = session.createQuery(hql);
query.setParameter(0, usercode);
return (User) query.uniqueResult();
}
});
}
}
package cn.zdfy.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateCallback;
import cn.zdfy.dao.UserDao;
import cn.zdfy.domain.User;
//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public User getByUserCode(final String usercode) {
// Criteria
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add(Restrictions.eq("user_code", usercode));
List list = (List) getHibernateTemplate().findByCriteria(dc);
if (list != null && list.size() > 0) {
return list.get(0);
} else {
return null;
}
}
}
<bean name="userDao" class="cn.zdfy.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<bean name="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<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>
<aop:config>
<aop:pointcut expression="execution(* cn.zdfy.service.impl.*ServiceImpl.*(..))"
id="txPc" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
aop:config>
<tx:annotation-driven transaction-manager="transactionManager"/>
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService {
//======================================================//
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void saveUser(User u) {
ud.save(u);
}
为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围
<filter>
<filter-name>openSessionInViewfilter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
filter>
<filter-mapping>
<filter-name>openSessionInViewfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>