激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring 整合 mybatis 中數據源的幾種配置方式(總結篇)

spring 整合 mybatis 中數據源的幾種配置方式(總結篇)

2021-04-26 14:43海小鑫 Java教程

因為spring 整合mybatis的過程中, 有好幾種整合方式,尤其是數據源那塊,經常看到不一樣的配置方式,總感覺有點亂,所以今天有空總結下,感興趣的朋友跟隨腳本之家小編一起學習吧

因為spring 整合mybatis的過程中, 有好幾種整合方式,尤其是數據源那塊,經常看到不一樣的配置方式,總感覺有點亂,所以今天有空總結下。

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

  其實逆向工程也是這種方式

  1、數據源配配置文件

 2、dao文件

?
1
2
3
4
5
6
7
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 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <?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接口了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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、數據源配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <?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類

?
1
2
3
4
5
6
7
8
9
10
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 類

?
1
2
3
4
5
6
7
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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、數據源文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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 類

?
1
2
3
4
5
6
7
8
9
10
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 類  

?
1
2
3
4
5
6
7
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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,如下:

?
1
2
3
4
5
6
7
8
9
10
11
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 的源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// 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 接口, 因此不管操作哪個效果都一樣

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
 // 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 中數據源的幾種配置方式,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

原文鏈接:https://www.cnblogs.com/xiexin2015/archive/2018/05/06/8998331.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩色 | 热99在线视频| www.com国产精品 | 国产精品视频一区二区三区四区五区 | 韩毛片 | 国产午夜精品久久久久久免费视 | 欧美性受ⅹ╳╳╳黑人a性爽 | 色综合视频 | 亚洲国产精品久久久 | 亚洲射逼 | 久久久久.com | 国产亚洲精品久久久久久久久久 | 亚洲性综合网 | 视频在线亚洲 | 欧美一级网 | 日本免费成人网 | 久久久国产精品视频 | 亚洲福利在线免费观看 | 成人影片在线免费观看 | 一区二区三区日韩在线 | 久久人人av | 亚洲精品成人悠悠色影视 | 国产精品视频久久久 | 欧美成人一级 | 男人的天堂视频网站 | 国产精品午夜在线 | 国产一级性生活视频 | 在线观看免费视频麻豆 | 蜜桃欧美性大片免费视频 | 澳门一级淫片免费视频 | 91精品国产一区二区三区四区在线 | 国产高潮国产高潮久久久91 | 午夜视频在线看 | 在线播放黄色网址 | 越南一级黄色片 | 成人18网站| 成人午夜网址 | 99影视电影电视剧在线播放 | 日韩在线观看视频免费 | 一级免费特黄视频 | 国av在线 |