当前位置: 首页 > news >正文

dynamic-datasource+Mybatis多数据源使用

Gitee地址:dynamic-datasource: 基于 SpringBoot 多数据源 动态数据源 主从分离 快速启动器 支持分布式事务

依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version>
</dependency>
<!--dynamic-datasource -->
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot3-starter</artifactId><version>4.3.1</version>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.21</version>
</dependency>

application.properties

spring.application.name=dynamic-datasourceserver.port=8088#设置默认的数据源或者数据源组,默认值即为master
spring.datasource.dynamic.primary=master
#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
spring.datasource.dynamic.strict=falsespring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/dy?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=root
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.master.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.dynamic.datasource.slave.url=jdbc:mysql://localhost2:3306/dy?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
spring.datasource.dynamic.datasource.slave.username=root
spring.datasource.dynamic.datasource.slave.password=root
spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver
#去掉默认使用HikariCP
spring.datasource.dynamic.datasource.slave.type=com.alibaba.druid.pool.DruidDataSourcelogging.level.com.example.dynamicdatasource=debugmybatis.type-aliases-package=com.example.dynamicdatasource.pojo
mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
#驼峰
mybatis.configuration.map-underscore-to-camel-case=true

package com.example.dynamicdatasource.controller;import com.example.dynamicdatasource.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author hrui* @date 2024/8/6 15:49*/
@RestController
public class ProductController {@Autowiredprivate ProductService productService;@GetMapping("/saveProduct")public String saveProduct(){productService.saveProduct();return "saveProduct success";}
}

package com.example.dynamicdatasource.controller;import com.example.dynamicdatasource.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author hrui* @date 2024/8/6 16:10*/
@RestController
public class TransactionController {@Autowiredprivate TransactionService transactionService;@GetMapping("/trans")public String saveUserAndProduct() {transactionService.saveUserAndProduct();return "success";}
}

package com.example.dynamicdatasource.controller;import com.example.dynamicdatasource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author hrui* @date 2024/8/6 15:49*/
@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/saveUser")public String saveUser() {userService.saveUser();return "success";}
}

package com.example.dynamicdatasource.service;/*** @author hrui* @date 2024/8/6 15:49*/
public interface ProductService {void saveProduct();
}

package com.example.dynamicdatasource.service;/*** @author hrui* @date 2024/8/6 16:11*/
public interface TransactionService {void saveUserAndProduct();
}

package com.example.dynamicdatasource.service;/*** @author hrui* @date 2024/8/6 15:49*/
public interface UserService {void saveUser();
}

package com.example.dynamicdatasource.service.impl;import com.example.dynamicdatasource.mapper.ProductMapper;
import com.example.dynamicdatasource.pojo.Product;
import com.example.dynamicdatasource.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.math.BigDecimal;/*** @author hrui* @date 2024/8/6 15:49*/
@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductMapper productMapper;@Overridepublic void saveProduct() {Product product = new Product();product.setName("product");product.setPrice(new BigDecimal(100));int i=productMapper.saveProduct(product);System.out.println(i>0?"插入成功":"插入失败");}
}

package com.example.dynamicdatasource.service.impl;import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.dynamicdatasource.mapper.ProductMapper;
import com.example.dynamicdatasource.mapper.UserMapper;
import com.example.dynamicdatasource.pojo.Product;
import com.example.dynamicdatasource.pojo.User;
import com.example.dynamicdatasource.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.math.BigDecimal;/*** @author hrui* @date 2024/8/6 16:11*/
@Service
public class TransactionServiceImpl implements TransactionService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@DSTransactional//使用@Transaction时候 配置类启动类加  @TransactionServiceImpl  现在不加也行//@Transactional//切换时候注意maven清理  这个注解此时不允许使用  报表不存在因数据源还在主库里 Table 'dy.t_product' doesn't existpublic void saveUserAndProduct() {//判断是否开启事务
//        if( TransactionSynchronizationManager.isActualTransactionActive()){
//            System.out.println("开启事务");
//        }else{
//            System.out.println("未开启事务");
//        }User user = new User(null, "李四", "123456", 18, "男", "北京");Product product = new Product(null, "手机", new BigDecimal(1000));int i=userMapper.saveUser(user);int i2=productMapper.saveProduct(product);System.out.println(1/0);System.out.println(i>0&&i2>0?"插入成功":"插入失败");}
}

