struts2.2.3 + spring3.1.0 + mybatis3.1.0集成简单demo

来源:互联网 发布:mac如何u盘安装win7 编辑:程序博客网 时间:2024/05/17 04:28

struts2.2.3 + spring3.1.0 + mybatis3.1.0集成简单demo

项目下载地址:http://download.csdn.net/detail/afgasdg/4171359

主要实现用户的增删改查操作

1、导入相应的jar包


2、配置web.xml主要是配置struts2和spring

web.xml文件内容如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.   
  10.     <!-- 加载spring的配置文件 -->  
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.     </listener>  
  14.     <!-- 配置spring配置文件加载的位置 -->  
  15.     <context-param>  
  16.         <param-name>contextConfigLocation</param-name>  
  17.         <param-value>classpath:beans.xml</param-value>  
  18.     </context-param>  
  19.   
  20.   
  21.     <!-- 配置struts2 -->  
  22.     <filter>  
  23.         <filter-name>struts2</filter-name>  
  24.         <filter-class>  
  25.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  26.     </filter>  
  27.     <filter-mapping>  
  28.         <filter-name>struts2</filter-name>  
  29.         <url-pattern>/*</url-pattern>  
  30.     </filter-mapping>  
  31.   
  32.   
  33. </web-app>  
3、配置spring配置文件,主要包括配置数据源、事务、mybaits等

beans.xml配置文件详细如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.            http://www.springframework.org/schema/aop   
  10.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  11.            http://www.springframework.org/schema/tx  
  12.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.            http://www.springframework.org/schema/context  
  14.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  15.       
  16.     <!-- 采用注释的方式配置bean -->  
  17.     <context:annotation-config />  
  18.     <!-- 配置要扫描的包 -->  
  19.     <context:component-scan base-package="com.pdsu.edu"></context:component-scan>  
  20.       
  21.     <!--proxy-target-class="true"强制使用cglib代理   如果为false则spring会自动选择-->  
  22.     <aop:aspectj-autoproxy  proxy-target-class="true"/>  
  23.       
  24.     <!-- 数据库配置文件位置 -->  
  25.     <context:property-placeholder location="classpath:jdbc.properties" />  
  26.       
  27.     <!-- 配置dbcp数据源 -->  
  28.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  29.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  30.         <property name="url" value="${jdbc.url}" />  
  31.         <property name="username" value="${jdbc.username}" />  
  32.         <property name="password" value="${jdbc.password}" />  
  33.         <!-- 队列中的最小等待数 -->  
  34.         <property name="minIdle" value="${jdbc.minIdle}"></property>  
  35.         <!-- 队列中的最大等待数 -->  
  36.         <property name="maxIdle" value="${jdbc.maxIdle}"></property>  
  37.         <!-- 最长等待时间,单位毫秒 -->  
  38.         <property name="maxWait" value="${jdbc.maxWait}"></property>  
  39.         <!-- 最大活跃数 -->  
  40.         <property name="maxActive" value="${jdbc.maxActive}"></property>  
  41.         <property name="initialSize" value="${jdbc.initialSize}"></property>  
  42.     </bean>  
  43.       
  44.     <!-- 配置mybitasSqlSessionFactoryBean -->  
  45.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  46.         <property name="dataSource" ref="dataSource" />  
  47.         <property name="configLocation" value="classpath:mybatis.xml"></property>  
  48.     </bean>  
  49.       
  50.     <!-- 配置SqlSessionTemplate -->  
  51.     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">  
  52.         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />  
  53.     </bean>  
  54.       
  55.     <!-- 事务配置 -->  
  56.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  57.         <property name="dataSource" ref="dataSource" />  
  58.     </bean>  
  59.       
  60.     <!-- 使用annotation注解方式配置事务 -->  
  61.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  62.   
  63. </beans>  

4.JDBC配置文件详细

  

[plain] view plaincopy
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/operationLog  
  3. jdbc.username=root  
  4. jdbc.password=  
  5. jdbc.maxActive = 2  
  6. jdbc.maxIdle =5  
  7. jdbc.minIdle=1  
  8. jdbc.initialSize =3  
  9. jdbc.maxWait =3000  

5、配置mybatis主配置文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  3. <configuration>  
  4.     <typeAliases>  
  5.         <typeAlias alias="user" type="com.pdsu.edu.domain.User"/>  
  6.     </typeAliases>  
  7.     <mappers>  
  8.         <mapper resource="com/pdsu/edu/domain/sqlMappers/user.xml" />  
  9.     </mappers>  
  10. </configuration>  

6、配置user.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="com.pdsu.edu.domain.User">  
  5.       
  6.     <resultMap type="com.pdsu.edu.domain.User" id="userResult">  
  7.         <result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />  
  8.         <result property="username" column="username" />  
  9.         <result property="password" column="password" />  
  10.     </resultMap>  
  11.     <select id="userLogin"  parameterType="user" resultMap="userResult">  
  12.         select * from user   
  13.         where   
  14.             username=#{username} and password=#{password}  
  15.     </select>  
  16.   
  17.     <select id="selectAllUser" resultMap="userResult">  
  18.         select * from user  
  19.     </select>  
  20.   
  21.     <select id="findUserById" parameterType="int" resultMap="userResult">  
  22.         select *  
  23.         from user where id=#{id}  
  24.     </select>  
  25.   
  26.     <insert id="insertUser" parameterType="user">  
  27.      <![CDATA[ 
  28.         insert into 
  29.         user(username,password) values(#{username},#{password}) 
  30.         ]]>  
  31.     </insert>  
  32.   
  33.     <update id="updateUser" parameterType="user">  
  34.         update user set  
  35.         username=#{username},password=#{password} where id=#{id}  
  36.     </update>  
  37.       
  38.     <delete id="deleteUser" parameterType="int">  
  39.         delete from user where  
  40.         id=#{id}  
  41.     </delete>  
  42.   
  43. </mapper>  

7、User实体的写法

[java] view plaincopy
  1. public class User implements Serializable {  
  2.     private static final long serialVersionUID = -4415990281535582814L;  
  3.     private Integer id;  
  4.     private String username;  
  5.     private String password;  
  6.   
  7.     public Integer getId() {  
  8.         return id;  
  9.     }  
  10.   
  11.     public void setId(Integer id) {  
  12.         this.id = id;  
  13.     }  
  14.   
  15.     public String getUsername() {  
  16.         return username;  
  17.     }  
  18.   
  19.     public void setUsername(String username) {  
  20.         this.username = username;  
  21.     }  
  22.   
  23.     public String getPassword() {  
  24.         return password;  
  25.     }  
  26.   
  27.     public void setPassword(String password) {  
  28.         this.password = password;  
  29.     }  
  30.   
  31.     @Override  
  32.     public String toString() {  
  33.         return "User [id=" + id + ", password=" + password + ", username=" + username + "]";  
  34.     }  
  35.   
  36.     @Override  
  37.     public int hashCode() {  
  38.         final int prime = 31;  
  39.         int result = 1;  
  40.         result = prime * result + ((id == null) ? 0 : id.hashCode());  
  41.         return result;  
  42.     }  
  43.   
  44.     @Override  
  45.     public boolean equals(Object obj) {  
  46.         if (this == obj)  
  47.             return true;  
  48.         if (obj == null)  
  49.             return false;  
  50.         if (getClass() != obj.getClass())  
  51.             return false;  
  52.         User other = (User) obj;  
  53.         if (id == null) {  
  54.             if (other.id != null)  
  55.                 return false;  
  56.         } else if (!id.equals(other.id))  
  57.             return false;  
  58.         return true;  
  59.     }  
  60. }  

8、UserDao的写法

[java] view plaincopy
  1. public interface UserDao {  
  2.   
  3.     public abstract void insertUser(User user);  
  4.   
  5.     public abstract void updateUser(User user);  
  6.   
  7.     public abstract void deleteUser(Integer userId);  
  8.   
  9.     public abstract User findUserByid(Integer userId);  
  10.   
  11.     public abstract List<User> findAll();  
  12.   
  13.     public abstract User userLogin(User user);  
  14.   
  15. }  

9、UserDao的实现

[java] view plaincopy
  1. @Repository  
  2. public class UserDaoImpl implements UserDao {  
  3.     private final String INSERT_USER = "insertUser";  
  4.     private final String UPDATE_USER = "updateUser";  
  5.     private final String DELETE_USER = "deleteUser";  
  6.     private final String FIND_USER_BYID = "findUserById";  
  7.     private final String SELECT_ALL_USER = "selectAllUser";  
  8.     private final String USER_LOGIN = "userLogin";  
  9.     @Autowired  
  10.     private SqlSessionTemplate sqlSessionTemplate;  
  11.   
  12.     public void insertUser(User user) {  
  13.         sqlSessionTemplate.insert(INSERT_USER, user);  
  14.     }  
  15.   
  16.     public void updateUser(User user) {  
  17.         sqlSessionTemplate.update(UPDATE_USER, user);  
  18.     }  
  19.   
  20.     public void deleteUser(Integer userId) {  
  21.         sqlSessionTemplate.delete(DELETE_USER, userId);  
  22.     }  
  23.   
  24.     public User findUserByid(Integer userId) {  
  25.         return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId);  
  26.     }  
  27.   
  28.     public List<User> findAll() {  
  29.         return sqlSessionTemplate.selectList(SELECT_ALL_USER);  
  30.     }  
  31.   
  32.     public User userLogin(User user) {  
  33.         return sqlSessionTemplate.selectOne(USER_LOGIN, user);  
  34.     }  
  35. }  

10、UserService接口

[java] view plaincopy
  1. public interface UserService {  
  2.   
  3.     // 添加用户  
  4.     public abstract void addUser(User user);  
  5.   
  6.     public abstract void updateUser(User user);  
  7.   
  8.     public abstract void deleteUser(Integer userId);  
  9.   
  10.     public abstract User findUserById(Integer userId);  
  11.   
  12.     public abstract List<User> findAllUser();  
  13.   
  14.     public abstract User login(User user);  
  15.   
  16. }  

11、UserService接口的实现

[java] view plaincopy
  1. @Service  
  2. @Transactional  
  3. public class UserServiceImpl implements UserService {  
  4.   
  5.     @Autowired  
  6.     private UserDao userDao;  
  7.   
  8.     // 添加用户  
  9.     public void addUser(User user) {  
  10.         userDao.insertUser(user);  
  11.     }  
  12.   
  13.     // 更新用户  
  14.     public void updateUser(User user) {  
  15.         userDao.updateUser(user);  
  16.     }  
  17.   
  18.     public void deleteUser(Integer userId) {  
  19.         userDao.deleteUser(userId);  
  20.     }  
  21.   
  22.     public User findUserById(Integer userId) {  
  23.         return userDao.findUserByid(userId);  
  24.     }  
  25.   
  26.     public List<User> findAllUser() {  
  27.         return userDao.findAll();  
  28.     }  
  29.   
  30.     public User login(User user) {  
  31.         return userDao.userLogin(user);  
  32.     }  
  33. }  

12、配置Struts2

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>    
  4.      <constant name="struts.i18n.encoding" value="UTF-8"/>  
  5.             <!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 -->  
  6.     <constant name="struts.configuration.xmlreload" value="true"/>  
  7.             <!-- 当struts配置文件修改时是否自动加载 -->  
  8.     <constant name="struts.devMode" value="true"/>  
  9.             <!-- 开发模式下打印详细的错误信息 -->  
  10.     <constant name="struts.ui.theme" value="xhtml"/>  
  11.       
  12.     <package name="user" namespace="/user" extends="struts-default">  
  13.         <action name="user_*" class="userAction" method="{1}">  
  14.             <result name="success" type="redirectAction">user_queryAllUser.action</result>  
  15.             <result name="input">/index.jsp</result>  
  16.             <result name="userList">/userList.jsp</result>  
  17.             <result name="addUser">/userAdd.jsp</result>  
  18.             <result name="updateUser">/userUpdate.jsp</result>  
  19.         </action>  
  20.     </package>  
  21. </struts>      

13、UserAction具体实现

[java] view plaincopy
  1. @Controller  
  2. @Scope("prototype")  
  3. public class UserAction extends ActionSupport {  
  4.     @Autowired  
  5.     private UserService userService;  
  6.     private User user;  
  7.     private List<User> userList;  
  8.   
  9.     public String execute() throws Exception {  
  10.         return null;  
  11.     }  
  12.   
  13.     public String login() {  
  14.         if (user != null) {  
  15.             User user2 = userService.login(user);  
  16.             if (user2 != null) {  
  17.                 return SUCCESS;  
  18.             }  
  19.         }  
  20.         this.addFieldError("user.username""用户名或密码错误!");  
  21.         return INPUT;  
  22.     }  
  23.   
  24.     public String addUI() {  
  25.         return "addUser";  
  26.     }  
  27.   
  28.     public String updateUI() {  
  29.         user = userService.findUserById(user.getId());  
  30.         return "updateUser";  
  31.     }  
  32.   
  33.     public String add() {  
  34.         userService.addUser(user);  
  35.         return SUCCESS;  
  36.     }  
  37.   
  38.     public String delete() {  
  39.         userService.deleteUser(user.getId());  
  40.         return SUCCESS;  
  41.     }  
  42.   
  43.     public String update() {  
  44.         userService.updateUser(user);  
  45.         return SUCCESS;  
  46.     }  
  47.   
  48.     public User getUser() {  
  49.         return user;  
  50.     }  
  51.   
  52.     public void setUser(User user) {  
  53.         this.user = user;  
  54.     }  
  55.   
  56.     public String queryAllUser() {  
  57.         userList = userService.findAllUser();  
  58.         return "userList";  
  59.     }  
  60.   
  61.     public List<User> getUserList() {  
  62.         return userList;  
  63.     }  
  64.   
  65.     public void setUserList(List<User> userList) {  
  66.         this.userList = userList;  
  67.     }  
  68.   
  69. }  

14、登录页面的实现

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s"  uri="/struts-tags"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>用户登录</title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.     <s:head/>  
  23.   </head>  
  24.     
  25.   <body>  
  26.     <center>  
  27.         <h1>用户登录</h1>  
  28.         <s:a action="user_addUI" namespace="/user">添加新用户</s:a>  
  29.         <s:form action="user_login" namespace="/user" method="post">  
  30.             <s:textfield label="用户名" name="user.username"></s:textfield>  
  31.             <s:password label="密码" name="user.password"></s:password>  
  32.             <s:submit value="登录"></s:submit>  
  33.         </s:form>  
  34.     </center>  
  35.   </body>  
  36. </html>  

15、添加页面
[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s"  uri="/struts-tags"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.       
  7.     <title>添加新用户</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">     
  12.   
  13.   </head>  
  14.     
  15.   <body>  
  16.     <center>  
  17.         <h1>添加新用户</h1>  
  18.         <s:form action="user_add" namespace="/user" method="post">  
  19.             <s:textfield label="用户名" name="user.username"></s:textfield>  
  20.             <s:password label="密码" name="user.password"></s:password>  
  21.             <s:submit value="提交"></s:submit>  
  22.         </s:form>  
  23.     </center>  
  24.   </body>  
  25. </html>  

16、修改页面

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s"  uri="/struts-tags"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.       
  7.     <title>修改用户</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">     
  12.   
  13.   </head>  
  14.     
  15.   <body>  
  16.     <center>  
  17.         <h1>修改用户</h1>  
  18.         <s:form action="user_update" namespace="/user" method="post">  
  19.             <s:hidden name="user.id"></s:hidden>  
  20.             <s:textfield label="用户名" name="user.username"></s:textfield>  
  21.             <s:password label="密码" name="user.password"></s:password>  
  22.             <s:submit value="提交"></s:submit>  
  23.         </s:form>  
  24.     </center>  
  25.   </body>  
  26. </html>  

17、列表页面

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s"  uri="/struts-tags"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.       
  7.     <title>用户列表</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">     
  12.   
  13.   </head>  
  14.     
  15.   <body>  
  16.     <center>  
  17.         <h2>用户列表</h2>  
  18.         <h3><s:a action="user_addUI" namespace="/user">添加新用户</s:a> </h3>  
  19.         <table width="90%" border="1">  
  20.             <tr>  
  21.                 <th>用户id</th>  
  22.                 <th>用户名称</th>  
  23.                 <th>用户密码</th>  
  24.                 <th>操作</th>  
  25.             </tr>  
  26.             <s:iterator  value="userList">  
  27.                 <tr>  
  28.                     <td><s:property value="id"/> </td>  
  29.                     <td><s:property value="username"/> </td>  
  30.                     <td><s:property value="password"/> </td>  
  31.                     <td><s:a action="user_updateUI" namespace="/user"><s:param name="user.id">${id}</s:param>修改</s:a>  
  32.                       <s:a action="user_delete" namespace="/user"><s:param name="user.id">${id}</s:param>删除</s:a></td>  
  33.                 </tr>  
  34.             </s:iterator>  
  35.         </table>  
  36.     </center>  
  37.   </body>  
  38. </html>  

项目下载地址:http://download.csdn.net/detail/afgasdg/4171359
0 0