当前位置: 移动技术网 > IT编程>开发语言>Java > spring 整合 mybatis 中数据源的几种配置方式(总结篇)

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

因为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 中数据源的几种配置方式,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网