SpringBoot實戰(三):Mybatis配置多數據源

2020-09-15 hank

【前言】

最近接到一個新需求,經過分析後做了相應的設計;其中需要在一個項目中操做不同的數據源;於是進行了相關驗證;在此記錄一下驗證過程。

【實戰多數據源】

一、Pom中引入相應的Jar包

<!-- mysql 基礎服務--><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version></dependency><!-- mybatis --><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version></dependency>

二、主要代碼展示

1、application.properties增加資料庫連接的相關配置

****************************訂單資料庫***************************order.datasource.url=jdbc:mysql://127.0.0.1:3306/zh_order?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8order.datasource.username=rootorder.datasource.password=rootorder.datasource.connectionTimeout=30000order.datasource.idleTimeout=600000order.datasource.maxLifetime=1800000order.datasource.maximumPoolSize=10order.datasource.minimumIdle=5

2、啟動類中排除DataSourceAutoConfiguration和MybatisAutoConfiguration

package com.zhanghan.zhboot;import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class})public class ZhBootApplication { public static void main(String[] args) { SpringApplication.run(ZhBootApplication.class, args); }}

3、資料庫屬性讀取類(以UserDataSourceProperties為例)

package com.zhanghan.zhboot.properties;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@Data@ConfigurationProperties(prefix = &34;)public class UserDataSourceProperties { private String url; private String username; private String password; private Integer connectionTimeout; private Integer idleTimeout; private Integer maxLifetime; private Integer maximumPoolSize; private Integer minimumIdle;}

4、數據源配置器(以UserDataSourceConfig為例)---註:資料庫連接池用的是springboot2.0後默認的hikariCP(性能高)

package com.zhanghan.zhboot.config;import com.zaxxer.hikari.HikariConfig;import com.zaxxer.hikari.HikariDataSource;import com.zhanghan.zhboot.properties.UserDataSourceProperties;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration// 掃描 Mapper 接口並容器管理@MapperScan(basePackages = UserDataSourceConfig.PACKAGE, sqlSessionFactoryRef = &34;)public class UserDataSourceConfig { static final String PACKAGE = &34;; @Autowired private UserDataSourceProperties userDataSourceProperties; @Bean(name = &34;) public DataSource userDataSource() { HikariConfig config = new HikariConfig(); config.setJdbcUrl(userDataSourceProperties.getUrl()); config.setUsername(userDataSourceProperties.getUsername()); config.setPassword(userDataSourceProperties.getPassword()); config.setConnectionTestQuery(&34;); config.setConnectionTimeout(userDataSourceProperties.getConnectionTimeout()); config.setIdleTimeout(userDataSourceProperties.getIdleTimeout()); config.setMaxLifetime(userDataSourceProperties.getMaxLifetime()); config.setMaximumPoolSize(userDataSourceProperties.getMaximumPoolSize()); config.setMinimumIdle(userDataSourceProperties.getMinimumIdle()); return new HikariDataSource(config); } @Bean(name = &34;) public DataSourceTransactionManager userTransactionManager() { return new DataSourceTransactionManager(userDataSource()); } @Bean(name = &34;) public SqlSessionFactory userSqlSessionFactory( @Qualifier(&34;) DataSource userDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(userDataSource); return sessionFactory.getObject(); }}

5、相應的mapper文件

