开发工具:eclipse JEE luna
服务器:apache-tomcat-6.0.48
JAVA: jdk-7u79
数据库:mysql-5.6.24
操作系统:window xp
(有源码提供下载)
在WEB-INF/下面配置web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>ssh</display-name>
<context-param> <!-- 启动spring4 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter> <!-- 启动struts2 -->
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
在WEB-INF/下面配置bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"1111111111111111111111
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!-- 启用扫描注解bean,为了移除bean配制-->
<context:component-scan base-package="cn.akelin" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 数据库信息 -->
<property name="dataSource" ref="dataSource" />
<!-- 可选信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<!-- hbm文件信息 -->
<property name="mappingResources">
<list>
<value>cn/akelin/domain/Employee.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器,txManager管理 sessionFactory -->
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 注解驱动事务:指明事务管理器:txManager -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="60" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="60" />
</bean>
</beans>
在WEB-INF/下面配置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.serve.static.browserCache" value="false" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.multipart.saveDir" value="/images/tempdir" />
<constant name="struts.multipart.maxSize" value="5242880" />
<constant name="struts.i18n.encodeing" value="UTF-8"/>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<!-- 指定spring 管理struts-plugin.xml -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="employee" namespace="/employee" extends="struts-default">
<action name="list" class="employeeAction">//调用bean id=employeeAction
<result name="list">/WEB-INF/page/employee.jsp</result>
</action>
<action name="manage_*" class="employeeManageAction" method="{1}">
<result name="add">/WEB-INF/page/employeeAdd.jsp</result>
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
EmployeeServiceBean.java
package cn.akelin.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.akelin.domain.Employee;
import cn.akelin.service.EmployeeService;
@Service("employeeService")
//服务层:扫描注解bean id=employeeService
@Transactional
//注解开启事务驱动
public class EmployeeServiceBean implements EmployeeService {
@Resource//装配sessionFactory,spring自动开启Session
SessionFactory sessionFactory;//相当于new一个bean出来,现在这个被spring自动装配
@Override
public void save(Employee employee) {
sessionFactory.getCurrentSession().persist(employee);//saveOrUpdate()
}
@Override
public void update(Employee employee) {
sessionFactory.getCurrentSession().merge(employee);//saveOrUpdate()
}
//注解不需要事务
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public Employee findById(int id) {
return sessionFactory.getCurrentSession().get(Employee.class, id);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public List findByName(String username) {
return sessionFactory.getCurrentSession()
.createQuery("from Employee e where e.username=:username")
.setString("username", username).list();
}
@Override
public void delete(int... id) {
for (int i : id) {
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().get(Employee.class, i));
}
}
@SuppressWarnings("unchecked")
public List<Employee> list() {
return sessionFactory.getCurrentSession().createQuery("from Employee")
.list();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
EmployeeManageAction.java
package cn.akelin.action;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.akelin.domain.Employee;
import cn.akelin.service.EmployeeService;
@Controller("employeeManageAction")//注册:bean id=employeeManageAction
@Scope("prototype")//每次请求,都创建bean
public class EmployeeManageAction {
@Resource
private EmployeeService employeeService;
private Employee employee;
public String addUI() {
return "add";//返回add视图
}
public String add() {
System.out.println("employeeService.save(employee)");
System.out.println(employee.toString());
employeeService.save(employee);
ActionContext.getContext().put("message", "保存成功");
return "message";//返回message视图
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
EmployeeAction.java
package cn.akelin.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.akelin.service.EmployeeService;
@Controller("employeeAction")//扫描注解bean,bean id=employeeAction
//employeeAction
public class EmployeeAction {
@Resource
//装配bean:employeeService//相当于new 一个bean出来
EmployeeService employeeService;
public String execute() {
ActionContext.getContext().put("employees", employeeService.list());
return "list";
}
}
cn/akelin/domain/Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-1-16 0:10:24 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="cn.akelin.domain.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="gender">
<column name="GENDER" />
<type name="org.hibernate.type.EnumType">
<param name="type">12</param>
<param name="enumClass">cn.akelin.domain.Gender</param>
</type>
</property>
</class>
</hibernate-mapping>
EmployeeService.java
package cn.akelin.service;
import java.util.List;
import cn.akelin.domain.Employee;
public interface EmployeeService {
public void save(Employee employee);
public void update(Employee employee);
public Employee findById(int id);
public List findByName(String username);
public void delete(int... id);
public List<Employee> list();
}