本文主要介绍了如何将mybatis和spring整合在一起的几种方式。



环境:mybatis3.2.4+ spring3.1.1,使用dbcp作为数据库连接池。



  • 1.编写数据访问接口(UserMapper.java)



package com.eshore.common.mybatis.test.mapper; 
  
 
  
 
   
 import java.util.List; 
   

 import org.apache.ibatis.annotations.Insert; 
   
 import org.apache.ibatis.annotations.Param; 
   
 import org.apache.ibatis.annotations.Select; 
   
 import org.apache.ibatis.annotations.SelectKey; 
   

 import com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   

 @MyBatisAnnotation 
   
 public interface UserMapper { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      @Select("SELECT * FROM tb_user WHERE user_id = #{userId}") 
   
      public UserBean getUser(@Param("userId") int userId); 
   

      @Insert("INSERT INTO tb_user(user_id,user_name) VALUES (#{userId},#{userName})") 
   
      @SelectKey(keyProperty = "userId", before = true, resultType = Integer.class, statement = { " select SQ_BM_MSG_QUE.nextval AS userId from dual" }) 
   
      public void insert(UserBean userBean); 
   
 }




此处为了演示,使用了Annotation方式,包括接口的声明部分。





  • 2.编写数据访问接口映射文件(UserMapper.xml)






<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    mapper  
    PUBLIC  
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"  
    >
 
   