package com.example.dynamicdatasource.service.impl;import com.example.dynamicdatasource.mapper.UserMapper;
import com.example.dynamicdatasource.pojo.User;
import com.example.dynamicdatasource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author hrui* @date 2024/8/6 15:49*/
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic void saveUser() {User user = new User(null, "张三", "123456", 18, "男", "北京");int i=userMapper.saveUser(user);System.out.println(i>0?"插入成功":"插入失败");}
}

package com.example.dynamicdatasource.mapper;import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.dynamicdatasource.pojo.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;/*** @author hrui* @date 2024/8/6 15:50*/
@DS("slave")
public interface ProductMapper {@Insert("insert into t_product(name,price) values(#{name},#{price})")//返回自增主键@Options(useGeneratedKeys = true, keyProperty = "id")int saveProduct(Product product);}

package com.example.dynamicdatasource.mapper;import com.example.dynamicdatasource.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;/*** @author hrui* @date 2024/8/6 15:50*/
public interface UserMapper {@Insert("insert into t_user(name,sex,age,address,password) values(#{name},#{sex},#{age},#{address},#{password})")//返回自增主键@Options(useGeneratedKeys = true, keyProperty = "id")int saveUser(User user);
}

package com.example.dynamicdatasource.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;/*** @author hrui* @date 2024/8/6 15:50*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {private Integer id;private String name;private BigDecimal price;
}

package com.example.dynamicdatasource.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author hrui* @date 2024/8/6 15:50*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String name;private String password;private Integer age;private String sex;private String address;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • ctfhub Bypass disable_function
  • 论文辅导 | 基于概率密度估计与时序Transformer网络的风功率日前区间预测
  • 测试总结8/6
  • 08.SQL注入-下(超详细!!!)
  • 如何在SQLite中实现自动时间戳
  • 【C++ | 泛型编程】C++函数模板详解(定义、使用、特化、重载)
  • 工具学习_CONAN_Consuming Packages
  • 如何在 Debian 上安装运行极狐GitLab Runner?【一】
  • Hadoop入门:构建你的第一个大数据处理平台
  • Spring Boot 使用多线程完成 统计当日用户所属区域
  • 选电脑——电脑配置
  • ViP-LLaVA: Making Large Multimodal Models Understand Arbitrary Visual Prompts
  • 江协科技51单片机学习- p31 LCD1602液晶屏驱动
  • Java二十三种设计模式-组合模式(11/23)
  • 揭秘LoRA:利用深度学习原理在Stable Diffusion中打造完美图像生成的秘密武器
  • 时间复杂度分析经典问题——最大子序列和
  • 「译」Node.js Streams 基础
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【翻译】babel对TC39装饰器草案的实现
  • Angularjs之国际化
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Facebook AccountKit 接入的坑点
  • Python3爬取英雄联盟英雄皮肤大图
  • storm drpc实例
  • sublime配置文件
  • VuePress 静态网站生成
  • Webpack 4 学习01(基础配置)
  • 关于springcloud Gateway中的限流
  • 如何用vue打造一个移动端音乐播放器
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 小程序 setData 学问多
  • 7行Python代码的人脸识别
  • ​​​​​​​​​​​​​​Γ函数
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​业务双活的数据切换思路设计(下)
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # Spring Cloud Alibaba Nacos_配置中心与服务发现(四)
  • ######## golang各章节终篇索引 ########
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #WEB前端(HTML属性)
  • #微信小程序:微信小程序常见的配置传旨
  • $(this) 和 this 关键字在 jQuery 中有何不同?
  • (3)llvm ir转换过程
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (实测可用)(3)Git的使用——RT Thread Stdio添加的软件包,github与gitee冲突造成无法上传文件到gitee
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (一)Dubbo快速入门、介绍、使用
  • (转)VC++中ondraw在什么时候调用的
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core使用ef 6
  • .Net Winform开发笔记(一)
  • .net6使用Sejil可视化日志
  • .Net6使用WebSocket与前端进行通信