Our company's projects use Spring+mybatis combination. So we have to use the mybatis-spring. So here's a summary of the DAO-level writing problem that mybatis-spring from 1.1 to 1.2 yesterday.
Let's take a look at the code for Sqlsessiondaosupport in the 1.1.1 version of the mybatis-spring framework:
1 PackageOrg.mybatis.spring.support;2 3 Import Staticorg.springframework.util.assert.*;4 5 Importorg.apache.ibatis.session.SqlSession;6 Importorg.apache.ibatis.session.SqlSessionFactory;7 Importorg.mybatis.spring.SqlSessionTemplate;8 Importorg.springframework.beans.factory.annotation.Autowired;9 ImportOrg.springframework.dao.support.DaoSupport;Ten One /** A * Convenient Super class for MyBatis sqlsession data Access objects. - * It gives you access to the template which can and be used to execute SQL methods. - * <p> the * This class needs a sqlsessiontemplate or a sqlsessionfactory. - * If Both is set the sqlsessionfactory would be ignored. - * - * @see#setSqlSessionFactory + * @see#setSqlSessionTemplate - * @seesqlsessiontemplate + * @version$Id: Sqlsessiondaosupport.java 4885 2012-03-12 09:58:54z Simone.tripodi $ A */ at Public Abstract classSqlsessiondaosupportextendsDaosupport { - - Privatesqlsession sqlsession; - - Private Booleanexternalsqlsession; - in@Autowired (required =false) - Public Final voidsetsqlsessionfactory (sqlsessionfactory sqlsessionfactory) { to if(! This. Externalsqlsession) { + This. sqlsession =Newsqlsessiontemplate (sqlsessionfactory); - } the } * $@Autowired (required =false)Panax Notoginseng Public Final voidsetsqlsessiontemplate (sqlsessiontemplate sqlsessiontemplate) { - This. sqlsession =sqlsessiontemplate; the This. externalsqlsession =true; + } A the /** + * Users should use the This method to get a sqlsession to call its statement methods - * This is sqlsession are managed by spring. Users should not commit/rollback/close it $ * because it 'll be is automatically done. $ * - * @returnSpring Managed thread safe sqlsession - */ the Public Finalsqlsession getsqlsession () { - return This. sqlsession;Wuyi } the - /** Wu * {@inheritDoc} - */ About protected voidCheckdaoconfig () { $Notnull ( This. Sqlsession, "Property ' sqlsessionfactory ' or ' sqlsessiontemplate ' is required"); - } - -}
From the above source can be seen: in the method Setsqlsessionfactory and Setsqlsessiontemplate methods are marked with: "@Autowired (required = false)" such annotations.
So when we write the DAO level code, we only need DAO to inherit sqlsessiondaosupport directly, and annotate annotation @repository, then we can use similar getsqlsession (). SelectList (" User.selectusers "); This is the way to use it, and there are fewer configurations in the spring configuration file:
1<tx:annotation-driven transaction-manager= "Txmanager"2proxy-target-class= "true"/>3 4<bean id= "Txmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >5<property name= "DataSource" ref= "DataSource"/>6</bean>7 8<bean id= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean" >9<property name= "DataSource" ref= "DataSource"/>Ten<property name= "configlocation" value= "Classpath:mybatis-config.xml"/> One</bean>
But after upgrading to 1.2, let's look at the source code for Sqlsessiondaosupport:
1 Public Abstract classSqlsessiondaosupportextendsDaosupport {2 3 Privatesqlsession sqlsession;4 5 Private Booleanexternalsqlsession;6 7 Public voidsetsqlsessionfactory (sqlsessionfactory sqlsessionfactory) {8 if(! This. Externalsqlsession) {9 This. sqlsession =Newsqlsessiontemplate (sqlsessionfactory);Ten } One } A - Public voidsetsqlsessiontemplate (sqlsessiontemplate sqlsessiontemplate) { - This. sqlsession =sqlsessiontemplate; the This. externalsqlsession =true; - } - - /** + * Users should use the This method to get a sqlsession to call its statement methods - * This is sqlsession are managed by spring. Users should not commit/rollback/close it + * because it 'll be is automatically done. A * at * @returnSpring Managed thread safe sqlsession - */ - Publicsqlsession getsqlsession () { - return This. sqlsession; - } - in /** - * {@inheritDoc} to */ + protected voidCheckdaoconfig () { -Notnull ( This. Sqlsession, "Property ' sqlsessionfactory ' or ' sqlsessiontemplate ' is required"); the } * $}
From the above source can be seen: in the method Setsqlsessionfactory and Setsqlsessiontemplate methods are not labeled Now: "@Autowired (required = false)" such annotations.
Problems can occur if some systems are upgraded directly from mybatis-spring1.1.1 to version 1.2.
There are several ways to use it under version 1.2:
From the above source can be seen: in the method Setsqlsessionfactory and Setsqlsessiontemplate methods are not labeled Now: "@Autowired (required = false)" such annotations.
Problems can occur if some systems are upgraded directly from mybatis-spring1.1.1 to version 1.2.
There are several ways to use it under version 1.2:
1 @Repository2 Public classUserdaoextendssqlsessiondaosupport{3 PublicList<user>userlist () {4 returnGetsqlsession (). SelectList ("User.selectusers");5 }6 7 @Override8 @Autowired9 Public voidsetsqlsessionfactory (sqlsessionfactory sqlsessionfactory) {Ten Super. Setsqlsessionfactory (sqlsessionfactory); One } A}
We can rewrite the set method ourselves. In this scenario, the spring configuration file does not need to be modified. This example is arbitrary, if you have a lot of DAO classes in your project (most of the cases are), so you can write a Basedao, and then rewrite the method in this Basedao, the other DAO only need to inherit this Basedao.
The second chapter is based on the XML file configuration:
1 Public class extends Sqlsessiondaosupport {2 Public List<user> userlist () {3 return getsqlsession (). SelectList (" User.selectusers "); 4 }5 }
However, this Userdao configuration needs to be added to the spring configuration file:
1 class= "Com.qunar.corp.paginator.dao.UserDao" >2 <property name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/>3 </bean>
The first annotation-based configuration has the advantage of not having to write XML, but this comparison is easy to hack into business logic.
The second, XML-based configuration, has the advantage of not intruding into business logic, but when there is a large number of DAO, it needs to be configured in XML.
So the final specific choice of which, we can combine their own situation.
Mybatis-spring a DAO-level authoring issue from upgrading from 1.1 to 1.2