< 
    mapper  
    namespace 
    = 
    "com.eshore.common.mybatis.test.mapper.UserMapper"  
    >
 
   
         
    < 
    cache  
    />
 
   
         
    <!--列名以下划线分割, 自动映射成驼峰格式 -->
 
   
         
    < 
    select  
    id 
    =  
    "queryUsers"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    < 
    select  
    id 
    =  
    "User_select"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    <!-- -->
 
   
         
    < 
    select  
    id 
    =  
    "selectUser"  
    parameterType  
    = 
    "Integer"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
              where t.user_id = #{userId}
 
   
         
    </ 
    select 
    >
 
   

 
   
         
    <!-- 新增记录 -->
 
   
         
    < 
    insert  
    id 
    =  
    "insertUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
                
    < 
    selectKey  
    resultType 
    = 
    "int"  
    keyProperty 
    =  
    "userId"  
    order  
    = 
    "BEFORE" 
    >  
      
 
   
             
    <![CDATA[ 
    select SQ_BM_MSG_QUE.nextval as ID from dual 
    ]]>
 
   
                
    </ 
    selectKey 
    >
 
   
       
    <![CDATA[ 
    insert into tb_user(user_id,user_name,password,address)
 
   
                      values(#{userId},#{userName},#{password},#{address})  
    ]]>
 
   
         
    </ 
    insert 
    >
 
   

 
   
         
    <!-- 修改记录 -->
 
   
         
    < 
    update  
    id 
    =  
    "updateUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
              update tb_user
 
   
                
    < 
    trim  
    prefix 
    =  
    "SET"  
    suffixOverrides  
    = 
    "," 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName !=null" 
    > 
    user_name = #{userName},  
    </ 
    if 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "address !=null" 
    > 
    address = #{address},  
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
              where user_id =#{userId}
 
   
         
    </ 
    update 
    >
 
   

 
   
         
    <!-- 删除记录 -->
 
   
         
    < 
    delete  
    id 
    =  
    "deleteUser"  
    parameterType  
    = 
    "int" 
    >
 
   
              delete from tb_user where
 
   
              user_id = #{userId}
 
   
         
    </ 
    delete 
    >
 
   

 
   

 
   
         
    < 
    select  
    id 
    =  
    "proHello"  
    statementType  
    = 
    "CALLABLE"  
    useCache 
    =  
    "false" 
    > 
     
 
   
         
    <![CDATA[ 
     
 
   
           {call pro_hello (#{p_user_name,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})} 
 
   
         
    ]]>
 
   
         
    </ 
    select 
    >
 
   
</ 
    mapper 
    >
 
  

 
  
• 3.编写mybatis配置文件(MyBatis-Configuration.xml)
 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    configuration  
    PUBLIC
 
   
         
    "-//mybatis.org//DTD Config 3.0//EN"
 
   
         
    "http://mybatis.org/dtd/mybatis-3-config.dtd"  
    >
 
   
< 
    configuration 
    >
 
   
         
    < 
    settings 
    >
 
   
                
    < 
    setting  
    name 
    =  
    "jdbcTypeForNull"  
    value  
    = 
    "OTHER"  
    />
 
   
                
    < 
    setting  
    name 
    =  
    "mapUnderscoreToCamelCase"  
    value 
    = 
    "true"  
    /> 
    <!-- 
    驼峰形式 
    -->
 
   
         
    </ 
    settings 
    >
 
   

 
   
         
    < 
    typeAliases 
    >
 
   
                
    < 
    typeAlias  
    alias 
    =  
    "userBean"
 
   
                       
    type 
    =  
    "com.eshore.common.mybatis.test.model.UserBean"  
    />
 
   
         
    </ 
    typeAliases 
    >
 
   
         
    < 
    plugins 
    >
 
   
                
    < 
    plugin  
    interceptor 
    = 
    "com.poson.daopub.database.plugin.PagePlugin"  
    >
 
   
                       
    < 
    property  
    name 
    =  
    "dialect"  
    value  
    = 
    "oracle"  
    />
 
   
                       
    < 
    property  
    name 
    =  
    "pageSqlId"  
    value  
    = 
    ".*select.*"  
    />
 
   
                
    </ 
    plugin 
    >
 
   
         
    </ 
    plugins 
    >
 
   

 
   
         
    <!-- 配置mappers -->
 
   
         
    < 
    mappers 
    >
 
   
                
    < 
    mapper  
    resource 
    = 
    "com/eshore/common/mybatis/test/mapper/UserMapper.xml"  
    />
 
   
         
    </ 
    mappers 
    >
 
   

 
   
</ 
    configuration 
    >  
       
 
   

 
  
• 4.编写服务层接口(IUserService .java)
 
  

    package com.eshore.common.mybatis.test.service; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   

 public interface IUserService { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      public UserBean getUser(int userId); 
   

      public void insert(UserBean userBean); 
   

      public void testProcedure(String prodId,Map<String, String> param); 
   

 } 
    
  
• 5.编写服务层实现代码
• 5.1SqlSessinTemplate方式
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.jdbc.SQL; 
   
 import org.mybatis.spring.SqlSessionTemplate; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl2 implements IUserService { 
   

      public SqlSessionTemplate sqlSessionTemplate; 
   

      @Override 
   
      public void deleteUser(int userId) { 
   
           // TODO Auto-generated method stub 
   
           sqlSessionTemplate.delete( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.deleteUser", 
   
                     userId); 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return sqlSessionTemplate.selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
    
  
 
   
 
  

        ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.2 SqlSessionDaoSupport
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.mybatis.spring.support.SqlSessionDaoSupport; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl3 extends SqlSessionDaoSupport implements 
   
           IUserService { 
   
      @Override 
   
      public void deleteUser(int userId) { 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return (UserBean) this.getSqlSession().selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
   
  

      ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.3注解方式
 
  
package  
     com.eshore.common.mybatis.test.service.impl;
 
    

 
    
import  
     java.util.List;
 
    
import  
     java.util.Map;
 
    

 
    
import  
     org.springframework.beans.factory.annotation.Autowired;
 
    
import  
     org.springframework.stereotype.Service;
 
    
import  
     org.springframework.transaction.annotation.Transactional;
 
    

 
    
import  
     com.eshore.common.mybatis.test.mapper.UserMapper;
 
    
import  
     com.eshore.common.mybatis.test.model.UserBean;
 
    

 
   
@Service 
    (  
    "userServiceImpl4" 
    )
 
   
public  
    class  
    UserServiceImpl4 {
 
   

 
   
      
    @Autowired
 
   
      
    private  
    UserMapper  
    mapper 
    ;
 
   

 
   
       
    @Transactional
 
   
      
    public  
    void  
    deleteUser(  
    int  
    userId) {
 
   
             
    mapper 
    .deleteUser(userId);
 
   
     }
 
   

 
   
      
    public  
    UserBean getUser(  
    int  
    userId) {
 
   
             
    return  
    (UserBean)  
    mapper 
    .getUser(userId);
 
   

 
   
     }
 
   
}
 
  
• 6.编写spring配置文件(applicationContext.xml)
 
  
<? 
   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:tx 
   =  
   "http://www.springframework.org/schema/tx"  
   xmlns:mvc 
   = 
   "http://www.springframework.org/schema/mvc"
 
  
         
   xsi:schemaLocation 
   = 
   "http://www.springframework.org/schema/beans 
 
  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 
  
     http://www.springframework.org/schema/context 
 
  
   http://www.springframework.org/schema/context/spring-context-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/mvc 
 
  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
   >
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "propertyConfigurer"
 
  
                
   class 
   =  
   "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "locations" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   classpath:conf/jdbc.properties 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "dataSource"  
   class  
   = 
   "org.apache.commons.dbcp.BasicDataSource"
 
  
                
   destroy-method 
   = 
   "close"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "driverClassName"  
   value  
   = 
   "${jdbc.driverClassName}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "url"  
   value  
   = 
   "${jdbc.url}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "username"  
   value  
   = 
   "${jdbc.username}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "password"  
   value  
   = 
   "${jdbc.password}"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
       
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "transactionManager"
 
  
                
   class 
   =  
   "org.springframework.jdbc.datasource.DataSourceTransactionManager"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
         
   </ 
   bean 
   >
 
  
         
   <!--
 
  
              product <bean id="dataSource"
 
  
              class="org.springframework.jndi.JndiObjectFactoryBean"> <property
 
  
              name="jndiName" value="${datasource.name}" /> </bean>
 
  
       -->
 
  
         
   < 
   bean  
   id 
   =  
   "transactionInterceptor"
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionInterceptor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionManager"  
   ref  
   = 
   "transactionManager"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "transactionAttributes"  
   >
 
  
                       
   < 
   props 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "writeLogToDBTable*" 
   >
 
  
                                  PROPAGATION_REQUIRES_NEW
 
  
                             
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "saveRequest"  
   > 
   PROPAGATION_REQUIRES_NEW  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "*" 
   > 
   PROPAGATION_REQUIRED  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "find*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "query*"  
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "get*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                       
   </ 
   props 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "beanNames"  
   value  
   = 
   "*ServiceImpl*"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "proxyTargetClass"  
   value  
   = 
   "true" 
   ></  
   property 
   >
 
  
                
   < 
   property  
   name 
   =  
   "interceptorNames" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   transactionInterceptor 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionInterceptor"  
   ref 
   = 
   "transactionInterceptor"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   <!-- 开启注解配置 -->
 
  
         
   < 
   context:annotation-config  
   />
 
  

 
  
         
   <!--
 
  
              扫描service层 <context:exclude-filter type="regex"
 
  
              expression="com.poson.*.*.model"/>
 
  
       -->
 
  
         
   < 
   context:component-scan  
   base-package 
   = 
   "com.eshore.common.mybatis.test.service"  
   />
 
  

 
  
         
   <!-- 开启事务注解驱动 -->
 
  
         
   < 
   tx:annotation-driven  
   transaction-manager 
   = 
   "transactionManager"  
   />
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionFactory"  
   class 
   = 
   "org.mybatis.spring.SqlSessionFactoryBean"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "configLocation"  
   value 
   = 
   "classpath:conf/MyBatis-config.xml"  
   />
 
  
                
   <!--
 
  
                     <property name="typeAliasesPackage"
 
  
                     value="com.eshore.common.mybatis.test.model" />
 
  
              -->
 
  
                
   <!-- 多个以,分开 ;可以在MyBatis-config.xml中用typeAliases指定具体的别名-->
 
  
                
   <!--
 
  
                     <property name="transactionFactory"> <bean
 
  
                     class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"
 
  
                     /> </property>
 
  
              -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionTemplate"  
   class 
   = 
   "org.mybatis.spring.SqlSessionTemplate"  
   >
 
  
                
   < 
   constructor-arg  
   index 
   =  
   "0"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userMapper "  
   class  
   = 
   "org.mybatis.spring.mapper.MapperFactoryBean" 
   >
 
  
                
   < 
   property  
   name 
   =  
   "mapperInterface"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.UserMapper"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactory"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   <!--
 
  
              自动创建包下类名对应的bean Id为类名(第一个字符小写)
 
  
              两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。
 
  
              annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。
 
  
              markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。
 
  
              如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。
 
  
              即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。
 
  
       -->
 
  
         
   < 
   bean  
   class 
   =  
   "org.mybatis.spring.mapper.MapperScannerConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "basePackage"  
   value  
   = 
   "com.eshore.common.mybatis.test"  
   />
 
  

 
  
                
   < 
   property  
   name 
   =  
   "annotationClass"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation"  
   />
 
  
                
   <!--
 
  
                     <property name="markerInterface"
 
  
                     value="com.tiantian.mybatis.mapper.SuperMapper" />
 
  
              -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactoryBeanName"  
   value 
   = 
   "sqlSessionFactory"  
   />
 
  
                
   <!-- sqlSessionFactory已废弃 -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "userMapper"  
   ref  
   = 
   "userMapper" 
   ></  
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl2"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl2"  
   >
 
  
                
   <!--注入SqlSessionTemplate实例 -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
                
   <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
 
  
                
   <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl3"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl3"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
</ 
   beans 
   >  
    
 
  

 
  
• 7.测试代码(UserServiceTest.java)
 
  

    package com.eshore.common.mybatis.test; 
   

 import java.io.IOException; 
   
 import java.io.Reader; 
   
 import java.util.Date; 
   
 import java.util.HashMap; 
   
 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.io.Resources; 
   
 import org.apache.ibatis.logging.Log; 
   
 import org.apache.ibatis.logging.LogFactory; 
   
 import org.apache.ibatis.session.SqlSession; 
   
 import org.apache.ibatis.session.SqlSessionFactory; 
   
 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
   
 import org.junit.Before; 
   
 import org.junit.Test; 
   
 import org.springframework.context.ApplicationContext; 
   
 import org.springframework.context.support.ClassPathXmlApplicationContext; 
   

 import com.eshore.common.mybatis.test.mapper.UserMapper; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   
 import com.eshore.common.mybatis.test.service.impl.UserServiceImpl4; 
   

 public class UserTest { 
   
      private static final Log logger = LogFactory.getLog(UserTest.class); 
   
      private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder; 
   
      private static SqlSessionFactory sqlSessionFactory; 
   
      private static ApplicationContext context; 
   

      @Before 
   
      public void init() { 
   

           context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
   

      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   
      @Test 
   
      public void testService1() { 
   
           IUserService userService = (IUserService) context 
   
                     .getBean("userServiceImpl"); 
   
           UserBean user = new UserBean(); 
   
           user.setUserName("crm"); 
   
           try { 
   
                List<UserBean> list = (List<UserBean>) userService.queryUsers(user); 
   

                if (null != list && list.size() > 0) { 
   

                     System.out.println("userServiceImpl===" 
   
                               + ((UserBean) list.get(0)).toString()); 
   
                } 
   
           } catch (Exception e) { 
   
                e.printStackTrace(); 
   
           } 
   
           UserBean userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           userBean.setAddress("poson"); 
   
           userService.updateUser(userBean); 
   

           userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           UserBean newUser = new UserBean(); 
   
           newUser.setAddress("test"); 
   
           newUser.setUserName("crm"); 
   
           newUser.setPassword("aaaaa"); 
   
           newUser.setCreateTime(new Date().toLocaleString()); 
   
           userService.insertUser(newUser); 
   

           UserBean otherUser = new UserBean(); 
   
           otherUser.setUserName("ods"); 
   
           otherUser.setPassword("test"); 
   
           userService.insert(otherUser); 
   

           userService.deleteUser(37070013); 
   
      } 
   

      /** 
   
      * IOC sqlSessionTemplate 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService2() { 
   
           IUserService userService2 = (IUserService) context 
   
                     .getBean("userServiceImpl2"); 
   
           UserBean userBean = userService2.getUser(2); 
   
           System.out.println("userServiceImpl2====" + userBean.toString()); 
   
      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService3() { 
   
           UserBean userBean = new UserBean(); 
   
           IUserService userService3 = (IUserService) context 
   
                     .getBean("userServiceImpl3"); 
   
           userBean = userService3.getUser(1); 
   
           System.out.println("userServiceImpl3====" + userBean.toString()); 
   
      } 
   

      @Test 
   
      public void TestService4() { // 
   
           UserBean userBean = new UserBean(); 
   
           UserServiceImpl4 userService4 = (UserServiceImpl4) context 
   
                     .getBean("userServiceImpl4"); // // 
   
           userBean.setUserName("serviceImpl4"); 
   
           userService4.insert(userBean); 
   
           System.out.println("userServiceImpl4====" + userBean.toString()); 
   
      } 
   

 } 
   
  
 
 

         
 
本文主要介绍了如何将mybatis和spring整合在一起的几种方式。
 
  
环境:mybatis3.2.4+ spring3.1.1,使用dbcp作为数据库连接池。
 
  
• 1.编写数据访问接口(UserMapper.java)
 
  

    package com.eshore.common.mybatis.test.mapper; 
  
 
  
 
   
 import java.util.List; 
   

 import org.apache.ibatis.annotations.Insert; 
   
 import org.apache.ibatis.annotations.Param; 
   
 import org.apache.ibatis.annotations.Select; 
   
 import org.apache.ibatis.annotations.SelectKey; 
   

 import com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   

 @MyBatisAnnotation 
   
 public interface UserMapper { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      @Select("SELECT * FROM tb_user WHERE user_id = #{userId}") 
   
      public UserBean getUser(@Param("userId") int userId); 
   

      @Insert("INSERT INTO tb_user(user_id,user_name) VALUES (#{userId},#{userName})") 
   
      @SelectKey(keyProperty = "userId", before = true, resultType = Integer.class, statement = { " select SQ_BM_MSG_QUE.nextval AS userId from dual" }) 
   
      public void insert(UserBean userBean); 
   
 } 
    
  
 
   
 
  

    此处为了演示,使用了Annotation方式,包括接口的声明部分。 
  
 
  
 
   
 
  
• 2.编写数据访问接口映射文件(UserMapper.xml)
 
  

 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    mapper  
    PUBLIC  
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"  
    >
 
   
< 
    mapper  
    namespace 
    = 
    "com.eshore.common.mybatis.test.mapper.UserMapper"  
    >
 
   
         
    < 
    cache  
    />
 
   
         
    <!--列名以下划线分割, 自动映射成驼峰格式 -->
 
   
         
    < 
    select  
    id 
    =  
    "queryUsers"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    < 
    select  
    id 
    =  
    "User_select"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    <!-- -->
 
   
         
    < 
    select  
    id 
    =  
    "selectUser"  
    parameterType  
    = 
    "Integer"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
              where t.user_id = #{userId}
 
   
         
    </ 
    select 
    >
 
   

 
   
         
    <!-- 新增记录 -->
 
   
         
    < 
    insert  
    id 
    =  
    "insertUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
                
    < 
    selectKey  
    resultType 
    = 
    "int"  
    keyProperty 
    =  
    "userId"  
    order  
    = 
    "BEFORE" 
    >  
      
 
   
             
    <![CDATA[ 
    select SQ_BM_MSG_QUE.nextval as ID from dual 
    ]]>
 
   
                
    </ 
    selectKey 
    >
 
   
       
    <![CDATA[ 
    insert into tb_user(user_id,user_name,password,address)
 
   
                      values(#{userId},#{userName},#{password},#{address})  
    ]]>
 
   
         
    </ 
    insert 
    >
 
   

 
   
         
    <!-- 修改记录 -->
 
   
         
    < 
    update  
    id 
    =  
    "updateUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
              update tb_user
 
   
                
    < 
    trim  
    prefix 
    =  
    "SET"  
    suffixOverrides  
    = 
    "," 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName !=null" 
    > 
    user_name = #{userName},  
    </ 
    if 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "address !=null" 
    > 
    address = #{address},  
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
              where user_id =#{userId}
 
   
         
    </ 
    update 
    >
 
   

 
   
         
    <!-- 删除记录 -->
 
   
         
    < 
    delete  
    id 
    =  
    "deleteUser"  
    parameterType  
    = 
    "int" 
    >
 
   
              delete from tb_user where
 
   
              user_id = #{userId}
 
   
         
    </ 
    delete 
    >
 
   

 
   

 
   
         
    < 
    select  
    id 
    =  
    "proHello"  
    statementType  
    = 
    "CALLABLE"  
    useCache 
    =  
    "false" 
    > 
     
 
   
         
    <![CDATA[ 
     
 
   
           {call pro_hello (#{p_user_name,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})} 
 
   
         
    ]]>
 
   
         
    </ 
    select 
    >
 
   
</ 
    mapper 
    >
 
  

 
  
• 3.编写mybatis配置文件(MyBatis-Configuration.xml)
 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    configuration  
    PUBLIC
 
   
         
    "-//mybatis.org//DTD Config 3.0//EN"
 
   
         
    "http://mybatis.org/dtd/mybatis-3-config.dtd"  
    >
 
   
< 
    configuration 
    >
 
   
         
    < 
    settings 
    >
 
   
                
    < 
    setting  
    name 
    =  
    "jdbcTypeForNull"  
    value  
    = 
    "OTHER"  
    />
 
   
                
    < 
    setting  
    name 
    =  
    "mapUnderscoreToCamelCase"  
    value 
    = 
    "true"  
    /> 
    <!-- 
    驼峰形式 
    -->
 
   
         
    </ 
    settings 
    >
 
   

 
   
         
    < 
    typeAliases 
    >
 
   
                
    < 
    typeAlias  
    alias 
    =  
    "userBean"
 
   
                       
    type 
    =  
    "com.eshore.common.mybatis.test.model.UserBean"  
    />
 
   
         
    </ 
    typeAliases 
    >
 
   
         
    < 
    plugins 
    >
 
   
                
    < 
    plugin  
    interceptor 
    = 
    "com.poson.daopub.database.plugin.PagePlugin"  
    >
 
   
                       
    < 
    property  
    name 
    =  
    "dialect"  
    value  
    = 
    "oracle"  
    />
 
   
                       
    < 
    property  
    name 
    =  
    "pageSqlId"  
    value  
    = 
    ".*select.*"  
    />
 
   
                
    </ 
    plugin 
    >
 
   
         
    </ 
    plugins 
    >
 
   

 
   
         
    <!-- 配置mappers -->
 
   
         
    < 
    mappers 
    >
 
   
                
    < 
    mapper  
    resource 
    = 
    "com/eshore/common/mybatis/test/mapper/UserMapper.xml"  
    />
 
   
         
    </ 
    mappers 
    >
 
   

 
   
</ 
    configuration 
    >  
       
 
   

 
  
• 4.编写服务层接口(IUserService .java)
 
  

    package com.eshore.common.mybatis.test.service; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   

 public interface IUserService { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      public UserBean getUser(int userId); 
   

      public void insert(UserBean userBean); 
   

      public void testProcedure(String prodId,Map<String, String> param); 
   

 } 
    
  
• 5.编写服务层实现代码
• 5.1SqlSessinTemplate方式
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.jdbc.SQL; 
   
 import org.mybatis.spring.SqlSessionTemplate; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl2 implements IUserService { 
   

      public SqlSessionTemplate sqlSessionTemplate; 
   

      @Override 
   
      public void deleteUser(int userId) { 
   
           // TODO Auto-generated method stub 
   
           sqlSessionTemplate.delete( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.deleteUser", 
   
                     userId); 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return sqlSessionTemplate.selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
    
  
 
   
 
  

        ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.2 SqlSessionDaoSupport
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.mybatis.spring.support.SqlSessionDaoSupport; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl3 extends SqlSessionDaoSupport implements 
   
           IUserService { 
   
      @Override 
   
      public void deleteUser(int userId) { 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return (UserBean) this.getSqlSession().selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
   
  

      ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.3注解方式
 
  
package  
     com.eshore.common.mybatis.test.service.impl;
 
    

 
    
import  
     java.util.List;
 
    
import  
     java.util.Map;
 
    

 
    
import  
     org.springframework.beans.factory.annotation.Autowired;
 
    
import  
     org.springframework.stereotype.Service;
 
    
import  
     org.springframework.transaction.annotation.Transactional;
 
    

 
    
import  
     com.eshore.common.mybatis.test.mapper.UserMapper;
 
    
import  
     com.eshore.common.mybatis.test.model.UserBean;
 
    

 
   
@Service 
    (  
    "userServiceImpl4" 
    )
 
   
public  
    class  
    UserServiceImpl4 {
 
   

 
   
      
    @Autowired
 
   
      
    private  
    UserMapper  
    mapper 
    ;
 
   

 
   
       
    @Transactional
 
   
      
    public  
    void  
    deleteUser(  
    int  
    userId) {
 
   
             
    mapper 
    .deleteUser(userId);
 
   
     }
 
   

 
   
      
    public  
    UserBean getUser(  
    int  
    userId) {
 
   
             
    return  
    (UserBean)  
    mapper 
    .getUser(userId);
 
   

 
   
     }
 
   
}
 
  
• 6.编写spring配置文件(applicationContext.xml)
 
  
<? 
   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:tx 
   =  
   "http://www.springframework.org/schema/tx"  
   xmlns:mvc 
   = 
   "http://www.springframework.org/schema/mvc"
 
  
         
   xsi:schemaLocation 
   = 
   "http://www.springframework.org/schema/beans 
 
  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 
  
     http://www.springframework.org/schema/context 
 
  
   http://www.springframework.org/schema/context/spring-context-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/mvc 
 
  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
   >
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "propertyConfigurer"
 
  
                
   class 
   =  
   "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "locations" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   classpath:conf/jdbc.properties 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "dataSource"  
   class  
   = 
   "org.apache.commons.dbcp.BasicDataSource"
 
  
                
   destroy-method 
   = 
   "close"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "driverClassName"  
   value  
   = 
   "${jdbc.driverClassName}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "url"  
   value  
   = 
   "${jdbc.url}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "username"  
   value  
   = 
   "${jdbc.username}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "password"  
   value  
   = 
   "${jdbc.password}"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
       
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "transactionManager"
 
  
                
   class 
   =  
   "org.springframework.jdbc.datasource.DataSourceTransactionManager"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
         
   </ 
   bean 
   >
 
  
         
   <!--
 
  
              product <bean id="dataSource"
 
  
              class="org.springframework.jndi.JndiObjectFactoryBean"> <property
 
  
              name="jndiName" value="${datasource.name}" /> </bean>
 
  
       -->
 
  
         
   < 
   bean  
   id 
   =  
   "transactionInterceptor"
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionInterceptor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionManager"  
   ref  
   = 
   "transactionManager"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "transactionAttributes"  
   >
 
  
                       
   < 
   props 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "writeLogToDBTable*" 
   >
 
  
                                  PROPAGATION_REQUIRES_NEW
 
  
                             
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "saveRequest"  
   > 
   PROPAGATION_REQUIRES_NEW  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "*" 
   > 
   PROPAGATION_REQUIRED  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "find*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "query*"  
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "get*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                       
   </ 
   props 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "beanNames"  
   value  
   = 
   "*ServiceImpl*"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "proxyTargetClass"  
   value  
   = 
   "true" 
   ></  
   property 
   >
 
  
                
   < 
   property  
   name 
   =  
   "interceptorNames" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   transactionInterceptor 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionInterceptor"  
   ref 
   = 
   "transactionInterceptor"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   <!-- 开启注解配置 -->
 
  
         
   < 
   context:annotation-config  
   />
 
  

 
  
         
   <!--
 
  
              扫描service层 <context:exclude-filter type="regex"
 
  
              expression="com.poson.*.*.model"/>
 
  
       -->
 
  
         
   < 
   context:component-scan  
   base-package 
   = 
   "com.eshore.common.mybatis.test.service"  
   />
 
  

 
  
         
   <!-- 开启事务注解驱动 -->
 
  
         
   < 
   tx:annotation-driven  
   transaction-manager 
   = 
   "transactionManager"  
   />
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionFactory"  
   class 
   = 
   "org.mybatis.spring.SqlSessionFactoryBean"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "configLocation"  
   value 
   = 
   "classpath:conf/MyBatis-config.xml"  
   />
 
  
                
   <!--
 
  
                     <property name="typeAliasesPackage"
 
  
                     value="com.eshore.common.mybatis.test.model" />
 
  
              -->
 
  
                
   <!-- 多个以,分开 ;可以在MyBatis-config.xml中用typeAliases指定具体的别名-->
 
  
                
   <!--
 
  
                     <property name="transactionFactory"> <bean
 
  
                     class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"
 
  
                     /> </property>
 
  
              -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionTemplate"  
   class 
   = 
   "org.mybatis.spring.SqlSessionTemplate"  
   >
 
  
                
   < 
   constructor-arg  
   index 
   =  
   "0"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userMapper "  
   class  
   = 
   "org.mybatis.spring.mapper.MapperFactoryBean" 
   >
 
  
                
   < 
   property  
   name 
   =  
   "mapperInterface"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.UserMapper"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactory"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   <!--
 
  
              自动创建包下类名对应的bean Id为类名(第一个字符小写)
 
  
              两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。
 
  
              annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。
 
  
              markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。
 
  
              如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。
 
  
              即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。
 
  
       -->
 
  
         
   < 
   bean  
   class 
   =  
   "org.mybatis.spring.mapper.MapperScannerConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "basePackage"  
   value  
   = 
   "com.eshore.common.mybatis.test"  
   />
 
  

 
  
                
   < 
   property  
   name 
   =  
   "annotationClass"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation"  
   />
 
  
                
   <!--
 
  
                     <property name="markerInterface"
 
  
                     value="com.tiantian.mybatis.mapper.SuperMapper" />
 
  
              -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactoryBeanName"  
   value 
   = 
   "sqlSessionFactory"  
   />
 
  
                
   <!-- sqlSessionFactory已废弃 -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "userMapper"  
   ref  
   = 
   "userMapper" 
   ></  
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl2"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl2"  
   >
 
  
                
   <!--注入SqlSessionTemplate实例 -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
                
   <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
 
  
                
   <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl3"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl3"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
</ 
   beans 
   >
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    mapper  
    PUBLIC  
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"  
    >
 
   
< 
    mapper  
    namespace 
    = 
    "com.eshore.common.mybatis.test.mapper.UserMapper"  
    >
 
   
         
    < 
    cache  
    />
 
   
         
    <!--列名以下划线分割, 自动映射成驼峰格式 -->
 
   
         
    < 
    select  
    id 
    =  
    "queryUsers"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    < 
    select  
    id 
    =  
    "User_select"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    <!-- -->
 
   
         
    < 
    select  
    id 
    =  
    "selectUser"  
    parameterType  
    = 
    "Integer"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
              where t.user_id = #{userId}
 
   
         
    </ 
    select 
    >
 
   

 
   
         
    <!-- 新增记录 -->
 
   
         
    < 
    insert  
    id 
    =  
    "insertUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
                
    < 
    selectKey  
    resultType 
    = 
    "int"  
    keyProperty 
    =  
    "userId"  
    order  
    = 
    "BEFORE" 
    >  
      
 
   
             
    <![CDATA[ 
    select SQ_BM_MSG_QUE.nextval as ID from dual 
    ]]>
 
   
                
    </ 
    selectKey 
    >
 
   
       
    <![CDATA[ 
    insert into tb_user(user_id,user_name,password,address)
 
   
                      values(#{userId},#{userName},#{password},#{address})  
    ]]>
 
   
         
    </ 
    insert 
    >
 
   

 
   
         
    <!-- 修改记录 -->
 
   
         
    < 
    update  
    id 
    =  
    "updateUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
              update tb_user
 
   
                
    < 
    trim  
    prefix 
    =  
    "SET"  
    suffixOverrides  
    = 
    "," 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName !=null" 
    > 
    user_name = #{userName},  
    </ 
    if 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "address !=null" 
    > 
    address = #{address},  
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
              where user_id =#{userId}
 
   
         
    </ 
    update 
    >
 
   

 
   
         
    <!-- 删除记录 -->
 
   
         
    < 
    delete  
    id 
    =  
    "deleteUser"  
    parameterType  
    = 
    "int" 
    >
 
   
              delete from tb_user where
 
   
              user_id = #{userId}
 
   
         
    </ 
    delete 
    >
 
   

 
   

 
   
         
    < 
    select  
    id 
    =  
    "proHello"  
    statementType  
    = 
    "CALLABLE"  
    useCache 
    =  
    "false" 
    > 
     
 
   
         
    <![CDATA[ 
     
 
   
           {call pro_hello (#{p_user_name,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})} 
 
   
         
    ]]>
 
   
         
    </ 
    select 
    >
 
   
</ 
    mapper 
    >
 
  

 
  
• 3.编写mybatis配置文件(MyBatis-Configuration.xml)
 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    configuration  
    PUBLIC
 
   
         
    "-//mybatis.org//DTD Config 3.0//EN"
 
   
         
    "http://mybatis.org/dtd/mybatis-3-config.dtd"  
    >
 
   
< 
    configuration 
    >
 
   
         
    < 
    settings 
    >
 
   
                
    < 
    setting  
    name 
    =  
    "jdbcTypeForNull"  
    value  
    = 
    "OTHER"  
    />
 
   
                
    < 
    setting  
    name 
    =  
    "mapUnderscoreToCamelCase"  
    value 
    = 
    "true"  
    /> 
    <!-- 
    驼峰形式 
    -->
 
   
         
    </ 
    settings 
    >
 
   

 
   
         
    < 
    typeAliases 
    >
 
   
                
    < 
    typeAlias  
    alias 
    =  
    "userBean"
 
   
                       
    type 
    =  
    "com.eshore.common.mybatis.test.model.UserBean"  
    />
 
   
         
    </ 
    typeAliases 
    >
 
   
         
    < 
    plugins 
    >
 
   
                
    < 
    plugin  
    interceptor 
    = 
    "com.poson.daopub.database.plugin.PagePlugin"  
    >
 
   
                       
    < 
    property  
    name 
    =  
    "dialect"  
    value  
    = 
    "oracle"  
    />
 
   
                       
    < 
    property  
    name 
    =  
    "pageSqlId"  
    value  
    = 
    ".*select.*"  
    />
 
   
                
    </ 
    plugin 
    >
 
   
         
    </ 
    plugins 
    >
 
   

 
   
         
    <!-- 配置mappers -->
 
   
         
    < 
    mappers 
    >
 
   
                
    < 
    mapper  
    resource 
    = 
    "com/eshore/common/mybatis/test/mapper/UserMapper.xml"  
    />
 
   
         
    </ 
    mappers 
    >
 
   

 
   
</ 
    configuration 
    >  
       
 
   

 
  
• 4.编写服务层接口(IUserService .java)
 
  

    package com.eshore.common.mybatis.test.service; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   

 public interface IUserService { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      public UserBean getUser(int userId); 
   

      public void insert(UserBean userBean); 
   

      public void testProcedure(String prodId,Map<String, String> param); 
   

 } 
    
  
• 5.编写服务层实现代码
• 5.1SqlSessinTemplate方式
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.jdbc.SQL; 
   
 import org.mybatis.spring.SqlSessionTemplate; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl2 implements IUserService { 
   

      public SqlSessionTemplate sqlSessionTemplate; 
   

      @Override 
   
      public void deleteUser(int userId) { 
   
           // TODO Auto-generated method stub 
   
           sqlSessionTemplate.delete( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.deleteUser", 
   
                     userId); 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return sqlSessionTemplate.selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
    
  
 
   
 
  

        ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.2 SqlSessionDaoSupport
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.mybatis.spring.support.SqlSessionDaoSupport; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl3 extends SqlSessionDaoSupport implements 
   
           IUserService { 
   
      @Override 
   
      public void deleteUser(int userId) { 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return (UserBean) this.getSqlSession().selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
   
  

      ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.3注解方式
 
  
package  
     com.eshore.common.mybatis.test.service.impl;
 
    

 
    
import  
     java.util.List;
 
    
import  
     java.util.Map;
 
    

 
    
import  
     org.springframework.beans.factory.annotation.Autowired;
 
    
import  
     org.springframework.stereotype.Service;
 
    
import  
     org.springframework.transaction.annotation.Transactional;
 
    

 
    
import  
     com.eshore.common.mybatis.test.mapper.UserMapper;
 
    
import  
     com.eshore.common.mybatis.test.model.UserBean;
 
    

 
   
@Service 
    (  
    "userServiceImpl4" 
    )
 
   
public  
    class  
    UserServiceImpl4 {
 
   

 
   
      
    @Autowired
 
   
      
    private  
    UserMapper  
    mapper 
    ;
 
   

 
   
       
    @Transactional
 
   
      
    public  
    void  
    deleteUser(  
    int  
    userId) {
 
   
             
    mapper 
    .deleteUser(userId);
 
   
     }
 
   

 
   
      
    public  
    UserBean getUser(  
    int  
    userId) {
 
   
             
    return  
    (UserBean)  
    mapper 
    .getUser(userId);
 
   

 
   
     }
 
   
}
 
  
• 6.编写spring配置文件(applicationContext.xml)
 
  
<? 
   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:tx 
   =  
   "http://www.springframework.org/schema/tx"  
   xmlns:mvc 
   = 
   "http://www.springframework.org/schema/mvc"
 
  
         
   xsi:schemaLocation 
   = 
   "http://www.springframework.org/schema/beans 
 
  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 
  
     http://www.springframework.org/schema/context 
 
  
   http://www.springframework.org/schema/context/spring-context-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/mvc 
 
  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
   >
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "propertyConfigurer"
 
  
                
   class 
   =  
   "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "locations" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   classpath:conf/jdbc.properties 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "dataSource"  
   class  
   = 
   "org.apache.commons.dbcp.BasicDataSource"
 
  
                
   destroy-method 
   = 
   "close"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "driverClassName"  
   value  
   = 
   "${jdbc.driverClassName}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "url"  
   value  
   = 
   "${jdbc.url}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "username"  
   value  
   = 
   "${jdbc.username}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "password"  
   value  
   = 
   "${jdbc.password}"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
       
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "transactionManager"
 
  
                
   class 
   =  
   "org.springframework.jdbc.datasource.DataSourceTransactionManager"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
         
   </ 
   bean 
   >
 
  
         
   <!--
 
  
              product <bean id="dataSource"
 
  
              class="org.springframework.jndi.JndiObjectFactoryBean"> <property
 
  
              name="jndiName" value="${datasource.name}" /> </bean>
 
  
       -->
 
  
         
   < 
   bean  
   id 
   =  
   "transactionInterceptor"
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionInterceptor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionManager"  
   ref  
   = 
   "transactionManager"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "transactionAttributes"  
   >
 
  
                       
   < 
   props 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "writeLogToDBTable*" 
   >
 
  
                                  PROPAGATION_REQUIRES_NEW
 
  
                             
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "saveRequest"  
   > 
   PROPAGATION_REQUIRES_NEW  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "*" 
   > 
   PROPAGATION_REQUIRED  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "find*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "query*"  
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "get*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                       
   </ 
   props 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "beanNames"  
   value  
   = 
   "*ServiceImpl*"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "proxyTargetClass"  
   value  
   = 
   "true" 
   ></  
   property 
   >
 
  
                
   < 
   property  
   name 
   =  
   "interceptorNames" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   transactionInterceptor 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionInterceptor"  
   ref 
   = 
   "transactionInterceptor"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   <!-- 开启注解配置 -->
 
  
         
   < 
   context:annotation-config  
   />
 
  

 
  
         
   <!--
 
  
              扫描service层 <context:exclude-filter type="regex"
 
  
              expression="com.poson.*.*.model"/>
 
  
       -->
 
  
         
   < 
   context:component-scan  
   base-package 
   = 
   "com.eshore.common.mybatis.test.service"  
   />
 
  

 
  
         
   <!-- 开启事务注解驱动 -->
 
  
         
   < 
   tx:annotation-driven  
   transaction-manager 
   = 
   "transactionManager"  
   />
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionFactory"  
   class 
   = 
   "org.mybatis.spring.SqlSessionFactoryBean"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "configLocation"  
   value 
   = 
   "classpath:conf/MyBatis-config.xml"  
   />
 
  
                
   <!--
 
  
                     <property name="typeAliasesPackage"
 
  
                     value="com.eshore.common.mybatis.test.model" />
 
  
              -->
 
  
                
   <!-- 多个以,分开 ;可以在MyBatis-config.xml中用typeAliases指定具体的别名-->
 
  
                
   <!--
 
  
                     <property name="transactionFactory"> <bean
 
  
                     class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"
 
  
                     /> </property>
 
  
              -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionTemplate"  
   class 
   = 
   "org.mybatis.spring.SqlSessionTemplate"  
   >
 
  
                
   < 
   constructor-arg  
   index 
   =  
   "0"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userMapper "  
   class  
   = 
   "org.mybatis.spring.mapper.MapperFactoryBean" 
   >
 
  
                
   < 
   property  
   name 
   =  
   "mapperInterface"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.UserMapper"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactory"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   <!--
 
  
              自动创建包下类名对应的bean Id为类名(第一个字符小写)
 
  
              两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。
 
  
              annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。
 
  
              markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。
 
  
              如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。
 
  
              即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。
 
  
       -->
 
  
         
   < 
   bean  
   class 
   =  
   "org.mybatis.spring.mapper.MapperScannerConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "basePackage"  
   value  
   = 
   "com.eshore.common.mybatis.test"  
   />
 
  

 
  
                
   < 
   property  
   name 
   =  
   "annotationClass"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation"  
   />
 
  
                
   <!--
 
  
                     <property name="markerInterface"
 
  
                     value="com.tiantian.mybatis.mapper.SuperMapper" />
 
  
              -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactoryBeanName"  
   value 
   = 
   "sqlSessionFactory"  
   />
 
  
                
   <!-- sqlSessionFactory已废弃 -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "userMapper"  
   ref  
   = 
   "userMapper" 
   ></  
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl2"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl2"  
   >
 
  
                
   <!--注入SqlSessionTemplate实例 -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
                
   <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
 
  
                
   <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl3"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl3"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
</ 
   beans 
   >  
    
 
  

 
  
• 7.测试代码(UserServiceTest.java)
 
  

    package com.eshore.common.mybatis.test; 
   

 import java.io.IOException; 
   
 import java.io.Reader; 
   
 import java.util.Date; 
   
 import java.util.HashMap; 
   
 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.io.Resources; 
   
 import org.apache.ibatis.logging.Log; 
   
 import org.apache.ibatis.logging.LogFactory; 
   
 import org.apache.ibatis.session.SqlSession; 
   
 import org.apache.ibatis.session.SqlSessionFactory; 
   
 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
   
 import org.junit.Before; 
   
 import org.junit.Test; 
   
 import org.springframework.context.ApplicationContext; 
   
 import org.springframework.context.support.ClassPathXmlApplicationContext; 
   

 import com.eshore.common.mybatis.test.mapper.UserMapper; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   
 import com.eshore.common.mybatis.test.service.impl.UserServiceImpl4; 
   

 public class UserTest { 
   
      private static final Log logger = LogFactory.getLog(UserTest.class); 
   
      private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder; 
   
      private static SqlSessionFactory sqlSessionFactory; 
   
      private static ApplicationContext context; 
   

      @Before 
   
      public void init() { 
   

           context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
   

      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   
      @Test 
   
      public void testService1() { 
   
           IUserService userService = (IUserService) context 
   
                     .getBean("userServiceImpl"); 
   
           UserBean user = new UserBean(); 
   
           user.setUserName("crm"); 
   
           try { 
   
                List<UserBean> list = (List<UserBean>) userService.queryUsers(user); 
   

                if (null != list && list.size() > 0) { 
   

                     System.out.println("userServiceImpl===" 
   
                               + ((UserBean) list.get(0)).toString()); 
   
                } 
   
           } catch (Exception e) { 
   
                e.printStackTrace(); 
   
           } 
   
           UserBean userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           userBean.setAddress("poson"); 
   
           userService.updateUser(userBean); 
   

           userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           UserBean newUser = new UserBean(); 
   
           newUser.setAddress("test"); 
   
           newUser.setUserName("crm"); 
   
           newUser.setPassword("aaaaa"); 
   
           newUser.setCreateTime(new Date().toLocaleString()); 
   
           userService.insertUser(newUser); 
   

           UserBean otherUser = new UserBean(); 
   
           otherUser.setUserName("ods"); 
   
           otherUser.setPassword("test"); 
   
           userService.insert(otherUser); 
   

           userService.deleteUser(37070013); 
   
      } 
   

      /** 
   
      * IOC sqlSessionTemplate 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService2() { 
   
           IUserService userService2 = (IUserService) context 
   
                     .getBean("userServiceImpl2"); 
   
           UserBean userBean = userService2.getUser(2); 
   
           System.out.println("userServiceImpl2====" + userBean.toString()); 
   
      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService3() { 
   
           UserBean userBean = new UserBean(); 
   
           IUserService userService3 = (IUserService) context 
   
                     .getBean("userServiceImpl3"); 
   
           userBean = userService3.getUser(1); 
   
           System.out.println("userServiceImpl3====" + userBean.toString()); 
   
      } 
   

      @Test 
   
      public void TestService4() { // 
   
           UserBean userBean = new UserBean(); 
   
           UserServiceImpl4 userService4 = (UserServiceImpl4) context 
   
                     .getBean("userServiceImpl4"); // // 
   
           userBean.setUserName("serviceImpl4"); 
   
           userService4.insert(userBean); 
   
           System.out.println("userServiceImpl4====" + userBean.toString()); 
   
      } 
   

 } 
   
  
 
 

         
 
本文主要介绍了如何将mybatis和spring整合在一起的几种方式。
 
  
环境:mybatis3.2.4+ spring3.1.1,使用dbcp作为数据库连接池。
 
  
• 1.编写数据访问接口(UserMapper.java)
 
  

    package com.eshore.common.mybatis.test.mapper; 
  
 
  
 
   
 import java.util.List; 
   

 import org.apache.ibatis.annotations.Insert; 
   
 import org.apache.ibatis.annotations.Param; 
   
 import org.apache.ibatis.annotations.Select; 
   
 import org.apache.ibatis.annotations.SelectKey; 
   

 import com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   

 @MyBatisAnnotation 
   
 public interface UserMapper { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      @Select("SELECT * FROM tb_user WHERE user_id = #{userId}") 
   
      public UserBean getUser(@Param("userId") int userId); 
   

      @Insert("INSERT INTO tb_user(user_id,user_name) VALUES (#{userId},#{userName})") 
   
      @SelectKey(keyProperty = "userId", before = true, resultType = Integer.class, statement = { " select SQ_BM_MSG_QUE.nextval AS userId from dual" }) 
   
      public void insert(UserBean userBean); 
   
 } 
    
  
 
   
 
  

    此处为了演示,使用了Annotation方式,包括接口的声明部分。 
  
 
  
 
   
 
  
• 2.编写数据访问接口映射文件(UserMapper.xml)
 
  

 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    mapper  
    PUBLIC  
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"  
    >
 
   
< 
    mapper  
    namespace 
    = 
    "com.eshore.common.mybatis.test.mapper.UserMapper"  
    >
 
   
         
    < 
    cache  
    />
 
   
         
    <!--列名以下划线分割, 自动映射成驼峰格式 -->
 
   
         
    < 
    select  
    id 
    =  
    "queryUsers"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    < 
    select  
    id 
    =  
    "User_select"  
    parameterType  
    = 
    "UserBean"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
                
    < 
    trim  
    prefix 
    =  
    "WHERE"  
    prefixOverrides  
    = 
    "AND|OR" 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName != null" 
    >
 
   
                           t.user_name = #{userName}
 
   
                       
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
         
    </ 
    select 
    >
 
   
         
    <!-- -->
 
   
         
    < 
    select  
    id 
    =  
    "selectUser"  
    parameterType  
    = 
    "Integer"  
    resultType 
    = 
    "UserBean"
 
   
                
    useCache 
    =  
    "false" 
    >
 
   
              select * from tb_user t
 
   
              where t.user_id = #{userId}
 
   
         
    </ 
    select 
    >
 
   

 
   
         
    <!-- 新增记录 -->
 
   
         
    < 
    insert  
    id 
    =  
    "insertUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
                
    < 
    selectKey  
    resultType 
    = 
    "int"  
    keyProperty 
    =  
    "userId"  
    order  
    = 
    "BEFORE" 
    >  
      
 
   
             
    <![CDATA[ 
    select SQ_BM_MSG_QUE.nextval as ID from dual 
    ]]>
 
   
                
    </ 
    selectKey 
    >
 
   
       
    <![CDATA[ 
    insert into tb_user(user_id,user_name,password,address)
 
   
                      values(#{userId},#{userName},#{password},#{address})  
    ]]>
 
   
         
    </ 
    insert 
    >
 
   

 
   
         
    <!-- 修改记录 -->
 
   
         
    < 
    update  
    id 
    =  
    "updateUser"  
    parameterType  
    = 
    "UserBean" 
    >
 
   
              update tb_user
 
   
                
    < 
    trim  
    prefix 
    =  
    "SET"  
    suffixOverrides  
    = 
    "," 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "userName !=null" 
    > 
    user_name = #{userName},  
    </ 
    if 
    >
 
   
                       
    < 
    if  
    test 
    =  
    "address !=null" 
    > 
    address = #{address},  
    </ 
    if 
    >
 
   
                
    </ 
    trim 
    >
 
   
              where user_id =#{userId}
 
   
         
    </ 
    update 
    >
 
   

 
   
         
    <!-- 删除记录 -->
 
   
         
    < 
    delete  
    id 
    =  
    "deleteUser"  
    parameterType  
    = 
    "int" 
    >
 
   
              delete from tb_user where
 
   
              user_id = #{userId}
 
   
         
    </ 
    delete 
    >
 
   

 
   

 
   
         
    < 
    select  
    id 
    =  
    "proHello"  
    statementType  
    = 
    "CALLABLE"  
    useCache 
    =  
    "false" 
    > 
     
 
   
         
    <![CDATA[ 
     
 
   
           {call pro_hello (#{p_user_name,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})} 
 
   
         
    ]]>
 
   
         
    </ 
    select 
    >
 
   
</ 
    mapper 
    >
 
  

 
  
• 3.编写mybatis配置文件(MyBatis-Configuration.xml)
 
  
<? 
    xml  
    version 
    =  
    "1.0"  
    encoding  
    = 
    "UTF-8"  
    ?>
 
   
<! 
    DOCTYPE  
    configuration  
    PUBLIC
 
   
         
    "-//mybatis.org//DTD Config 3.0//EN"
 
   
         
    "http://mybatis.org/dtd/mybatis-3-config.dtd"  
    >
 
   
< 
    configuration 
    >
 
   
         
    < 
    settings 
    >
 
   
                
    < 
    setting  
    name 
    =  
    "jdbcTypeForNull"  
    value  
    = 
    "OTHER"  
    />
 
   
                
    < 
    setting  
    name 
    =  
    "mapUnderscoreToCamelCase"  
    value 
    = 
    "true"  
    /> 
    <!-- 
    驼峰形式 
    -->
 
   
         
    </ 
    settings 
    >
 
   

 
   
         
    < 
    typeAliases 
    >
 
   
                
    < 
    typeAlias  
    alias 
    =  
    "userBean"
 
   
                       
    type 
    =  
    "com.eshore.common.mybatis.test.model.UserBean"  
    />
 
   
         
    </ 
    typeAliases 
    >
 
   
         
    < 
    plugins 
    >
 
   
                
    < 
    plugin  
    interceptor 
    = 
    "com.poson.daopub.database.plugin.PagePlugin"  
    >
 
   
                       
    < 
    property  
    name 
    =  
    "dialect"  
    value  
    = 
    "oracle"  
    />
 
   
                       
    < 
    property  
    name 
    =  
    "pageSqlId"  
    value  
    = 
    ".*select.*"  
    />
 
   
                
    </ 
    plugin 
    >
 
   
         
    </ 
    plugins 
    >
 
   

 
   
         
    <!-- 配置mappers -->
 
   
         
    < 
    mappers 
    >
 
   
                
    < 
    mapper  
    resource 
    = 
    "com/eshore/common/mybatis/test/mapper/UserMapper.xml"  
    />
 
   
         
    </ 
    mappers 
    >
 
   

 
   
</ 
    configuration 
    >  
       
 
   

 
  
• 4.编写服务层接口(IUserService .java)
 
  

    package com.eshore.common.mybatis.test.service; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   

 public interface IUserService { 
   

      public List<UserBean> queryUsers(UserBean user) throws Exception; 
   

      public void insertUser(UserBean user); 
   

      public void updateUser(UserBean user); 
   

      public void deleteUser(int userId); 
   

      public UserBean getUser(int userId); 
   

      public void insert(UserBean userBean); 
   

      public void testProcedure(String prodId,Map<String, String> param); 
   

 } 
    
  
• 5.编写服务层实现代码
• 5.1SqlSessinTemplate方式
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.jdbc.SQL; 
   
 import org.mybatis.spring.SqlSessionTemplate; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl2 implements IUserService { 
   

      public SqlSessionTemplate sqlSessionTemplate; 
   

      @Override 
   
      public void deleteUser(int userId) { 
   
           // TODO Auto-generated method stub 
   
           sqlSessionTemplate.delete( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.deleteUser", 
   
                     userId); 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return sqlSessionTemplate.selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
    
  
 
   
 
  

        ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.2 SqlSessionDaoSupport
 
  

    package com.eshore.common.mybatis.test.service.impl; 
   

 import java.util.List; 
   
 import java.util.Map; 
   

 import org.mybatis.spring.support.SqlSessionDaoSupport; 
   

 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   

 public class UserServiceImpl3 extends SqlSessionDaoSupport implements 
   
           IUserService { 
   
      @Override 
   
      public void deleteUser(int userId) { 
   

      } 
   

      @Override 
   
      public UserBean getUser(int userId) { 
   
           return (UserBean) this.getSqlSession().selectOne( 
   
                     "com.eshore.common.mybatis.test.mapper.UserMapper.selectUser", 
   
                     userId); 
   
      } 
   
  

      ...其他方法实现省略....... 
  
 
  

    } 
  
 
  
 
   
 
  
• 5.3注解方式
 
  
package  
     com.eshore.common.mybatis.test.service.impl;
 
    

 
    
import  
     java.util.List;
 
    
import  
     java.util.Map;
 
    

 
    
import  
     org.springframework.beans.factory.annotation.Autowired;
 
    
import  
     org.springframework.stereotype.Service;
 
    
import  
     org.springframework.transaction.annotation.Transactional;
 
    

 
    
import  
     com.eshore.common.mybatis.test.mapper.UserMapper;
 
    
import  
     com.eshore.common.mybatis.test.model.UserBean;
 
    

 
   
@Service 
    (  
    "userServiceImpl4" 
    )
 
   
public  
    class  
    UserServiceImpl4 {
 
   

 
   
      
    @Autowired
 
   
      
    private  
    UserMapper  
    mapper 
    ;
 
   

 
   
       
    @Transactional
 
   
      
    public  
    void  
    deleteUser(  
    int  
    userId) {
 
   
             
    mapper 
    .deleteUser(userId);
 
   
     }
 
   

 
   
      
    public  
    UserBean getUser(  
    int  
    userId) {
 
   
             
    return  
    (UserBean)  
    mapper 
    .getUser(userId);
 
   

 
   
     }
 
   
}
 
  
• 6.编写spring配置文件(applicationContext.xml)
 
  
<? 
   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:tx 
   =  
   "http://www.springframework.org/schema/tx"  
   xmlns:mvc 
   = 
   "http://www.springframework.org/schema/mvc"
 
  
         
   xsi:schemaLocation 
   = 
   "http://www.springframework.org/schema/beans 
 
  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 
  
     http://www.springframework.org/schema/context 
 
  
   http://www.springframework.org/schema/context/spring-context-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/mvc 
 
  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
   >
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "propertyConfigurer"
 
  
                
   class 
   =  
   "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "locations" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   classpath:conf/jdbc.properties 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "dataSource"  
   class  
   = 
   "org.apache.commons.dbcp.BasicDataSource"
 
  
                
   destroy-method 
   = 
   "close"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "driverClassName"  
   value  
   = 
   "${jdbc.driverClassName}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "url"  
   value  
   = 
   "${jdbc.url}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "username"  
   value  
   = 
   "${jdbc.username}"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "password"  
   value  
   = 
   "${jdbc.password}"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
       
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "transactionManager"
 
  
                
   class 
   =  
   "org.springframework.jdbc.datasource.DataSourceTransactionManager"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
         
   </ 
   bean 
   >
 
  
         
   <!--
 
  
              product <bean id="dataSource"
 
  
              class="org.springframework.jndi.JndiObjectFactoryBean"> <property
 
  
              name="jndiName" value="${datasource.name}" /> </bean>
 
  
       -->
 
  
         
   < 
   bean  
   id 
   =  
   "transactionInterceptor"
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionInterceptor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionManager"  
   ref  
   = 
   "transactionManager"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "transactionAttributes"  
   >
 
  
                       
   < 
   props 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "writeLogToDBTable*" 
   >
 
  
                                  PROPAGATION_REQUIRES_NEW
 
  
                             
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "saveRequest"  
   > 
   PROPAGATION_REQUIRES_NEW  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "*" 
   > 
   PROPAGATION_REQUIRED  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "find*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   = 
   "query*"  
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                             
   < 
   prop  
   key 
   =  
   "get*" 
   > 
   PROPAGATION_REQUIRED,readOnly  
   </ 
   prop 
   >
 
  
                       
   </ 
   props 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "beanNames"  
   value  
   = 
   "*ServiceImpl*"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "proxyTargetClass"  
   value  
   = 
   "true" 
   ></  
   property 
   >
 
  
                
   < 
   property  
   name 
   =  
   "interceptorNames" 
   >
 
  
                       
   < 
   list 
   >
 
  
                             
   < 
   value 
   >  
   transactionInterceptor 
   </ 
   value  
   >
 
  
                       
   </ 
   list 
   >
 
  
                
   </ 
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   < 
   bean
 
  
                
   class 
   =  
   "org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "transactionInterceptor"  
   ref 
   = 
   "transactionInterceptor"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   <!-- 开启注解配置 -->
 
  
         
   < 
   context:annotation-config  
   />
 
  

 
  
         
   <!--
 
  
              扫描service层 <context:exclude-filter type="regex"
 
  
              expression="com.poson.*.*.model"/>
 
  
       -->
 
  
         
   < 
   context:component-scan  
   base-package 
   = 
   "com.eshore.common.mybatis.test.service"  
   />
 
  

 
  
         
   <!-- 开启事务注解驱动 -->
 
  
         
   < 
   tx:annotation-driven  
   transaction-manager 
   = 
   "transactionManager"  
   />
 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionFactory"  
   class 
   = 
   "org.mybatis.spring.SqlSessionFactoryBean"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "dataSource"  
   ref  
   = 
   "dataSource"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "configLocation"  
   value 
   = 
   "classpath:conf/MyBatis-config.xml"  
   />
 
  
                
   <!--
 
  
                     <property name="typeAliasesPackage"
 
  
                     value="com.eshore.common.mybatis.test.model" />
 
  
              -->
 
  
                
   <!-- 多个以,分开 ;可以在MyBatis-config.xml中用typeAliases指定具体的别名-->
 
  
                
   <!--
 
  
                     <property name="transactionFactory"> <bean
 
  
                     class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"
 
  
                     /> </property>
 
  
              -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "sqlSessionTemplate"  
   class 
   = 
   "org.mybatis.spring.SqlSessionTemplate"  
   >
 
  
                
   < 
   constructor-arg  
   index 
   =  
   "0"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userMapper "  
   class  
   = 
   "org.mybatis.spring.mapper.MapperFactoryBean" 
   >
 
  
                
   < 
   property  
   name 
   =  
   "mapperInterface"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.UserMapper"  
   />
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactory"  
   ref  
   = 
   "sqlSessionFactory"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  
         
   <!--
 
  
              自动创建包下类名对应的bean Id为类名(第一个字符小写)
 
  
              两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。
 
  
              annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。
 
  
              markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。
 
  
              如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。
 
  
              即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。
 
  
       -->
 
  
         
   < 
   bean  
   class 
   =  
   "org.mybatis.spring.mapper.MapperScannerConfigurer"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "basePackage"  
   value  
   = 
   "com.eshore.common.mybatis.test"  
   />
 
  

 
  
                
   < 
   property  
   name 
   =  
   "annotationClass"
 
  
                       
   value 
   =  
   "com.eshore.common.mybatis.test.mapper.annotation.MyBatisAnnotation"  
   />
 
  
                
   <!--
 
  
                     <property name="markerInterface"
 
  
                     value="com.tiantian.mybatis.mapper.SuperMapper" />
 
  
              -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionFactoryBeanName"  
   value 
   = 
   "sqlSessionFactory"  
   />
 
  
                
   <!-- sqlSessionFactory已废弃 -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "userMapper"  
   ref  
   = 
   "userMapper" 
   ></  
   property 
   >
 
  
         
   </ 
   bean 
   >
 
  

 
  

 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl2"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl2"  
   >
 
  
                
   <!--注入SqlSessionTemplate实例 -->
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
                
   <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
 
  
                
   <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
 
  
         
   </ 
   bean 
   >
 
  

 
  
         
   < 
   bean  
   id 
   =  
   "userServiceImpl3"
 
  
                
   class 
   =  
   "com.eshore.common.mybatis.test.service.impl.UserServiceImpl3"  
   >
 
  
                
   < 
   property  
   name 
   =  
   "sqlSessionTemplate"  
   ref  
   = 
   "sqlSessionTemplate"  
   />
 
  
         
   </ 
   bean 
   >
 
  

 
  
</ 
   beans 
   >






  • 7.测试代码(UserServiceTest.java)


package com.eshore.common.mybatis.test; 
   

 import java.io.IOException; 
   
 import java.io.Reader; 
   
 import java.util.Date; 
   
 import java.util.HashMap; 
   
 import java.util.List; 
   
 import java.util.Map; 
   

 import org.apache.ibatis.io.Resources; 
   
 import org.apache.ibatis.logging.Log; 
   
 import org.apache.ibatis.logging.LogFactory; 
   
 import org.apache.ibatis.session.SqlSession; 
   
 import org.apache.ibatis.session.SqlSessionFactory; 
   
 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
   
 import org.junit.Before; 
   
 import org.junit.Test; 
   
 import org.springframework.context.ApplicationContext; 
   
 import org.springframework.context.support.ClassPathXmlApplicationContext; 
   

 import com.eshore.common.mybatis.test.mapper.UserMapper; 
   
 import com.eshore.common.mybatis.test.model.UserBean; 
   
 import com.eshore.common.mybatis.test.service.IUserService; 
   
 import com.eshore.common.mybatis.test.service.impl.UserServiceImpl4; 
   

 public class UserTest { 
   
      private static final Log logger = LogFactory.getLog(UserTest.class); 
   
      private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder; 
   
      private static SqlSessionFactory sqlSessionFactory; 
   
      private static ApplicationContext context; 
   

      @Before 
   
      public void init() { 
   

           context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
   

      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   
      @Test 
   
      public void testService1() { 
   
           IUserService userService = (IUserService) context 
   
                     .getBean("userServiceImpl"); 
   
           UserBean user = new UserBean(); 
   
           user.setUserName("crm"); 
   
           try { 
   
                List<UserBean> list = (List<UserBean>) userService.queryUsers(user); 
   

                if (null != list && list.size() > 0) { 
   

                     System.out.println("userServiceImpl===" 
   
                               + ((UserBean) list.get(0)).toString()); 
   
                } 
   
           } catch (Exception e) { 
   
                e.printStackTrace(); 
   
           } 
   
           UserBean userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           userBean.setAddress("poson"); 
   
           userService.updateUser(userBean); 
   

           userBean = userService.getUser(2); 
   
           System.out.println(userBean.toString()); 
   

           UserBean newUser = new UserBean(); 
   
           newUser.setAddress("test"); 
   
           newUser.setUserName("crm"); 
   
           newUser.setPassword("aaaaa"); 
   
           newUser.setCreateTime(new Date().toLocaleString()); 
   
           userService.insertUser(newUser); 
   

           UserBean otherUser = new UserBean(); 
   
           otherUser.setUserName("ods"); 
   
           otherUser.setPassword("test"); 
   
           userService.insert(otherUser); 
   

           userService.deleteUser(37070013); 
   
      } 
   

      /** 
   
      * IOC sqlSessionTemplate 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService2() { 
   
           IUserService userService2 = (IUserService) context 
   
                     .getBean("userServiceImpl2"); 
   
           UserBean userBean = userService2.getUser(2); 
   
           System.out.println("userServiceImpl2====" + userBean.toString()); 
   
      } 
   

      /** 
   
      * 
   
      * 
   
      * @param context 
   
      */ 
   

      public void testService3() { 
   
           UserBean userBean = new UserBean(); 
   
           IUserService userService3 = (IUserService) context 
   
                     .getBean("userServiceImpl3"); 
   
           userBean = userService3.getUser(1); 
   
           System.out.println("userServiceImpl3====" + userBean.toString()); 
   
      } 
   

      @Test 
   
      public void TestService4() { // 
   
           UserBean userBean = new UserBean(); 
   
           UserServiceImpl4 userService4 = (UserServiceImpl4) context 
   
                     .getBean("userServiceImpl4"); // // 
   
           userBean.setUserName("serviceImpl4"); 
   
           userService4.insert(userBean); 
   
           System.out.println("userServiceImpl4====" + userBean.toString()); 
   
      } 
   

 }