springboot集成mybatis?plus和dynamic-datasource注意事項說明
springboot集成mybatis plus和dynamic-datasource注意事項
環(huán)境
- spring-boot-starter-parent 1.5.2.RELEASE
- mybatis-plus-boot-starter 2.x
- dynamic-datasource-spring-boot-starter 2.5.0
- druid-spring-boot-starter 1.1.10
注意事項
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
dynamic所有版本默認啟用stat和wall過濾器(默認不支持批量執(zhí)行sql, 并且有些低版本無法自定義)
開啟批量執(zhí)行sql的方法
# 方法1:升級版本, 如2.5.0 spring: ? datasource: ? ? dynamic: ? ? ? druid:? ? ? ? ? wall: ? ? ? ? ? noneBaseStatementAllow: true ? ? ? ? ? multiStatementAllow: true # 方法2:移除wall過濾器 spring: ? datasource: ? ? dynamic: ? ? ? druid:? ? ? ? ? filters: stat
現(xiàn)有項目集成mybatis plus時,應(yīng)指定另外的枚舉包,否則會出問題
mybatis-plus: ? type-enums-package: com.zxkj.demo.enums.mp
springboot mybatis plus多數(shù)據(jù)源配置整合dynamic-datasource
pro文件引入依賴
? ? ? ?<dependency> ? ? ? ? ? ? <groupId>mysql</groupId> ? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId> ? ? ? ? ? ? <scope>runtime</scope> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.alibaba</groupId> ? ? ? ? ? ? <artifactId>druid-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>1.2.6</version> ? ? ? ? </dependency> ? ? ? ? <!--mybatis-plus--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>mybatis-plus-boot-starter</artifactId> ? ? ? ? ? ? <version>3.1.2</version> ? ? ? ? </dependency> ? ? ? ? <!--主從配置依賴--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>dynamic-datasource-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>2.5.6</version> ? ? ? ? </dependency> ? ? ? ? <!--lombok用來簡化實體類:需要安裝lombok插件--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.projectlombok</groupId> ? ? ? ? ? ? <artifactId>lombok</artifactId> ? ? ? ? ? ? <version>1.18.20</version> ? ? ? ? </dependency>
application.yml配置
spring: ? datasource: ? ? dynamic: ? ? ? primary: master #設(shè)置默認數(shù)據(jù)源或數(shù)據(jù)源組,master默認值(數(shù)據(jù)源名稱可以隨意起名,沒有固定值,eg:db1,db2) ? ? ? strict: false #設(shè)置嚴格模式,默認false不啟動. 啟動后在未匹配到指定數(shù)據(jù)源時候回拋出異常,不啟動會使用默認數(shù)據(jù)源. ? ? ? datasource: ? ? ? ? master: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://192.168.3.220:3306/mchouse_test1?useUnicode=true&characterEncoding=utf-8 ? ? ? ? ? username: ***** ? ? ? ? ? password: ***** ? ? ? ? slave_1: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://112.30.184.149:3306/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai ? ? ? ? ? username: ***** ? ? ? ? ? password: ***** ? ? ? ? slave_2: ? ? ? ? ? driver-class-name: com.mysql.cj.jdbc.Driver ? ? ? ? ? url: jdbc:mysql://120.55.168.100:33066/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai ? ? ? ? ? username: ***** ? ? ? ? ? password: ***** mybatis-plus: # ?configuration: # ? ?log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #org.apache.ibatis.logging.slf4j.Slf4jImpl ? mapper-locations: classpath:mapper/*.xml #配置mybatis.xml文件路勁 classpath根路徑 ? global-config: ? ? # 邏輯刪除配置 ? ? db-config: ? ? ? # 刪除后 ? ? ? logic-delete-value: 1 ? ? ? # 刪除前 ? ? ? logic-not-delete-value: 0
修改Application啟動類
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})這里要排除DruidDataSourceAutoConfigure ,因為DruidDataSourceAutoConfigure會注入一個DataSourceWrapper,其會在原生的spring.datasource下找url,username,password等。而我們動態(tài)數(shù)據(jù)源的配置路徑是變化的。
創(chuàng)建MybatisPlusConfig
@Configuration
@EnableTransactionManagement
@MapperScan("com.example.md5_demo.com.db.**.mapper")
public class MyBatisPlusConfig {
? ? /**
? ? ?* SQL 執(zhí)行性能分析插件
? ? ?* 開發(fā)環(huán)境使用,線上不推薦。 maxTime 指的是 sql 最大執(zhí)行時長
? ? ?*/
? ? @Bean
? ? @Profile({"dev","test"})// 設(shè)置 dev test 環(huán)境開啟
? ? public PerformanceInterceptor performanceInterceptor() {
? ? ? ? PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
? ? ? ? performanceInterceptor.setMaxTime(100000);//ms,超過此處設(shè)置的ms則sql不執(zhí)行
? ? ? ? performanceInterceptor.setFormat(true);
? ? ? ? return performanceInterceptor;
? ? }
? ? /**
? ? ?* 邏輯刪除插件
? ? ?*/
? ? @Bean
? ? public ISqlInjector sqlInjector() {
? ? ? ? return new LogicSqlInjector();
? ? }
? ? /**
? ? ?* 分頁插件
? ? ?*/
? ? @Bean
? ? public PaginationInterceptor paginationInterceptor() {
? ? ? ? return new PaginationInterceptor();
? ? }
}創(chuàng)建mapper接口
@Mapper
public interface DemoMapper extends BaseMapper<Demo> {
? ? List<Demo> getAllList();
? ? @DS("slave_2")
? ? List<Demo> getShopList();
}測試類測試
@SpringBootTest
class Md5DemoApplicationTests {
? ? @Autowired
? ? private DemoMapper demoMapper;
? ? @Test
? ? void contextLoads() {
? ? ? ? List<Demo> list=demoMapper.getAllList();
? ? ? ? System.out.println(list);
? ? ? ? System.out.println("***************");
? ? ? ? List<Demo> shopList=demoMapper.getShopList();
? ? ? ? System.out.println(shopList);
? ? }
}@DS優(yōu)先級:方法 > 類
@DS 可以注解在方法上和類上,同時存在方法注解優(yōu)先于類上注解,mapper或者service都可以添加,建議只在一個方法上添加即可。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot項目實現(xiàn)多數(shù)據(jù)源配置使用dynamic-datasource-spring-boot-starter的操作步驟
- Springboot多數(shù)據(jù)源配置之整合dynamic-datasource方式
- springboot配置多數(shù)據(jù)源的一款框架(dynamic-datasource-spring-boot-starter)
- Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解
- SpringBoot采用Dynamic-Datasource方式實現(xiàn)多JDBC數(shù)據(jù)源
相關(guān)文章
JAVA中 redisTemplate 和 jedis的配合使用操作
這篇文章主要介紹了JAVA中 redisTemplate 和 jedis的配合使用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Redisson分布式閉鎖RCountDownLatch的使用詳細講解
分布式鎖和我們java基礎(chǔ)中學(xué)習(xí)到的synchronized略有不同,synchronized中我們的鎖是個對象,當(dāng)前系統(tǒng)部署在不同的服務(wù)實例上,單純使用synchronized或者lock已經(jīng)無法滿足對庫存一致性的判斷。本次主要講解基于rediss實現(xiàn)的分布式鎖2023-02-02
詳解java封裝返回結(jié)果與RestControllerAdvice注解
這篇文章主要為大家介紹了java封裝返回結(jié)果與RestControllerAdvice注解實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

