转自 http://zmx.iteye.com/blog/599509

构造JavaBean如下(get,set方法省略):

Java代码  收藏代码
  1. public class Student {  
  2.     private Integer stu_id;  
  3.   
  4.     private String stu_name;  
  5.   
  6.     private Integer stu_age;  
  7.   
  8.     private float stu_score;  
  9.   
  10.     private Date stu_birth;  
  11. }  

 在JavaBean包里新建一个Student.xml,内容如下:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"  
  3.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
  4. <sqlMap namespace="student">  
  5.   
  6.     <typeAlias alias="Student" type="com.mengya.bean.Student" />  
  7.   
  8.     <insert id="save" parameterClass="Student">  
  9.         insert into student  
  10.         values(#stu_id#,#stu_name#,#stu_age#,#stu_score#,#stu_birth#)  
  11.     </insert>  
  12.   
  13.     <delete id="delStuByID" parameterClass="int">  
  14.         delete from student where stu_id = #stu_id#  
  15.     </delete>  
  16.   
  17.     <update id="update" parameterClass="Student">  
  18.         update student set  
  19.         stu_name=#stu_name#,stu_age=#stu_age#,stu_score=#stu_score#,stu_birth=#stu_birth#  
  20.         where stu_id=#stu_id#  
  21.     </update>  
  22.   
  23.     <select id="queryAll" resultClass="Student">  
  24.         select * from student  
  25.     </select>  
  26.   
  27.     <select id="queryById" resultClass="Student" parameterClass="int">  
  28.         select * from student where stu_id = #stu_id#  
  29.     </select>  
  30.   
  31. </sqlMap>  

 构造dao接口如下:

Java代码  收藏代码
  1. public interface StudentDao {  
  2.     public void save(Student stu);  
  3.   
  4.     public void delete(int stu_id);  
  5.   
  6.     public void update(Student stu);  
  7.   
  8.     public Student queryByPK(int stu_id);  
  9.   
  10.     public List<Student> queryAll();  
  11. }  

 对dao的实现:继承Spring提供的org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

Java代码  收藏代码
  1. public class StudentDaoImple extends SqlMapClientDaoSupport implements  
  2.         StudentDao {  
  3.   
  4.     public void delete(int stu_id) {  
  5.         getSqlMapClientTemplate().delete("delStuByID", stu_id);  
  6.     }  
  7.   
  8.     public List<Student> queryAll() {  
  9.         return getSqlMapClientTemplate().queryForList("queryAll");  
  10.     }  
  11.   
  12.     public Student queryByPK(int stu_id) {  
  13.         return (Student) getSqlMapClientTemplate().queryForObject("queryById",  
  14.                 stu_id);  
  15.     }  
  16.   
  17.     public void save(Student stu) {  
  18.         getSqlMapClientTemplate().insert("save", stu);  
  19.     }  
  20.   
  21.     public void update(Student stu) {  
  22.         getSqlMapClientTemplate().update("update", stu);  
  23.     }  
  24.   
  25. }  

 构造service接口:

Java代码  收藏代码
  1. public interface StudentService {  
  2.     public void save(Student stu);  
  3.   
  4.     public void delete(int stu_id);  
  5.   
  6.     public void update(Student stu);  
  7.   
  8.     public Student queryByPK(int stu_id);  
  9.   
  10.     public List<Student> queryAll();  
  11. }  

 对service的实现:

Java代码  收藏代码
  1. public class StudentServiceImple implements StudentService {  
  2.   
  3.     private StudentDao studao;  
  4.   
  5.     public void setStudao(StudentDao studao) {  
  6.         this.studao = studao;  
  7.     }  
  8.   
  9.     public void delete(int stu_id) {  
  10.         this.studao.delete(stu_id);  
  11.     }  
  12.   
  13.     public List<Student> queryAll() {  
  14.         return studao.queryAll();  
  15.     }  
  16.   
  17.     public Student queryByPK(int stu_id) {  
  18.         return studao.queryByPK(stu_id);  
  19.     }  
  20.   
  21.     public void save(Student stu) {  
  22.         studao.save(stu);  
  23.     }  
  24.   
  25.     public void update(Student stu) {  
  26.         studao.update(stu);  
  27.     }  
  28.   
  29. }  

 iBATIS的sqlMapConfig.xml配置如下:

Xml代码  收藏代码
  1. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
  2.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  3. <sqlMapConfig>  
  4.     <sqlMap resource="com/mengya/bean/Student.xml" />  
  5. </sqlMapConfig>  

 Spring的applicationContext.xml配置如下:

Xml代码  收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/context             
  9.             http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  12.   
  13.     <bean id="dataSource"  
  14.         class="org.apache.commons.dbcp.BasicDataSource">  
  15.         <property name="driverClassName"  
  16.             value="com.mysql.jdbc.Driver">  
  17.         </property>  
  18.         <property name="url" value="jdbc:mysql://localhost:3306/mp"></property>  
  19.         <property name="username" value="root"></property>  
  20.         <property name="password" value="123"></property>  
  21.         <property name="maxActive" value="10"></property>  
  22.         <property name="minIdle" value="2"></property>  
  23.         <property name="maxWait" value="300"></property>  
  24.     </bean>  
  25.   
  26.     <bean id="sqlMapClient"  
  27.         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  28.         <property name="dataSource" ref="dataSource"></property>  
  29.         <property name="configLocation"  
  30.             value="classpath:sqlMapConfig.xml">  
  31.         </property>  
  32.     </bean>  
  33.   
  34.     <bean id="transactionManager"  
  35.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  36.         <property name="dataSource" ref="dataSource"></property>  
  37.     </bean>  
  38.   
  39.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  40.         <tx:attributes>  
  41.             <tx:method name="save" propagation="REQUIRED" />  
  42.             <tx:method name="delete" propagation="REQUIRED" />  
  43.             <tx:method name="update" propagation="REQUIRED" />  
  44.             <tx:method name="*" read-only="true" />  
  45.         </tx:attributes>  
  46.     </tx:advice>  
  47.   
  48.     <aop:config>  
  49.         <aop:pointcut id="allMethod"  
  50.             expression="execution(* com.mengya.service.imple.*.*(..))" />  
  51.         <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />  
  52.     </aop:config>  
  53.   
  54.     <bean id="studao" class="com.mengya.dao.imple.StudentDaoImple">  
  55.         <property name="sqlMapClient" ref="sqlMapClient"></property>  
  56.     </bean>  
  57.   
  58.     <bean id="stuMght"  
  59.         class="com.mengya.service.imple.StudentServiceImple">  
  60.         <property name="studao" ref="studao"></property>  
  61.     </bean>  
  62.   
  63.     <bean id="studentAction" class="com.mengya.web.StudentAction"  
  64.         scope="prototype">  
  65.         <property name="stuMght" ref="stuMght"></property>  
  66.     </bean>  
  67.   
  68. </beans>  

 Struts2的action如下:

Java代码  收藏代码
  1. public class StudentAction extends ActionSupport {  
  2.     private StudentService stuMght;  
  3.   
  4.     private Student stu;  
  5.   
  6.     private List<Student> stuList;  
  7.   
  8.     public StudentService getStuMght() {  
  9.         return stuMght;  
  10.     }  
  11.   
  12.     public void setStuMght(StudentService stuMght) {  
  13.         this.stuMght = stuMght;  
  14.     }  
  15.   
  16.     public String list() {  
  17.         this.stuList = stuMght.queryAll();  
  18.         return "list";  
  19.     }  
  20.   
  21.     public Student getStu() {  
  22.         return stu;  
  23.     }  
  24.   
  25.     public void setStu(Student stu) {  
  26.         this.stu = stu;  
  27.     }  
  28.   
  29.     public List<Student> getStuList() {  
  30.         return stuList;  
  31.     }  
  32.   
  33.     public void setStuList(List<Student> stuList) {  
  34.         this.stuList = stuList;  
  35.     }  
  36.   
  37. }  

struts.xml的配置如下:

Xml代码  收藏代码
  1. <struts>  
  2.     <constant name="struts.i18n.encoding" value="gbk" />  
  3.     <constant name="struts.objectFactory" value="spring" />  
  4.     <package name="mengya" extends="struts-default">  
  5.         <action name="student_*" class="studentAction" method="{1}">  
  6.             <result name="list">/list.jsp</result>  
  7.         </action>  
  8.     </package>  
  9. </struts>  

 web.xml配置如下:

Xml代码  收藏代码
  1. <filter>  
  2.         <filter-name>struts2</filter-name>  
  3.         <filter-class>  
  4.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  5.         </filter-class>  
  6.     </filter>  
  7.   
  8.     <filter-mapping>  
  9.         <filter-name>struts2</filter-name>  
  10.         <url-pattern>/*</url-pattern>  
  11.     </filter-mapping>  
  12.   
  13.     <filter>  
  14.         <filter-name>springEncoding</filter-name>  
  15.         <filter-class>  
  16.             org.springframework.web.filter.CharacterEncodingFilter  
  17.         </filter-class>  
  18.         <init-param>  
  19.             <param-name>encoding</param-name>  
  20.             <param-value>gbk</param-value>  
  21.         </init-param>  
  22.     </filter>  
  23.     <filter-mapping>  
  24.         <filter-name>springEncoding</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.   
  28.     <context-param>  
  29.         <param-name>contextConfigLocation</param-name>  
  30.         <param-value>classpath:applicationContext.xml</param-value>  
  31.     </context-param>  
  32.     <listener>  
  33.         <listener-class>  
  34.             org.springframework.web.context.ContextLoaderListener  
  35.         </listener-class>  
  36.     </listener>  
posted on 2012-06-05 15:35  饭菜糖衣  阅读(229)  评论(0编辑  收藏  举报