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.objectFactory" value="spring" />
<package name="test" extends="struts-default">
<action name="login" class="login" method="execute">
<result>
/success.jsp
</result>
</action>
<action name="del" class="login" method="del">
<result name="del">
/success.jsp
</result>
</action>
<action name="addInfo" class="userInfo" method="add">
<result name="add">/test.jsp</result>
</action>
<action name="delInfo" class="userInfo" method="del">
<result name="del">/test.jsp</result>
</action>
</package>
</struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启注解 -->
<context:annotation-config />
<!-- 定义数据源的信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
<property name="maxPoolSize">
<value>80</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="initialPoolSize">
<value>1</value>
</property>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="annotatedClasses">
<list>
<value>test.cqb.entity.User</value>
<value>test.cqb.entity.UserInfo</value>
</list>
</property>
<!-- 配置Hibernate信息 -->
<property name="hibernateProperties">
<props>
<prop key="show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- 配置transactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置dao -->
<bean id="baseDao" class="test.cqb.dao.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userInfoDao" class="test.cqb.dao.UserInfoDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置service -->
<bean id="userService" class="test.cqb.service.UserService">
<property name="dao">
<ref bean="baseDao" />
</property>
</bean>
<bean id="userInfoService" class="test.cqb.service.UserInfoService">
<property name="dao">
<ref bean="userInfoDao" />
</property>
</bean>
<!-- 配置action -->
<bean id="login" class="test.cqb.action.LoginAction">
<property name="service">
<ref bean="userService" />
</property>
</bean>
<bean id="userInfo" class="test.cqb.action.UserInfoAction">
<property name="service">
<ref bean="userInfoService" />
</property>
</bean>
<!--
<aop:config>
<aop:pointcut expression="execution(* test.cqb.service.*(..))"
id="allpoint" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allpoint" />
</aop:config>
-->
<!-- 激活事务注释 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
dao示例
package test.cqb.dao;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import test.cqb.entity.User;
public class UserDao implements BaseDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void save(User user) throws HibernateException {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(user);
}
@Override
public void update(User user) throws HibernateException {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().update(user);
}
@Override
public void delete(User user) throws HibernateException {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().delete(user);
}
@Override
public User findById(int id) throws HibernateException {
// TODO Auto-generated method stub
return (User) sessionFactory.getCurrentSession().get(User.class, id);
}
}
//service示例
package test.cqb.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import test.cqb.dao.BaseDao; import test.cqb.dao.UserDao; import test.cqb.entity.User; @Transactional public class UserService implements BaseService { private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; } @Override public void addUser(User user) { // TODO Auto-generated method stub dao.save(user); } @Override public void delUser(User user) { dao.delete(user); } @Override public User findById(int id) { // TODO Auto-generated method stub return dao.findById(id); } @Override public void updateUser(User user) { // TODO Auto-generated method stub dao.update(user); } }
//action示例
package test.cqb.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import test.cqb.entity.User; import test.cqb.service.BaseService; import test.cqb.service.UserService; public class LoginAction extends ActionSupport { private UserService service; public void setService(UserService service) { this.service = service; } public UserService getService() { return service; } @Override public String execute() throws Exception { User user = new User(); user.setName("chenqingbin"); user.setPsd("1234"); ActionContext.getContext().getSession().put("user", user); service.addUser(user); return SUCCESS; } public String del() throws Exception { User user = service.findById(25); //user.setName("chenfeifei"); service.delUser(user); return "del"; } }