SSI整合 示例
sql语句
create table user_c (id varchar(10) primary key,name varchar(20),age int ,address varchar(30);)
1.引入jar包
1 antlr-2.7.6.jar 2 aspectjweaver.jar 3 backport-util-concurrent.jar 4 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 5 com.springsource.net.sf.cglib-2.2.0.jar 6 com.springsource.org.aopalliance-1.0.0.jar 7 com.springsource.org.apache.commons.logging-1.1.1.jar 8 com.springsource.org.quartz-1.6.2.jar 9 commons-collections-3.1.jar 10 commons-fileupload-1.2.1.jar 11 commons-httpclient-3.1.jar 12 commons-io-1.3.2.jar 13 dom4j-1.6.1.jar 14 ehcache-1.5.0.jar 15 freemarker-2.3.16.jar 16 hibernate3.jar 17 hibernate-jpa-2.0-api-1.0.0.Final.jar 18 javassist-3.12.0.GA.jar 19 javassist-3.7.ga.jar 20 jcommon-1.0.17.jar 21 jfreechart-1.0.14.jar 22 json-lib-2.3-jdk15.jar 23 jta-1.1.jar 24 log4j-1.2.13.jar 25 mybatis-3.2.2.jar 26 mybatis-spring-1.2.0.jar 27 mysql-connector-java-5.1.13-bin.jar 28 ognl-3.0.jar 29 org.springframework.aop-3.0.5.RELEASE.jar 30 org.springframework.asm-3.0.5.RELEASE.jar 31 org.springframework.aspects-3.0.5.RELEASE.jar 32 org.springframework.beans-3.0.5.RELEASE.jar 33 org.springframework.context.support-3.0.5.RELEASE.jar 34 org.springframework.context-3.0.5.RELEASE.jar 35 org.springframework.core-3.0.5.RELEASE.jar 36 org.springframework.expression-3.0.5.RELEASE.jar 37 org.springframework.instrument.tomcat-3.0.5.RELEASE.jar 38 org.springframework.instrument-3.0.5.RELEASE.jar 39 org.springframework.jdbc-3.0.5.RELEASE.jar 40 org.springframework.jms-3.0.5.RELEASE.jar 41 org.springframework.orm-3.0.5.RELEASE.jar 42 org.springframework.oxm-3.0.5.RELEASE.jar 43 org.springframework.test-3.0.5.RELEASE.jar 44 org.springframework.transaction-3.0.5.RELEASE.jar 45 org.springframework.web.portlet-3.0.5.RELEASE.jar 46 org.springframework.web.servlet-3.0.5.RELEASE.jar 47 org.springframework.web.struts-3.0.5.RELEASE.jar 48 org.springframework.web-3.0.5.RELEASE.jar 49 slf4j-api-1.6.1.jar 50 slf4j-nop-1.6.1.jar 51 struts2-core-2.2.1.jar 52 struts2-spring-plugin-2.2.1.jar 53 xwork-core-2.2.1.jar
2.写domain
User.java
1 package cn.itcast.mybatis.domain; 2 3 public class User { 4 private String id; 5 private String name; 6 private Integer age; 7 private String address; 8 public String getAddress() { 9 return address; 10 } 11 public void setAddress(String address) { 12 this.address = address; 13 } 14 public String getId() { 15 return id; 16 } 17 public void setId(String id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public Integer getAge() { 27 return age; 28 } 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 @Override 33 public String toString() { 34 return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; 35 } 36 37 }
MyUser.xml (mybatis对数据处理)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="cn.itcast.mybatis.domain.User"> 7 <sql id="cols"> 8 id,name,age,address 9 </sql> 10 <sql id="ucols"> 11 name=#{name},age=#{age},address=#{address} 12 </sql> 13 14 <!-- 查询所有记录 --> 15 <select id="listAll" resultType="aliasesUser"> 16 select <include refid="cols"/> from user_c 17 </select> 18 19 <!-- 按条件查询 --> 20 <select id="find" parameterType="aliasesUser" resultType="aliasesUser"> 21 select * from user_c 22 <where> 23 <if test="name!=null"> 24 and name like "%"#{name}"%" 25 </if> 26 <if test="age!=null"> 27 and age=#{age} 28 </if> 29 </where> 30 </select> 31 32 <!-- 查询一个用户 --> 33 <select id="get" parameterType="string" resultType="aliasesUser"> 34 select <include refid="cols"/> from user_c where id=#{id} 35 </select> 36 37 <!-- 新增 --> 38 <insert id="create" parameterType="aliasesUser"> 39 insert into user_c (id,name,age,address) values(#{id},#{name},#{age},#{address}) 40 </insert> 41 42 <!-- 修改 --> 43 <update id="update" parameterType="aliasesUser"> 44 update user_c 45 <set> 46 <if test="name!=null"> 47 name=#{name}, 48 </if> 49 <if test="age!=null"> 50 age=#{age}, 51 </if> 52 <if test="address!=null"> 53 address=#{address}, 54 </if> 55 </set> 56 57 where id=#{id} 58 </update> 59 60 <!-- 删除 --> 61 <delete id="delete" parameterType="string"> 62 delete from user_c where id=#{id} 63 </delete> 64 </mapper>
3.dao层处理
1 package cn.itcast.mybatis.dao; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.domain.User; 6 7 8 public interface IUserDao { 9 public List<User> list(); 10 public User get(String id); 11 public int insert(User u); 12 public int update(User u); 13 public int deleteById(String id); 14 }
daoImpl处理
1 package cn.itcast.mybatis.dao; 2 3 import java.util.List; 4 5 import org.mybatis.spring.support.SqlSessionDaoSupport; 6 7 import cn.itcast.mybatis.domain.User; 8 9 public class UserDaoImpl extends SqlSessionDaoSupport implements IUserDao { 10 11 //删除 12 public int deleteById(String id) { 13 return this.getSqlSession().delete("cn.itcast.mybatis.domain.User.delete",id); 14 } 15 16 //新增 17 public int insert(User u) { 18 return this.getSqlSession().insert("cn.itcast.mybatis.domain.User.create", u); 19 } 20 21 //列表 22 public List<User> list() { 23 return this.getSqlSession().selectList("cn.itcast.mybatis.domain.User.listAll"); 24 } 25 26 //修改 27 public int update(User u) { 28 return this.getSqlSession().update("cn.itcast.mybatis.domain.User.update",u); 29 } 30 31 //获取对象 32 public User get(String id) { 33 return (User) this.getSqlSession().selectOne("cn.itcast.mybatis.domain.User.get", id); 34 } 35 36 }
4.service层
1 package cn.itcast.mybatis.service; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.domain.User; 6 7 8 public interface IUserService { 9 public List<User> list(); 10 public User get(String id); 11 public int insert(User u); 12 public int update(User u); 13 public int deleteById(String id); 14 }
serviceImpl
1 package cn.itcast.mybatis.service; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.dao.IUserDao; 6 import cn.itcast.mybatis.domain.User; 7 8 public class UserServiceImpl implements IUserService { 9 10 private IUserDao userDao; 11 public void setUserDao(IUserDao userDao) { 12 this.userDao = userDao; 13 } 14 15 public int deleteById(String id) { 16 int i = userDao.deleteById(id); 17 //测试事务 18 int j = 1/0; 19 return i; 20 } 21 22 public User get(String id) { 23 return userDao.get(id); 24 } 25 26 public int insert(User u) { 27 return userDao.insert(u); 28 } 29 30 public List<User> list() { 31 return userDao.list(); 32 } 33 34 public int update(User u) { 35 return userDao.update(u); 36 } 37 38 }
5.action层
1 package cn.itcast.mybatis.action; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.domain.User; 6 import cn.itcast.mybatis.service.IUserService; 7 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.ActionSupport; 10 import com.opensymphony.xwork2.ModelDriven; 11 12 public class UserAction extends ActionSupport implements ModelDriven<User> { 13 private User model = new User(); 14 public User getModel() { 15 return model; 16 } 17 18 private IUserService userService; 19 public IUserService getUserService() { 20 return userService; 21 } 22 23 public void setUserService(IUserService userService) { 24 this.userService = userService; 25 } 26 27 public String list(){ 28 List<User> dataList = userService.list(); 29 ActionContext.getContext().put("dataList", dataList); 30 31 return "plist"; 32 } 33 34 public String tocreate(){ 35 36 return "pcreate"; 37 } 38 39 public String toupdate(){ 40 User u = userService.get(model.getId()); 41 ActionContext.getContext().getValueStack().push(u); 42 43 return "pupdate"; 44 } 45 46 public String toview(){ 47 User u = userService.get(model.getId()); 48 ActionContext.getContext().getValueStack().push(u); 49 50 return "pview"; 51 } 52 53 public String createSave(){ 54 userService.insert(model); 55 56 return "actlist"; 57 } 58 59 public String updateSave(){ 60 userService.update(model); 61 62 return "actlist"; 63 } 64 65 public String delete(){ 66 userService.deleteById(model.getId()); 67 68 return "actlist"; 69 } 70 }
6.核心 配置文件
sqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <typeAliases> 8 <typeAlias type="cn.itcast.mybatis.domain.User" alias="aliasesUser"></typeAlias> 9 </typeAliases> 10 <environments default="development"> 11 12 <environment id="development"> 13 <transactionManager type="JDBC"/> 14 <dataSource type="POOLED"> 15 <property name="driver" value="com.mysql.jdbc.Driver"/> 16 <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> 17 <property name="username" value="root"/> 18 <property name="password" value="friends"/> 19 </dataSource> 20 </environment> 21 <!-- 产品 可用于多个配置的 --> 22 <environment id="product"> 23 <transactionManager type="JDBC"/> 24 <dataSource type="POOLED"> 25 <property name="driver" value="com.mysql.jdbc.Driver"/> 26 <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> 27 <property name="username" value="root"/> 28 <property name="password" value="friends"/> 29 </dataSource> 30 </environment> 31 </environments> 32 <mappers> 33 <mapper resource="cn/itcast/mybatis/domain/MyUser.xml"/> 34 </mappers> 35 </configuration>
spring-beans.xml
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/context 10 http://www.springframework.org/schema/context/spring-context-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/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 15 16 <!-- 数据源 --> 17 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 18 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 19 <property name="url" value="jdbc:mysql://localhost/mybatisdb?characterEncoding=utf-8"></property> 20 <property name="username" value="root"></property> 21 <property name="password" value="friends"></property> 22 </bean> 23 24 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 25 <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> 26 <property name="dataSource" ref="dataSource"></property> 27 28 </bean> 29 30 <!-- 事务相关控制 --> 31 <bean name="transactionManager" 32 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 33 <property name="dataSource" ref="dataSource"/> 34 </bean> 35 36 <!-- 通知 --> 37 <tx:advice id="tx" 38 transaction-manager="transactionManager"> 39 <tx:attributes> 40 <tx:method name="delete*" propagation="REQUIRED" /> 41 <tx:method name="insert*" propagation="REQUIRED" /> 42 <tx:method name="update*" propagation="REQUIRED" /> 43 <tx:method name="find*" read-only="true" /> 44 <tx:method name="get*" read-only="true" /> 45 <tx:method name="select*" read-only="true" /> 46 </tx:attributes> 47 </tx:advice> 48 49 <aop:config> 50 <aop:pointcut id="pc" expression="execution(* cn.itcast.mybatis.service.*.*(..))" /> 51 <!--把事务控制在Service层--> 52 <aop:advisor pointcut-ref="pc" advice-ref="tx" /> 53 </aop:config> 54 55 <bean id="userDao" class="cn.itcast.mybatis.dao.UserDaoImpl"> 56 <property name="sqlSessionFactory" ref="sessionFactory"></property> 57 </bean> 58 59 <bean id="userService" class="cn.itcast.mybatis.service.UserServiceImpl"> 60 <property name="userDao" ref="userDao"></property> 61 </bean> 62 63 64 <bean id="userAction" class="cn.itcast.mybatis.action.UserAction" scope="prototype"> 65 <property name="userService" ref="userService"> 66 <!-- 一样 <ref bean="userService"/> --> 67 </property> 68 </bean> 69 </beans>
struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.ui.theme" value="simple"></constant> 7 <constant name="struts.devMode" value="true"></constant> 8 <constant name="struts.objectFactory" value="spring"></constant> 9 10 <package name="user" namespace="/user" extends="struts-default"> 11 <action name="userAction_*" class="userAction" method="{1}"> 12 <result name="pview">/WEB-INF/jsps/user/jUserView.jsp</result> 13 <result name="plist">/WEB-INF/jsps/user/jUserList.jsp</result> 14 <result name="pcreate">/WEB-INF/jsps/user/jUserCreate.jsp</result> 15 <result name="pupdate">/WEB-INF/jsps/user/jUserUpdate.jsp</result> 16 <result name="actlist" type="redirectAction">/user/userAction_list</result> 17 </action> 18 </package> 19 </struts>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" id="WebApp_ID" version="3.0"> 3 <display-name>MySSI</display-name> 4 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:spring-beans.xml</param-value> 8 </context-param> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 13 <filter> 14 <filter-name>struts2</filter-name> 15 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 16 </filter> 17 <filter-mapping> 18 <filter-name>struts2</filter-name> 19 <url-pattern>/*</url-pattern> 20 </filter-mapping> 21 22 <welcome-file-list> 23 <welcome-file>index.jsp</welcome-file> 24 </welcome-file-list> 25 </web-app>
显示层
jUserCreate.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <body> 5 <form action="${pageContext.request.contextPath}/user/userAction_createSave" method="post"> 6 <s:hidden name="id"/> 7 8 <h2>新增用户</h2> 9 10 <table border="1"> 11 <tr> 12 <td>姓名:</td><td><s:textfield name="name"/></td> 13 </tr> 14 <tr> 15 <td>年龄:</td><td><s:textfield name="age"/></td> 16 </tr> 17 <tr> 18 <td>地址:</td><td><s:textarea name="address"/></td> 19 </tr> 20 <td><input type="submit" name="btnOK"/></td> 21 </tr> 22 </table> 23 </form> 24 </body> 25 </html>
jUserList.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <h2>用户列表</h2> 4 5 <div style="padding:10px;"> 6 <a href="${pageContext.request.contextPath}/user/userAction_tocreate">新增</a> 7 </div> 8 9 <table border="1"> 10 <tr align="center"> 11 <td>序号</td> 12 <td>姓名</td> 13 <td>年龄</td> 14 <td>地址</td> 15 <td>操作</td> 16 </tr> 17 <s:iterator value="#dataList" var="u" status="line"> 18 <tr> 19 <td align="center"><s:property value="#line.index+1"/></td> 20 <td><s:property value="name"/></td> 21 <td align="center"><s:property value="age"/></td> 22 <td><s:property value="address"/></td> 23 <td> 24 <a href="${pageContext.request.contextPath}/user/userAction_toview?id=${u.id}">查看</a> 25 <a href="${pageContext.request.contextPath}/user/userAction_toupdate?id=${u.id}">修改</a> 26 <a href="${pageContext.request.contextPath}/user/userAction_delete?id=${u.id}">删除</a> 27 </td> 28 </tr> 29 </s:iterator> 30 31 </table>
jUserUpdate.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <body> 5 <form action="${pageContext.request.contextPath}/user/userAction_updateSave" method="post"> 6 <s:hidden name="id"/> 7 8 <h2>修改用户</h2> 9 10 <table border="1"> 11 <tr> 12 <td>姓名:</td><td><s:textfield name="name"/></td> 13 </tr> 14 <tr> 15 <td>年龄:</td><td><s:textfield name="age"/></td> 16 </tr> 17 <tr> 18 <td>地址:</td><td><s:textarea name="address"/></td> 19 </tr> 20 <td><input type="submit" name="btnOK"/></td> 21 </tr> 22 </table> 23 </form> 24 </body> 25 </html>
jUserView.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <body> 5 <form action="${pageContext.request.contextPath}/user/userAction_createSave" method="post"> 6 <s:hidden name="id"/> 7 8 <h2>浏览用户</h2> 9 10 <div style="padding:10px;"> 11 <a href="#" onclick="history.go(-1);">返回</a> 12 </div> 13 14 <table border="1"> 15 <tr> 16 <td>姓名:</td><td><s:property value="name"/></td> 17 </tr> 18 <tr> 19 <td>年龄:</td><td><s:property value="age"/></td> 20 </tr> 21 <tr> 22 <td>地址:</td><td><s:property value="address"/></td> 23 </table> 24 </form> 25 </body> 26 </html>