The main difference between this example and the above example is that the implementation class of DOA inherits from sqlsessiondaosupport. In fact, even if we do not inherit the sqlsessiondaosupport class, we will define a parent class to provide sqlsession member variables and the corresponding getter/setter. Otherwise, when there are more implementations of DOA, every implementation class writes this process by itself, which also produces a lot of redundant code. The redundant code should be extracted into the parent class, mybatis already provides this class: Org. mybatis. spring. support. sqlsessiondaosupport:
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"); }}
From the implementation of the class, it is a little surprising that only setter (that is, setsqlsessiontemplate () of sqlsessiontemplate is used, while getter is sqlsession (that is, getsqlsession (). In fact, sqlsessiontemplate is sqlsession, because sqlsessiontemplate implements the sqlsession interface, and the above class implementation directly this. sqlsession = sqlsessiontemplate ;.
This setter indicates that during spring integration, the property attribute value in bean configuration must be sqlsessiontemplate (not sqlsession), and getsqlsession () must be used in the DOA implementation class () (because there is no getsqlsessiontemplate ()). Therefore, if you do not like this form of inconsistency, you can simply write a parent class to complete this function.
The Code is the same as most of the above examples. Here, only the files that need to be modified relative to the above example are listed (other files are still the same as the above example, and the following sequence numbers are also consistent with the above example ):
(3) Spring configuration file: COM/mybatis/demo4/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" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <Bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "value =" classpath: COM/mybatis/demo5/MySQL. properties" /> </Bean> <! -- 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} "/> </ bean> <! -- Create sqlsessionfactory. You must specify the data source. The property name must be datasource --> <bean id = "sqlsessionfactory" class = "org. mybatis. spring. sqlsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" configlocation "value =" classpath: COM/mybatis/demo5/mybatis_config.xml "/> </bean> <! -- Use the sqlsession format. You do not need to use mapper again. --> <! -- Create a ER Mapper. The value of the mapperinterface attribute must be an interface class. --> <! -- <Bean id = "usermapper" class = "org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. mybatis. demo5.usermapper "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> --> <! -- Use sqlsession format --> <bean id = "sqlsession" class = "org. mybatis. spring. sqlsessiontemplate "> <constructor-Arg ref =" sqlsessionfactory "/> </bean> <! -- Data Access Dao: sqlsession is used in Dao to query data. Because only the setter method of sqlsessiontemplate is available (the setter method of sqlsession is not available ), therefore, the attribute name must be sqlsessiontemplate --> <bean id = "userdao" class = "com. mybatis. demo5.userdaoimpl "> <property name =" sqlsessiontemplate "ref =" sqlsession "/> </bean> </beans>
(7) DAO implementation class: COM/mybatis/demo2/userdaoimpl. Java
Package COM. mybatis. demo5; import Org. mybatis. spring. support. sqlsessiondaosupport; public class userdaoimpl extends sqlsessiondaosupport implements userdao {// only provides the setter method of sqlsessiontemplate in applicationcontext by inheriting sqlsessiondaosupport. when Dao is configured in XML, the attribute name must be fixed to sqlsessiontemplate @ overridepublic user getuserbyname (string name) {return getsqlsession (). selectone ("com. mybatis. demo5.usermapper. findbyname ", name );}}