What are the integration methods of Spring and Mybatis

  • 2020-04-01 04:18:48
  • OfStack

This paper mainly introduces three commonly used integration methods of Spring and Mybatis. The needed integration framework package is Mybatis -spring.jar, which can be linked
(link: http://code.google.com/p/mybatis/) to download.

  1. MapperFactoryBean is adopted to provide corresponding SQL statements and input parameters through annotation instead of writing mybatis mapping file.

  (1) Spring configuration file:


 <!--  The introduction of jdbc The configuration file  -->   
 <context:property-placeholder location="jdbc.properties"/>     
 <!-- create jdbc The data source  -->    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
 <property name="driverClassName" value="${driver}"/>    
 <property name="url" value="${url}"/>     
 <property name="username" value="${username}"/>     
 <property name="password" value="${password}"/>     
<property name="initialSize" value="${initialSize}"/>     
<property name="maxActive" value="${maxActive}"/>    
  <property name="maxIdle" value="${maxIdle}"/>     
<property name="minIdle" value="${minIdle}"/>    
 </bean>    
 <!--  create SqlSessionFactory , and specify the data source -->    
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
 <property name="dataSource" ref="dataSource" />     
</bean>     
 <!-- Create a data mapper, which must be an interface --> 
   <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
 <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />    
 <property name="sqlSessionFactory" ref="sqlSessionFactory" />    
 </bean>     
 <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">  
 <property name="userMapper" ref="userMapper"/> 
 </bean> 

(2) data mapper UserMapper, the code is as follows:


public interface UserMapper {   
  @Select("SELECT * FROM user WHERE id = #{userId}")    
 User getUser(@Param("userId") long id);  
 } 

(3)dao interface class UserDao, the code is as follows:


public interface UserDao {  
  public User getUserById(User user); 
 } 

(4)dao implementation class UserDaoImpl2, the code is as follows:


public class UserDaoImpl2 implements UserDao {  
   private UserMapper userMapper;   
   public void setUserMapper(UserMapper userMapper) {    
   this.userMapper = userMapper;   
  }     
  public User getUserById(User user) {   
   return userMapper.getUser(user.getId());   
  } 
 } 

2, using interface org. Apache. Ibatis. Session. The SqlSession implementation class org. Mybatis. Spring. SqlSessionTemplate.

      Mybatis, sessionFactory can be made of the SqlSessionFactoryBuilder is. To create.

In MyBatis-Spring, SqlSessionFactoryBean is used instead.
SqlSessionFactoryBean has a mandatory property dataSource and a generic property configLocation (which specifies the XML configuration file path for mybatis).

    (1) Spring configuration file:

< ! -- create SqlSessionFactory and specify data source -->  
< SqlSessionFactory bean id = "" class =" org. Mybatis. Spring. SqlSessionFactoryBean ">        
  < The property name = "dataSource" ref = "dataSource" / >        
  < ! -- specify the sqlMapConfig general configuration file, the custom environment is not in effect in the spring container -->    
  < Property  Name = "configLocation"   "Value =" classpath: sqlMapConfig. XML / >    
  < ! -- specify entity class mapping file, you can specify all configuration files under a certain package and subpackage at the same time, mapperLocations and configLocation can be one, when it is necessary to specify an alias for the entity class, you can specify the configLocation attribute, and then use mapper to introduce entity class mapping file in mybatis general configuration file -->  
  < ! - < Property  Name = "mapperLocations"   Value = "classpath * : com/XXT ibatis/DBCP / * * / *. XML" / >   - >
  < Bean>

  (2)mybatis general configuration file sqlmapconfig.xml:


<configuration>  
 <typeAliases>   
 <typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" />  
 </typeAliases>   
<mappers>   
 <mapper resource="com/xxt/ibatis/dbcp/domain/user.map.xml" />
 </mappers> 
 </configuration> 

(3) entity class mapping file user.map.xml:


<mapper namespace="com.xxt.ibatis.dbcp.domain.User">    
 <resultMap type="User" id="userMap">     
 <id property="id" column="id" />    
  <result property="name" column="name" />    
  <result property="password" column="password" />   
   <result property="createTime" column="createtime" />   
  </resultMap>   
  <select id="getUser" parameterType="User" resultMap="userMap">   
   select * from user where id = #{id}    
</select> 
 <mapper/> 

  (4)dao layer interface implementation class UserDaoImpl:

Java code


public class UserDaoImpl implements UserDao { 
  public SqlSessionTemplate sqlSession;  
   public User getUserById(User user) {   
   return (User)sqlSession.selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user); 
   } 
  public void setSqlSession(SqlSessionTemplate sqlSession) {    
  this.sqlSession = sqlSession;   } 
 } 

3, using an abstract class org. Mybatis. Spring. Support. Provide SqlSession SqlSessionDaoSupport.

    (1)spring configuration file:

Java code


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   
 <property name="dataSource" ref="dataSource" />   
 <property name="configLocation" value="classpath:sqlMapConfig.xml"/>   
 <!-- <property name="mapperLocations" value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/  > -->  
</bean>  
 <bean id="sqlSession"   class="org.mybatis.spring.SqlSessionTemplate">     
 <constructor-arg index="0" ref="sqlSessionFactory" />  
 </bean>  
 <bean id="userDaoImpl3" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3">   
 <!-- injection SqlSessionTemplate The instance  -->   
<property name="sqlSessionTemplate" ref="sqlSession" />   
 <!-- It can also be injected directly SqlSessionFactory Instance, when both are specified, SqlSessionFactory failure  -->   
 <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />  --> 
 </bean> 

    (2) dao layer interface implementation class UserDaoImpl3:

Java code


public class UserDaoImpl3 extends SqlSessionDaoSupport implements UserDao { 
  public User getUserById(User user) {   
  return (User) getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);   
} 
 } 

Related articles: