一、新建web项目导入所需jar包
1)struts2.jar包
下载:http://struts.apache.org/download.cgi#struts2231
完成后解压struts-2.2.3-all,到\struts-2.2.3\apps\struts2-blank\WEB-INF\lib下拷贝所有jar包(共11个)
此外因为要整合spring及使用json对象,所以需要到struts-2.2.3-all\lib下拷贝如下jar包(共3个)
struts2-json-plugin-2.2.3.jar
json-lib-2.1-jdk15.jar
struts2-spring-plugin-2.2.3.jar
2)spring.jar包
下载:http://www.springsource.org/download
完成后解压spring-framework-3.0.6.RELEASE,到\dist\下拷贝所需jar包(共11个)
org.springframework.aop-3.0.6.RELEASE.jar
org.springframework.asm-3.0.6.RELEASE.jar
org.springframework.aspects-3.0.6.RELEASE.jar
org.springframework.beans-3.0.6.RELEASE.jar
org.springframework.context.support-3.0.6.RELEASE.jar
org.springframework.context-3.0.6.RELEASE.jar
org.springframework.core-3.0.6.RELEASE.jar
org.springframework.expression-3.0.6.RELEASE.jar
org.springframework.jdbc-3.0.6.RELEASE.jar
org.springframework.transaction-3.0.6.RELEASE.jar
org.springframework.web-3.0.6.RELEASE.jar
下载依赖包:spring-framework-3.0.2.RELEASE-dependencies,导入以下包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
3)hibernate.jar包
下载: http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.7.Final/
完成后解压hibernate-distribution-3.6.7.Final,拷贝
hibernate3.jar
进入/lib/required/目录下拷贝所有包
hibernate-jpa-2.0-api-1.0.1.Final.jar
二、配置
1)配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <!-- 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>
- <!-- spring配置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 这里指定spring配置文件置于src目录下 -->
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- struts整合spring -->
- <constant name="struts.objectFactory" value="spring"></constant>
- <package name="default" namespace="/" extends="struts-default">
- </package>
- </struts>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 配置 Spring 的 支持注解方式 -->
- <context:annotation-config />
- <!-- 数据库的连接池配置 -->
- <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/onlinepm" />
- <property name="username" value="root" />
- <property name="password" value="123" />
- </bean>
- <!-- 配置 hibernate的SessionFactory-->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="mysqlDataSource" />
- <property name="mappingResources">
- <list>
- <value>/com/gl/dao/bean/User.hbm.xml</value>
- <value>/com/gl/dao/bean/Role.hbm.xml</value>
- <value>/com/gl/dao/bean/Depart.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- show_sql=true
- </value>
- </property>
- </bean>
- <!-- 定义hibernate的事务管理对象-->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 开启注解事务管理 -->
- <tx:annotation-driven transaction-manager="transactionManager" />
- <!-- 其他的Bean配置-->
- <bean id="userDaoImple" class="com.gl.dao.imple.UserDaoImple"/>
- <bean id="userService" class="com.gl.service.imple.UserServiceImple"/>
- <!-- Struts 中的Action配置
- <bean id="listEmpsAction" class="com.gl.action.ListEmpsAction"></bean>
- -->
- </beans>
三、搭建项目框架
1)对struts(偏向control)
com.linxcool.action XXXAction
com.linxcool.interceptor XXXInterceptor
2)对spring(业务处理)
com.linxcool.service.iface IXXXService
com.linxcool.service.impl XXXServiceImpl
3)对hibernate(偏向model)
com.linxcool.bean XXX.java XXX.hbm.xml
com.linxcool.dao.iface IXXXDAO
com.linxcool.dao.impl XXXDAOImpl
4)工具包
com.linxcool.utils MD5加密 类型转换
四、测试
1)构建pojo(bean)及hbm文件,建议使用myeclipse或其他插件生成,不然很容易出错。
2)编写dao接口及dao对象
- public interface IUserDao {
- public abstract boolean save(User user) throws Exception;
- public abstract boolean delete(String id) throws Exception;
- public abstract boolean update(User user) throws Exception;
- public abstract List<User> getUsers() throws Exception;
- public abstract User getUserById(int id) throws Exception;
- }
- public class UserDaoImple implements IUserDao {
- /**启用注解*/
- @Autowired
- private SessionFactory sessionFactory;
- @Override
- public boolean save(User user)throws Exception{
- try {
- Session session=sessionFactory.getCurrentSession();
- session.persist(user);
- return true;
- } catch (Exception e) {
- throw e;
- }
- }
- @Override
- public boolean delete(String id)throws Exception{
- try {
- Session session=sessionFactory.getCurrentSession();
- session.delete(id, User.class);
- return true;
- } catch (Exception e) {
- throw e;
- }
- }
- @Override
- public boolean update(User user)throws Exception{
- try {
- Session session=sessionFactory.getCurrentSession();
- session.merge(user);
- return true;
- } catch (Exception e) {
- throw e;
- }
- }
- @SuppressWarnings("unchecked")
- @Override
- public List<User> getUsers()throws Exception{
- List<User> users=new ArrayList<User>();
- try {
- Session session=sessionFactory.getCurrentSession();
- users=session.createQuery("from User user").list();
- } catch (Exception e) {
- throw e;
- }
- return users;
- }
- @Override
- public User getUserById(int id)throws Exception{
- User user=new User();
- try {
- Session session=sessionFactory.getCurrentSession();
- user=(User)session.createQuery("from User user where id=?").setInteger(0, id).uniqueResult();
- } catch (Exception e) {
- throw e;
- }
- return user;
- }
- }
- public interface IUserService {
- public abstract boolean save(User user) throws Exception;
- public abstract boolean delete(String id) throws Exception;
- public abstract boolean update(User user) throws Exception;
- public abstract List<User> getUsers() throws Exception;
- public abstract User getUserById(int id) throws Exception;
- }
- /**启用事物管理*/
- @Transactional
- public class UserServiceImple implements IUserService {
- @Autowired
- private UserDaoImple userDaoImple;
- @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.SERIALIZABLE)
- @Override
- public boolean save(User user)throws Exception{
- return userDaoImple.save(user);
- }
- @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.SERIALIZABLE)
- @Override
- public boolean delete(String id)throws Exception{
- return userDaoImple.delete(id);
- }
- @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.SERIALIZABLE)
- @Override
- public boolean update(User user)throws Exception{
- return userDaoImple.update(user);
- }
- @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
- @Override
- public List<User> getUsers()throws Exception{
- return userDaoImple.getUsers();
- }
- @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
- @Override
- public User getUserById(int id)throws Exception{
- return userDaoImple.getUserById(id);
- }
- }
- public class UserServiceTest {
- /**这里必须是接口 不然会出错*/
- private IUserService userService;
- @Before
- public void setUp() throws Exception {
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- userService=(IUserService)context.getBean("userService");
- }
- @Test
- public void testSave() {
- User user=new User();
- user.setName("catlin");
- user.setJobNum("LINX002");
- user.setPwd("02133");
- Depart depart=new Depart();
- depart.setId(1);
- Role role=new Role();
- role.setId(1);
- user.setDepart(depart);
- user.setRole(role);
- try {
- System.out.println(userService.save(user));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Test
- public void testDelete() {
- try {
- userService.delete("1");
- } catch (Exception e) {
- e.printStackTrace();
- }
- fail("Not yet implemented");
- }
- @Test
- public void testUpdate() {
- try {
- User user=userService.getUserById(2);
- user.setName("iso9001");
- userService.update(user);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Test
- public void testGetUsers() {
- try {
- List<User> users=userService.getUsers();
- for (User user : users) {
- System.out.println(user.getName());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Test
- public void testGetUserById() {
- try {
- User user=userService.getUserById(1);
- System.out.println(user.getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }