spring 整合 mybatis 中数据源的几种配置方式(总结篇)

因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下。

  一、采用org.mybatis.spring.mapper.MapperScannerConfigurer

  其实逆向工程也是这种方式

  1、数据源配配置文件

 2、DAO文件

 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 文件

   <?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>

  这样在service类里就可以直接注入dao接口了

 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();
   }
 }

    二、 采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport, 给它注入 sqlSessionFactory的方式

  1、数据源配置文件

   <?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/aop" xmlns:tx="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/context/spring-context-..xsd
   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">
   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:resource/*.properties" />
   <!-- 数据库连接池 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
     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" class="org.mybatis.spring.SqlSessionFactoryBean">
     <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>
 </beans>

  2、baseDao类

 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、接口 EmployeeDao.java 类

 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实现类 EmployeeDaoImpl

 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、这样就可以在service类里注入 employeeDao了

  三、采用 org.mybatis.spring.SqlSessionTemplate 模板类

  1、数据源文件

 <?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/aop" xmlns:tx="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/context/spring-context-..xsd
   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">
   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:resource/*.properties" />
   <!-- 数据库连接池 -->
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
     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" class="org.mybatis.spring.SqlSessionFactoryBean">
     <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" class="org.mybatis.spring.SqlSessionTemplate">
     <constructor-arg index="" ref="sqlSessionFactory"/>
   </bean>
 </beans>

  2、 basedao.java 类

 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、接口 EmployeeDao.java 类  

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实现类 EmployeeDaoImpl

 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.findAllEmployees");
   }
 }

  5、同样现在也可以在service类里直接注入 employeeDao使用了。

  注:这里basedao的注入比较灵活,也可以注入 SqlSessionFactory, 然后再setter方法里创建 SqlSessionTemplate,如下:

 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);
   }
 }

  其实不管是采用 继承SqlSessionDaoSupport类, 注入 sqlSessionFactory的方式, 还是直接注入 SqlSessionTemplate 的方式, 本质上是一样的。

  如果你采用 注入 sqlSessionFactory的方式, 它在底层也是通过sqlSessionFactory 来创建 SqlSessionTemplate ,然后通过其api来操作。

  不信给你们看下 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");
   }
 }

  同样 SqlSessionTemplate 继承了 SqlSession 接口, 因此不管操作哪个效果都一样

 //
  // 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;
     }
   }
 }

总结

以上所述是小编给大家介绍的spring 整合 mybatis 中数据源的几种配置方式,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

(0)

相关推荐

  • Spring配置多数据源切换

    多数据源切换 db.properties #MySQL jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=admin #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=1000 #定义最大空闲 maxIdle=2

  • spring+Jpa多数据源配置的方法示例

    今天临下班时遇到了一个需求,我的管理平台需要从不同的数据库中获取数据信息,这就需要进行Spring的多数据源配置,对于这种配置,第一次永远都是痛苦的,不过经历了这次的折磨,今后肯定会对这种配置印象深刻.我们这里简单回顾一下流程. 我们配置了两个数据库,一个是公司的数据库,另一个是我本地的一个数据库.首先是application.yml的配置(其中对于公司的数据库我们采取了假的地址,而本机的数据库是真是存在对应的表和库的) 数据库信息: 数据表信息: 1.application.yml datas

  • Spring中配置数据源的几种方式

    无论使用什么形式的Spring DAO支持类,都需要配置数据源的引用.Spring提供了多个选项,用于在Spring程序里配置数据库,其中包括: 1,由JDBC驱动程序定义的数据源. 2,由JNDI查询的数据源. 3,连接池的数据源. 在Spring里,我们可以像使用其他Bean一样来配置一个数据源的引用,并且把它装配到其他的类里. 在配置数据源的时候,其实就是配置一个<bean>节点,指定bean的id,指定bean的class,然后,指定bean的属性,比较通用的属性一般会包括driver

  • Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法

    前言 本文主要介绍的是关于Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法,分享出来供大家参考学习,下面来看看详细的介绍: 实现方法: 数据源在配置文件中的配置 <pre name="code" class="java"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spring

  • SpringBoot使用Druid数据源的配置方法

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池(据说是目前最好的连接池) 一.依赖 为了测试,使用jdbcTemplate <!-- jdbcTemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifa

  • Spring Boot + Mybatis多数据源和动态数据源配置方法

    网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种方式,记录一下配置方法供大家参考. 应用场景 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 多数据源 首先要将spring boot自带的DataSourceAutoConfiguration禁掉,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源.在@SpringBootApplication注解中添加ex

  • Spring Boot 2.0多数据源配置方法实例详解

    两个数据库实例,一个负责读,一个负责写. datasource-reader: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://192.168.43.61:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false username: icbc password: icbc driver-class-na

  • Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议

    一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入springboot并未做spring cloud微服务处理.但随着项目的进一步开发,急需拆分现有业务,做微服务处理.因此架构上的短板日益突出.spring cloud config 无法完全应用,每次项目部署需要修改大量配置文件.严重影响开发效率,因此便萌生了对项目架构再次调整的决心. 三.调整建议 为了

  • 通过Spring Boot配置动态数据源访问多个数据库的实现代码

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中也描述了):只适用于数据库数量不多且固定的情况.针对数据库动态增加的情况无能为力. 下面讲的方案能支持数据库动态增删,数量不限. 数据库环境准备 下面一Mysql为例,先在本地建3个数据库用于测试.需要说明的是本方案不限数据库数量,支持不同的数据库部署在不同的服务器上.如图所示db_project_001.d

  • springboot v2.0.3版本多数据源配置方法

    本篇分享的是springboot多数据源配置,在从springboot v1.5版本升级到v2.0.3时,发现之前写的多数据源的方式不可用了,捕获错误信息如: 异常:jdbcUrl is required with driverClassName. 先来说下之前的多数据源配置如: spring: datasource: url: jdbc:sqlserver://192.168.122.111;DatabaseName=flight username: sa password: 1234.abc

随机推荐