[转]Spring3.05简单集成MyBatis3.03

原文: http://blog.csdn.net/sustbeckham/archive/2010/12/17/6082677.aspx

mybatis一直没有发布release版本。所以spring也坐着看。但是spring还是必须用啊。

1. Pojo & mapper配置

 

  1. package cn.java.forum.domain;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class People implements Serializable{  
  7.    
  8.     private static final long serialVersionUID = 1L;  
  9.   
  10.     private int id;  
  11.       
  12.     private String username;  
  13.       
  14.     private String password;  
  15.       
  16.     private String realName;  
  17.       
  18.     private Date registerTime;  
  19.   
  20.     @Override  
  21.     public String toString() {  
  22.         return "< id:"+id+", username:"+username+", password:"+password+", realName:"+realName  
  23.                  +", registerTime:"+registerTime.toLocaleString()+" >";  
  24.     }  
  25.       
  26.     public int getId() {  
  27.         return id;  
  28.     }  
  29.   
  30.     public void setId(int id) {  
  31.         this.id = id;  
  32.     }  
  33.   
  34.     public String getUsername() {  
  35.         return username;  
  36.     }  
  37.   
  38.     public void setUsername(String username) {  
  39.         this.username = username;  
  40.     }  
  41.   
  42.     public String getPassword() {  
  43.         return password;  
  44.     }  
  45.   
  46.     public void setPassword(String password) {  
  47.         this.password = password;  
  48.     }  
  49.   
  50.     public String getRealName() {  
  51.         return realName;  
  52.     }  
  53.   
  54.     public void setRealName(String realName) {  
  55.         this.realName = realName;  
  56.     }  
  57.   
  58.     public Date getRegisterTime() {  
  59.         return registerTime;  
  60.     }  
  61.   
  62.     public void setRegisterTime(Date registerTime) {  
  63.         this.registerTime = registerTime;  
  64.     }  
  65. }  

 

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE mapper        
  4.     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        
  5.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  6.   
  7. <mapper namespace="cn.java.forum.domain.mapper.PeopleMapper">  
  8.     <!--  
  9.     <cache eviction="FIFO" flushInterval="30000" readOnly="true" size="512"></cache> 
  10.     -->  
  11.     
  12.     <select id="allPeople" resultType="People">  
  13.         select * from People  
  14.     </select>  
  15.   
  16. </mapper>  

 

2. mapper接口

  1. package cn.java.forum.domain.mapper;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.java.forum.domain.People;  
  6.   
  7. public interface PeopleMapper {  
  8.      List<People> allPeople();  
  9. }  

 

3.service接口

  1. package cn.java.forum.domain.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.java.forum.domain.People;  
  6.   
  7. public interface PeopleService {  
  8.     List<People> allPeople();  
  9. }  

 

4. service实现,mapper经过注入。

  1. package cn.java.forum.domain.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.java.forum.domain.People;  
  6. import cn.java.forum.domain.mapper.PeopleMapper;  
  7. import cn.java.forum.domain.service.PeopleService;  
  8.   
  9. public class PeopleServiceBean implements PeopleService{  
  10.   
  11.     /* 需要注入 */  
  12.     private PeopleMapper mapper;  
  13.   
  14.     public PeopleMapper getMapper() {  
  15.         return mapper;  
  16.     }  
  17.   
  18.     public void setMapper(PeopleMapper mapper) {  
  19.         this.mapper = mapper;  
  20.     }  
  21.   
  22.     @Override  
  23.     public List<People> allPeople() {  
  24.         return mapper.allPeople();  
  25.     }  
  26.   
  27. }  

 

基本的代码完成之后。就是配置文件了。

---------------------------------------------------华丽的分割线---------------------------------------------------

1.完整的spring主配置文件,包含了事务,mapper,数据源等的配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.        
  10.     <!-- ============================== 数据库配置 ==================================== -->  
  11.     <!-- 数据源配置 -->  
  12.     <bean name="dataSource"  
  13.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  14.         <property name="driverClassName">  
  15.             <value>com.mysql.jdbc.Driver</value>  
  16.         </property>  
  17.         <property name="url">  
  18.             <value>jdbc:mysql://localhost:3306/forum</value>  
  19.         </property>  
  20.         <property name="username">  
  21.             <value>root</value>  
  22.         </property>  
  23.         <property name="password">  
  24.             <value>123456</value>  
  25.         </property>  
  26.     </bean>  
  27.       
  28.     <!-- ================================ MyBatis SqlSession配置 ========================================= -->  
  29.     <!-- 使用SqlSessionFactoryBean工厂产生SqlSession对象,方便后期注入Dao -->  
  30.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.         <property name="configLocation" value="classpath:Configuration.xml"></property>  
  33.     </bean>  
  34.    
  35.     <!-- ================================= mapper ============================================= -->  
  36.     <!-- 人员mapper -->  
  37.     <bean id="peopleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  38.         <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
  39.         <!-- mapper的位置 -->  
  40.         <property name="mapperInterface" value="cn.java.forum.domain.mapper.PeopleMapper"/>  
  41.     </bean>  
  42.       
  43.     <!-- ================================= 事务控制相关 ============================================= -->  
  44.     <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
  45.         <property name="dataSource" ref="dataSource"></property>  
  46.     </bean>     
  47.       
  48.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  49.         <tx:attributes>  
  50.             <tx:method name="delete*" propagation="REQUIRED" read-only="false"   
  51.                        rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>  
  52.             <tx:method name="insert*" propagation="REQUIRED" read-only="false"   
  53.                        rollback-for="java.lang.RuntimeException" />  
  54.             <tx:method name="update*" propagation="REQUIRED" read-only="false"   
  55.                        rollback-for="java.lang.Exception" />  
  56.               
  57.             <tx:method name="find*" propagation="SUPPORTS"/>  
  58.             <tx:method name="get*" propagation="SUPPORTS"/>  
  59.             <tx:method name="select*" propagation="SUPPORTS"/>  
  60.         </tx:attributes>  
  61.     </tx:advice>   
  62.       
  63.     <!-- 引入service层的spring配置 -->  
  64.     <import resource="applicationContext-service.xml"/>   
  65. </beans>  

 

