mybatis-spring從1.1升級到1.2所帶來的dao層級的編寫問題

咱們公司的項目使用spring+mybatis組合。因此就必須得使用mybatis-spring了。因此此處就昨日mybatis-spring從1.1升級到1.2所帶來的dao層級的編寫問題,作了一個總結。java

咱們能夠先來看看mybatis-spring框架的1.1.1版本中關於SqlSessionDaoSupport的代碼吧:spring

package org.mybatis.spring.support;

import static org.springframework.util.Assert.*;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;

/**
 * Convenient super class for MyBatis SqlSession data access objects.
 * It gives you access to the template which can then be used to execute SQL methods.
 * <p>
 * This class needs a SqlSessionTemplate or a SqlSessionFactory.
 * If both are set the SqlSessionFactory will be ignored.
 *
 * @see #setSqlSessionFactory
 * @see #setSqlSessionTemplate
 * @see SqlSessionTemplate
 * @version $Id: SqlSessionDaoSupport.java 4885 2012-03-12 09:58:54Z simone.tripodi $
 */
public abstract class SqlSessionDaoSupport extends DaoSupport {

  private SqlSession sqlSession;

  private boolean externalSqlSession;

  @Autowired(required = false)
  public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    if (!this.externalSqlSession) {
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
    }
  }

  @Autowired(required = false)
  public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSession = sqlSessionTemplate;
    this.externalSqlSession = true;
  }

  /**
   * Users should use this method to get a SqlSession to call its statement methods
   * This is SqlSession is managed by spring. Users should not commit/rollback/close it
   * because it will be automatically done.
   *
   * @return Spring managed thread safe SqlSession
   */
  public final SqlSession getSqlSession() {
    return this.sqlSession;
  }

  /**
   * {@inheritDoc}
   */
  protected void checkDaoConfig() {
    notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
  }

}

  從上面的源碼能夠看出:在方法setSqlSessionFactory和setSqlSessionTemplate方法上面都標註有:「@Autowired(required = false)」這樣的註解。sql

因此咱們在編寫dao層級代碼的時候只須要dao直接繼承SqlSessionDaoSupport,並標註註解@Repository,而後就可使用相似的getSqlSession().selectList("User.selectUsers");這樣的方法來使用它了,並且在spring的配置文件中的配置也比較少:apache

 <tx:annotation-driven transaction-manager="txManager"
                          proxy-target-class="true"/>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

  

  可是升級到1.2以後,咱們看看SqlSessionDaoSupport的源代碼:session

public abstract class SqlSessionDaoSupport extends DaoSupport {

  private SqlSession sqlSession;

  private boolean externalSqlSession;

  public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    if (!this.externalSqlSession) {
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
    }
  }

  public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSession = sqlSessionTemplate;
    this.externalSqlSession = true;
  }

  /**
   * Users should use this method to get a SqlSession to call its statement methods
   * This is SqlSession is managed by spring. Users should not commit/rollback/close it
   * because it will be automatically done.
   *
   * @return Spring managed thread safe SqlSession
   */
  public SqlSession getSqlSession() {
    return this.sqlSession;
  }

  /**
   * {@inheritDoc}
   */
  protected void checkDaoConfig() {
    notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
  }

}

  

  從上面的源碼能夠看出:在方法setSqlSessionFactory和setSqlSessionTemplate方法上面如今都沒有標註有:「@Autowired(required = false)」這樣的註解。mybatis

若是一些系統直接從mybatis-spring1.1.1升級到1.2版本的時候,就會出現問題。框架

在1.2版本下面有幾種方式來使用:ide

第一種,基於註解:ui

@Repository
public class UserDao extends SqlSessionDaoSupport{
    public List<User> userList() {
        return getSqlSession().selectList("User.selectUsers");
    }

    @Override
    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
}

  

  咱們本身重寫set方法就能夠了。在這種狀況下spring的配置文件不須要修改。這個實例是隨意寫的,若是你的工程中dao類不少(絕大多數狀況都是),這樣你就能夠編寫一個BaseDao,而後在這個BaseDao中重寫這個方法,其餘的dao只須要繼承這個BaseDao就能夠了。this

第二章基於xml文件配置:

public class UserDao extends SqlSessionDaoSupport {
    public List<User> userList() {
        return getSqlSession().selectList("User.selectUsers");
    }
}

  

  可是須要在spring的配置文件中增長這個UserDao的配置:

    <bean id="userDao" class="com.xxx.paginator.dao.UserDao">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

  

  第一種基於註解的配置,好處是不須要編寫xml,可是這種比較容易侵入業務邏輯。

     第二種基於xml配置,好處是不侵入業務邏輯,可是當dao的數量不少的時候,須要在xml中配置好多。

     因此最後具體選擇哪一種,你們能夠結合本身的狀況。

相關文章
相關標籤/搜索