package com.zhanghan.zhboot.mybatis.mapper.user;import com.zhanghan.zhboot.mybatis.entity.UserInfoEntity;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Result;import org.apache.ibatis.annotations.Results;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.type.JdbcType;@Mapperpublic interface UserInfoMapper { @Select({ &34;, &34;, &34;, &34;, &{mobile,jdbcType=VARCHAR}&34;limit 1&34;id&34;id&34;no&34;no&34;mobile&34;mobile&34;name&34;name&34;sex&34;sex&34;create_time&34;createTime&34;update_time&34;updateTime&34;param1&34;param1&34;param2&34;param2&34;param3&34;param3&34;param4&34;param4&34;param5&34;param5&34;param6&34;param6&34;/get/order/borrow", method = RequestMethod.POST) public Object getOrderBorrow(@RequestBody OrderRequest orderRequest) { return orderService.findOrder(orderRequest); }}

三、效果圖

五、項目地址及代碼版本

1、地址:https://github.com/dangnianchuntian/springboot

2、代碼版本:1.0.0-Release

【總結】

1、實踐出真知;

2、技術是為業務服務;

相關焦點

  • MyBatis初級實戰之四:druid多數據源
    GitHubhttps://github.com/zq2599/blog_demos內容:所有原創文章分類匯總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;關於druid多數據源
  • 基於SpirngBoot2.0+ 的 SpringBoot+Mybatis 多數據源配置
    但是往往隨著業務量發展,我們通常會進行資料庫拆分或是引入其他資料庫,從而我們需要配置多個數據源。下面基於 SpringBoot+Mybatis ,帶著大家看一下 SpringBoot 中如何配置多數據源。
  • MyBatis初級實戰之三:集成druid
    初級實戰》系列的第三篇,我們將實戰springboot、mybatis、druid的集成,並驗證,由以下內容組成:新建springboot工程,裡面有詳細的集成druid的操作;編寫和執行單元測試代碼,並規避一個由集成druid帶來的問題;啟動springboot應用,通過swagger驗證基本功能正常;
  • SpringBoot+Mybatis+druid 多數據源配置
    其實配置相當簡單,僅供學習參考,如有不足請賜教。數據源(一)DruidDatasourceConfig.java(二)DruidDatasource2Config.javapackage com.huifu.myeinsole.dal;import com.alibaba.druid.pool.DruidDataSource;import com.github.pagehelper.PageInterceptor;import org.apache.ibatis.plugin.Interceptor
  • SpringBoot 的多數據源配置
    多數據源SpringBoot 的多數據源開發十分簡單,如果多個數據源的資料庫相同,比如都是 MySQL,那麼依賴是不需要任何改動的,只需要進行多數據源配置即可。如果你新增的資料庫數據源和目前的資料庫不同,記得引入新資料庫的驅動依賴,比如 MySQL 和 PGSQL。
  • springboot整合mybatis實現配置多數據源
    前言:實際開發中,隨著業務的擴張,使用單一的資料庫顯然有點臃腫,不便管理,經常會實現將不同的業務模塊的數據表結構放在各自的資料庫中,下邊簡單實現sprongboot實現多數據源的配置。一 項目結構:二 涉及到的資料庫:三 springboot中的application.properties文件:本次demo簡單配置下兩個數據源為例。
  • 單手擼了個springboot mybatis druid
    在這裡解釋一下為什麼是springboot+mybatis+druid,是因為作者認為但凡任何一個有靈魂的項目,都少不了資料庫,作者不喜歡用JPA那種混SQL的語法,因此選了mybatis,而Druid是阿里系(真香~)的一種資料庫連接池框架,在上一個項目作者用的屢試不爽,因此打算繼續用
  • 實戰:springboot整合Mybatis-plus
    ://mybatis.plus/guide/),MP只做增強不做改變,引入它不會對現有工程產生影響,如絲般順滑。>按下圖步驟,創建一個springboot項目。配置如下。 配置數據源  datasource:    url: jdbc:mysql://localhost:3306/db1?
  • SpringBoot配置數據源及MyBatis分頁的要點
    ,容易出現分頁無效的問題,這種情況常見於自定義數據源的情況。無論哪一種,都需要為數據源設置分頁插件配置。如果使用 SpringBoot 自帶的數據源,則不需要手動設置分頁插件,只需要提供分頁插件配置類即可;如果是自定義的數據源,則必須為其配置分頁插件。
  • SpringBoot多數據源配置詳解
    /mapper/oracle/*.xml #slave數據源對應mapper文件---# jpa相關的配置master: jpa: repos: com.pack.base.repository #master數據源對應的包配置 domain: com.pack.domain #master對應的實體包slave: jpa: repos: com.pack.slave.repository
  • 集成Redis、mybatis、springboot
    這就不多說了。這兒我們就不用jedis了,spring對redis也有支持,我們就用spring-boot-starter-data-redis來整合redis。spring: #redis 的配置 redis: host: localhost port: 6379 database: 0# 數據源,我用了druid datasource: type: com.alibaba.druid.pool.DruidDataSource password: 12345678 username
  • 一步步分析springboot啟動機制(starter機制)
    而用springboot後,一切都變得很簡便快速。下來我們來一步步分析springboot的起步依賴與自動配置這兩個核心原理。2、配置數據源,但是還沒有看到springboot是如何讀取yml或者properites配置文件的的屬性來創建數據源的?
  • 通關 MyBatis 實戰 (下篇)
    XML 配置文件的,但隨著 Java 的發展(Java 1.5 開始引入註解)和 MyBatis 自身的迭代升級,終於在 MyBatis 3 之後就開始支持基於註解的開發了。-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --›‹dependency›    ‹groupId›org.mybatis.spring.boot‹/groupId›    
  • SpringBoot集成Mybatis
    --mybatis集成SpringBoot框架起步依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId
  • mybatis最全教程之springboot集成mybatis
    經過前面的教程,相信您對mybatis有了比較深刻的認識和了解,我們看看在實際生產中怎麼使用mybatis來提高工作效率。那麼首先我們要做的就是與spring進行集成。本節課使用springboot與mybatis進行集成。
  • springBoot多數據源的yml配置+指定數據源
    接上一篇yml配置druid: type: com.alibaba.druid.pool.DruidDataSource user: url: jdbc:mysql://localhost:3306/user?
  • springboot整合mybatis
    本篇文章來手把手帶進行springboot與mybatis的整合,springboot的版本我選擇的是2.2.2.RELEASE,mybatis的是選擇mybatis-spring-boot-starter的一個組件,資料庫我選擇的是mysql,為了方便,我這裡省略了service層,也及是業務處理層
  • SpringBoot實戰(十一):多文件上傳異常解決方案
    .io.IOException: The temporary upload location[/tmp/tomcat.53876517222872.8078/work/Tomcat/localhost/ROOT] is not valid 二、問題解決思路增加上傳的配置類
  • 從零搭建SpringBoot+MyBatis+MySQL
    目錄創建工程了解MVC模型屬性配置文件.ymlspringboot裡的資料庫布局創建包創建數據表創建實體類DAO(model)創建mapper語句直接寫在)、mybatis(持久層)、mysql(資料庫驅動)
  • springboot+mybatis+SpringSecurity實現用戶角色資料庫管理(一)
    使用springboot+mybatis+SpringSecurity 實現用戶權限資料庫管理實現用戶和角色用資料庫存儲,而資源(url)和權限的對應採用硬編碼配置。 也就是角色可以訪問的權限通過硬編碼控制。