2.MyBatis的配置文件,暂时只配置了 别名 和 mapper

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE configuration        
  4.     PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"        
  5.     "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  6.       
  7. <configuration>  
  8.    
  9.     <typeAliases>  
  10.          <typeAlias type="cn.java.forum.domain.Grade" alias="Grade"/>  
  11.          <typeAlias type="cn.java.forum.domain.People" alias="People"/>  
  12.          <typeAlias type="cn.java.forum.domain.Response" alias="Response"/>  
  13.          <typeAlias type="cn.java.forum.domain.Topic" alias="Topic"/>  
  14.     </typeAliases>  
  15.       
  16.     <!--  spring配置之后 这些就可以省略了  
  17.     <environments default="development">  
  18.         <environment id="development">  
  19.             <transactionManager type="JDBC">  
  20.             </transactionManager>  
  21.               
  22.             <dataSource type="POOLED">  
  23.                 <property name="driver"  
  24.                     value="com.mysql.jdbc.Driver" />  
  25.                 <property name="url"  
  26.                     value="jdbc:mysql://localhost:3306/forum" />  
  27.                 <property name="username" value="root" />  
  28.                 <property name="password" value="123456" />  
  29.             </dataSource>  
  30.         </environment>  
  31.     </environments> -->  
  32.       
  33.     <mappers>  
  34.         <mapper resource="cn/java/forum/domain/config/Grade.xml"/>  
  35.         <mapper resource="cn/java/forum/domain/config/People.xml"/>  
  36.         <mapper resource="cn/java/forum/domain/config/Response.xml"/>  
  37.         <mapper resource="cn/java/forum/domain/config/Topic.xml"/>  
  38.     </mappers>  
  39.   
  40. </configuration>  

 

3. service层的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.        
  10.     <bean id="peopleService" class="cn.java.forum.domain.service.impl.PeopleServiceBean">  
  11.          <property name="mapper">  
  12.              <ref bean="peopleMapper"/>  
  13.          </property>  
  14.     </bean>  
  15. </beans>  

 

4.最后,是启动spring的配置 WEB.XML

  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         classpath:applicationContext.xml  
  5.     </param-value>  
  6. </context-param>  
  7. <listener>  
  8.     <listener-class>  
  9.         org.springframework.web.context.ContextLoaderListener  
  10.     </listener-class>  
  11. </listener>  

 

ok  写一个servlet来测试(其实类也可以的。)。

  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.springframework.context.ApplicationContext;  
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  13.   
  14. import cn.java.forum.domain.service.PeopleService;  
  15.   
  16. public class TestServlet extends HttpServlet {  
  17.   
  18.     private static final long serialVersionUID = 1L;  
  19.   
  20.     public TestServlet() {  
  21.         super();  
  22.     }  
  23.   
  24.     public void destroy() {  
  25.         super.destroy();  
  26.     }  
  27.   
  28.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  29.             throws ServletException, IOException {  
  30.         doPost(request, response);  
  31.     }  
  32.   
  33.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  34.             throws ServletException, IOException {  
  35.   
  36.         response.setContentType("text/html");  
  37.         PrintWriter out = response.getWriter();  
  38.            
  39.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  40.         PeopleService peopleService = (PeopleService) ctx.getBean("peopleService");  
  41.         System.out.println("the list:"+peopleService.allPeople());  
  42.         out.flush();  
  43.         out.close();  
  44.     }  
  45.   
  46.     public void init() throws ServletException {  
  47.     }  
  48.   
  49. }  

 

控制台显示 ....................

最后一行。

OK  大功告成。

posted on 2011-06-18 11:42 ... 阅读(432) 评论(0)  编辑  收藏 所属分类: Struts Hibernate Spring MyBatis


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

导航

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