日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Guns 企业版多数据源配置集成dynamic-datasource

發(fā)布時間:2024/9/27 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Guns 企业版多数据源配置集成dynamic-datasource 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

          • 一、改造多數(shù)據(jù)源
            • 1. 依賴引入
            • 2. 啟動類添加注解
            • 3. 配置多數(shù)據(jù)源
          • 二、案例實戰(zhàn)
            • 2.1. controller
            • 2.2. service
            • 2.3. impl
            • 2.4. mapper
            • 2.5. xml
          • 三、分頁失效解決方案

一、改造多數(shù)據(jù)源
1. 依賴引入

目前改用dynamic-datasource方式多數(shù)據(jù)源處理,配置如下圖:

<!--置動態(tài)數(shù)據(jù)源--><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.4.1</version></dependency>
2. 啟動類添加注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
3. 配置多數(shù)據(jù)源
########################################## 多數(shù)據(jù)源配置 ############################################ spring:datasource:dynamic:primary: master #設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為masterstrict: false #嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)false. true未匹配到指定數(shù)據(jù)源時拋異常,false使用默認(rèn)數(shù)據(jù)源datasource:master:url: jdbc:mysql://localhost:3306/dbmysql?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0開始支持SPI可省略此配置filters: wall,mergeStatoracle_1:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@192.168.xxx.xxx:1521:orclusername: orclpassword: orclfilters: wall,mergeStat
二、案例實戰(zhàn)

使用規(guī)范:只需要在持久層接口類上添加@DS(“oracle_1”) 注解,里面寫yml配置的數(shù)據(jù)庫別名即可

2.1. controller
/*** 英雄信息表控制器** @author gblfy* @Date 2021-08-13 11:19:00*/ @Controller @RequestMapping("/hero") public class HeroController extends BaseController {private String PREFIX = "/antifraud/hero";@Autowiredprivate HeroService heroService;/*** 查詢列表** @author gblfy* @Date 2021-08-13*/@ResponseBody@RequestMapping("/list")public LayuiPageInfo list(HeroParam heroParam) {return this.heroService.findPageBySpec(heroParam);}
2.2. service
/*** <p>* 英雄信息表 服務(wù)類* </p>** @author gblfy* @since 2021-08-13*/ public interface HeroService extends IService<Hero> {/*** 查詢分頁數(shù)據(jù),Specification模式** @author gblfy* @Date 2021-08-13*/LayuiPageInfo findPageBySpec(HeroParam param);}
2.3. impl
@Service public class HeroServiceImpl extends ServiceImpl<HeroMapper, Hero> implements HeroService {@Autowiredprivate HeroMapper heroMapper;@Overridepublic LayuiPageInfo findPageBySpec(HeroParam param){Page pageContext = getPageContext();IPage page = heroMapper.customPageList(pageContext, param);return LayuiPageFactory.createPageInfo(page);}private Page getPageContext() {return LayuiPageFactory.defaultPage();}}
2.4. mapper
@DS("oracle_1") public interface HeroMapper extends BaseMapper<Hero> {/*** 獲取分頁實體列表** @author gblfy* @Date 2021-08-13*/Page<HeroResult> customPageList(@Param("page") Page page, @Param("paramCondition") HeroParam paramCondition);}
2.5. xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.gblfy.modular.order.mapper.HeroMapper"><!-- 通用查詢映射結(jié)果 --><resultMap id="BaseResultMap" type="com.gblfy.modular.order.entity.Hero"><id column="SNO" property="sno"/><result column="USER_NAME" property="userName"/><result column="AGE" property="age"/></resultMap><!-- 通用查詢結(jié)果列 --><sql id="Base_Column_List">SNOAS "sno", USER_NAME AS "userName", AGE AS "age"</sql><select id="customPageList" resultType="com.gblfy.modular.order.model.result.HeroResult"parameterType="com.gblfy.modular.order.model.request.HeroParam">select<include refid="Base_Column_List"/>from HERO where 1 = 1</select> </mapper>
三、分頁失效解決方案

現(xiàn)象:改造完多數(shù)據(jù)源之后,分頁插件就失效了。
方案:重新加載分頁插件

  • 分頁插件
    MultiDataMyBatisPlusConfig
package com.gblfy.config.datasource;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @author gblfy* @date 2021-08-13*/ @Configuration public class MultiDataMyBatisPlusConfig {/*** 使用多數(shù)據(jù)源后分頁插件失效,攔截器重新調(diào)用分頁插件** @return*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();} }

總結(jié)

以上是生活随笔為你收集整理的Guns 企业版多数据源配置集成dynamic-datasource的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。