Because there are several integration methods in the process of spring integrating mybatis, especially the data source. I often see different configuration methods and always feel a little messy, so I have time to summarize them today.
1. Use org.mybatis.spring.mapper.MapperScannerConfigurer
In fact, this is the same way reverse engineering
1. Data source configuration file
2. DAO file
package com.jdd.mapper; import com.jdd.pojo.Employee; import java.util.List; public interface EmployeeMapper { public Employee getEmployeeById(int id); public List<Employee> findAllEmployees(); }3. Mapper.xml file
<?xml version="." encoding="UTF-" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper .//EN" "http://mybatis.org/dtd/mybatis--mapper.dtd"> <mapper namespace="com.jdd.mapper.EmployeeMapper"> <select id="getEmployeeById" parameterType="int" resultType="com.jdd.pojo.Employee"> <![CDATA[ select * from employee where id = #{id}; ]]> </select> <select id="findAllEmployees" resultType="com.jdd.pojo.Employee"> <![CDATA[ select * from employee where status=''; ]]> </select> </mapper>In this way, you can directly inject the dao interface into the service class
package com.jdd.service.impl; import com.jdd.mapper.EmployeeMapper; import com.jdd.pojo.Employee; import com.jdd.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("employeeService") public class EmployeeServiceImpl implements EmployeeService{ @Autowired private EmployeeMapper employeeMapper; @Override public Employee getEmployeeById(int id) { return employeeMapper.getEmployeeById(id); } @Override public List<Employee> findAllEmployees() { return employeeMapper.findAllEmployees(); } }2. Use the abstract class org.mybatis.spring.support.SqlSessionDaoSupport to inject it into the sqlSessionFactory.
1. Data source configuration file
<?xml version="." encoding="UTF-"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w.org//XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-..xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-..xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-..xsd"> <!-- Loading configuration file --> <context:property-placeholder location="classpath:resource/*.properties" /> <!-- Database Connection Pool--> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="" /> <property name="minIdle" value="" /> </bean> <!-- sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:com/jdd/mapper/*.xml"></property> </beans>2. BaseDao class
package com.jdd.dao; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.support.SqlSessionDaoSupport; import javax.annotation.Resource; public abstract class BaseDao extends SqlSessionDaoSupport { @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } }3. Interface EmployeeDao.java class
package com.jdd.dao; import com.jdd.pojo.Employee; import java.util.List; public interface EmployeeDao { Employee getEmployeeById(int id); List<Employee> findAllEmployees(); }4. Dao implements the EmployeeDaoImpl class
package com.jdd.dao.impl; import com.jdd.dao.BaseDao; import com.jdd.dao.EmployeeDao; import com.jdd.pojo.Employee; import org.springframework.stereotype.Repository; import java.util.List; @Repository("employeeDao") public class EmployeeDaoImpl extends BaseDao implements EmployeeDao { @Override public Employee getEmployeeById(int id) { return this.getSqlSession().selectOne("com.jdd.dao.EmployeeDao.getEmployeeById", id); } @Override public List<Employee> findAllEmployees() { return this.getSqlSession().selectList("com.jdd.dao.EmployeeDao.findAllEmployees"); } }5. In this way, you can inject employeeDao into the service class.
3. Use org.mybatis.spring.SqlSessionTemplate template class
1. Data source file
<?xml version="." encoding="UTF-"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w.org//XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-..xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-..xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-..xsd"> <!-- Loading configuration file --> <context:property-placeholder location="classpath:resource/*.properties" /> <!-- Database Connection Pool--> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="" /> <property name="minIdle" value="" /> </bean> <!-- sqlsessionFactory --> <bean id="sqlSessionFactory"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:com/jdd/mapper/*.xml"></property> </bean> <bean id="sqlSessionTemplate"> <constructor-arg index="" ref="sqlSessionFactory"/> </bean> </beans>2. basedao.java class
package com.jdd.dao; import org.mybatis.spring.SqlSessionTemplate; import javax.annotation.Resource; public abstract class BaseDao { public SqlSessionTemplate sqlSessionTemplate; @Resource public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } } 3. Interface EmployeeDao.java class
package com.jdd.dao; import com.jdd.pojo.Employee; import java.util.List; public interface EmployeeDao { Employee getEmployeeById(int id); List<Employee> findAllEmployees(); }4. Dao implements the EmployeeDaoImpl class
package com.jdd.dao.impl; import com.jdd.dao.BaseDao; import com.jdd.dao.EmployeeDao; import com.jdd.pojo.Employee; import org.springframework.stereotype.Repository; import java.util.List; @Repository("employeeDao") public class EmployeeDaoImpl extends BaseDao implements EmployeeDao { @Override public Employee getEmployeeById(int id) { return sqlSessionTemplate.selectOne("com.jdd.dao.EmployeeDao.getEmployeeById", id); } @Override public List<Employee> findAllEmployees() { return sqlSessionTemplate.selectList("com.jdd.dao.EmployeeDao.findAllEmployeees"); } }5. Now you can directly inject employeeDao into the service class to use it.
Note: The injection of basedao here is more flexible. You can also inject SqlSessionFactory, and then create SqlSessionTemplate in the setter method, as follows:
package com.jdd.dao; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import javax.annotation.Resource; public abstract class BaseDao { public SqlSessionTemplate sqlSessionTemplate; @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); } }In fact, whether it is the method of inheriting the SqlSessionDaoSupport class, injecting the sqlSessionFactory, or directly injecting the SqlSessionTemplate, it is essentially the same.
If you inject sqlSessionFactory, it also creates a SqlSessionTemplate through sqlSessionFactory at the bottom, and then operates through its API.
If you don’t believe it, let me see the source code of SqlSessionDaoSupport:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.mybatis.spring.support; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.dao.support.DaoSupport; import org.springframework.util.Assert; public abstract class SqlSessionDaoSupport extends DaoSupport { private SqlSession sqlSession; private boolean externalSqlSession; public SqlSessionDaoSupport() { } public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { if (!this.externalSqlSession) { this.sqlSession = new SqlSessionTemplate(sqlSessionFactory); } } public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSession = sqlSessionTemplate; this.externalSqlSession = true; } public SqlSession getSqlSession() { return this.sqlSession; } protected void checkDaoConfig() { Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"); } }Similarly, SqlSessionTemplate inherits the SqlSession interface, so the effect is the same regardless of operation.
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.mybatis.spring; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.util.List; import java.util.Map; import org.apache.ibatis.exceptions.PersistenceException; import org.apache.ibatis.executor.BatchResult; import org.apache.ibatis.reflection.ExceptionUtil; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.util.Assert; public class SqlSessionTemplate implements SqlSession { private final SqlSessionFactory sqlSessionFactory; private final ExecutorType executorType; private final SqlSession sqlSessionProxy; private final PersistenceExceptionTranslator exceptionTranslator; public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType()); } public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true)); } public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { Assert.notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required"); Assert.notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; this.sqlSessionProxy = (SqlSession)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionTemplate.SqlSessionInterceptor()); } public SqlSessionFactory getSqlSessionFactory() { return this.sqlSessionFactory; } public ExecutorType getExecutorType() { return this.executorType; } public PersistenceExceptionTranslator getPersistenceExceptionTranslator() { return this.exceptionTranslator; } public <T> T selectOne(String statement) { return this.sqlSessionProxy.selectOne(statement); } public <T> T selectOne(String statement, Object parameter) { return this.sqlSessionProxy.selectOne(statement, parameter); } public <K, V> Map<K, V> selectMap(String statement, String mapKey) { return this.sqlSessionProxy.selectMap(statement, mapKey); } public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) { return this.sqlSessionProxy.selectMap(statement, parameter, mapKey); } public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) { return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds); } public <E> List<E> selectList(String statement) { return this.sqlSessionProxy.selectList(statement); } public <E> List<E> selectList(String statement, Object parameter) { return this.sqlSessionProxy.selectList(statement, parameter); } public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) { return this.sqlSessionProxy.selectList(statement, parameter, rowBounds); } public void select(String statement, ResultHandler handler) { this.sqlSessionProxy.select(statement, handler); } public void select(String statement, Object parameter, ResultHandler handler) { this.sqlSessionProxy.select(statement, parameter, handler); } public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { this.sqlSessionProxy.select(statement, parameter, rowBounds, handler); } public int insert(String statement) { return this.sqlSessionProxy.insert(statement); } public int insert(String statement, Object parameter) { return this.sqlSessionProxy.insert(statement, parameter); } public int update(String statement) { return this.sqlSessionProxy.update(statement); } public int update(String statement, Object parameter) { return this.sqlSessionProxy.update(statement, parameter); } public int delete(String statement) { return this.sqlSessionProxy.delete(statement); } public int delete(String statement, Object parameter) { return this.sqlSessionProxy.delete(statement, parameter); } public <T> T getMapper(Class<T> type) { return this.getConfiguration().getMapper(type, this); } public void commit() { throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession"); } public void commit(boolean force) { throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession"); } public void rollback() { throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession"); } public void rollback(boolean force) { throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession"); } public void close() { throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession"); } public void clearCache() { this.sqlSessionProxy.clearCache(); } public Configuration getConfiguration() { return this.sqlSessionFactory.getConfiguration(); } public Connection getConnection() { return this.sqlSessionProxy.getConnection(); } public List<BatchResult> flushStatements() { return this.sqlSessionProxy.flushStatements(); } private class SqlSessionInterceptor implements InvocationHandler { private SqlSessionInterceptor() { } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { SqlSession sqlSession = SqlSessionUtils.getSqlSession(SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator); Object unwrapped; try { Object result = method.invoke(sqlSession, args); if (!SqlSessionUtils.isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) { sqlSession.commit(true); } unwrapped = result; } catch (Throwable var) { unwrapped = ExceptionUtil.unwrapThrowable(var); if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) { SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); sqlSession = null; Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException)unwrapped); if (translated != null) { unwrapped = translated; } } throw (Throwable)unwrapped; } finally { if (sqlSession != null) { SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); } } return unwrapped; } } } }Summarize
The above are several configuration methods for spring integrating data sources in mybatis introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!