郵箱網(wǎng)址查詢百度刷排名seo
配置多數(shù)據(jù)源
模擬多庫場景
適用于多種場景: 多庫(操作的表分布在不同數(shù)據(jù)庫當(dāng)中),讀寫分離(有的數(shù)據(jù)庫負(fù)責(zé)查詢的功能,有的數(shù)據(jù)庫負(fù)責(zé)增刪該的功能),一主多從,混合模式等
第一步: 模擬多庫,在mybatis_plus數(shù)據(jù)庫中創(chuàng)建user表,在mybatis_plus_1數(shù)據(jù)庫中創(chuàng)建product表
--創(chuàng)建mybatis_plus數(shù)據(jù)庫
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
--創(chuàng)建user表
CREATE TABLE `t_user` (`id` bigint(20) NOT NULL COMMENT '主鍵ID',`name` varchar(30) DEFAULT NULL COMMENT '姓名',`age` int(11) DEFAULT NULL COMMENT '年齡',`email` varchar(50) DEFAULT NULL COMMENT '郵箱',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--向user表中插入數(shù)據(jù)
INSERT INTO t_user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
--創(chuàng)建mybatis_plus_1數(shù)據(jù)庫
CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus_1`;
--創(chuàng)建product表
CREATE TABLE t_product(
id BIGINT(20) NOT NULL COMMENT '主鍵ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名稱',
price INT(11) DEFAULT 0 COMMENT '價格',
version INT(11) DEFAULT 0 COMMENT '樂觀鎖版本號',
PRIMARY KEY (id)
);
--向product表插入數(shù)據(jù)
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人筆記本', 100);
第二步:在pom.xml
文件中引入多數(shù)據(jù)源使用的依賴
<!--配置多數(shù)據(jù)源需要使用的依賴-->
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version>
</dependency>
第三步: 在appilcation.yaml
文件中添加配置多數(shù)據(jù)源的配置選項(xiàng)
spring:# 配置數(shù)據(jù)源信息datasource:dynamic:# 設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為masterprimary: master# 嚴(yán)格匹配數(shù)據(jù)源(默認(rèn)false),true表示如果未匹配到指定數(shù)據(jù)源時拋異常,false表示如果未匹配到指定的數(shù)據(jù)源就使用默認(rèn)的數(shù)據(jù)源strict: falsedatasource:master: # 默認(rèn)數(shù)據(jù)源url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456slave_1: # 其他數(shù)據(jù)源url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456
第四步: 創(chuàng)建實(shí)體類封裝表中查詢的數(shù)據(jù)
//封裝用戶信息的實(shí)體類
@Data
@TableName("t_user")
public class User {private Integer id;private String name;private Integer age;private String email;
}
//封裝產(chǎn)品信息的實(shí)體類
@Data
@TableName("t_product")
public class Product {private Integer id;private String name;private Integer price;private Integer version;
}
第五步: 編寫User和Product對應(yīng)的mapper接口和service接口
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}@Repository
public interface UserMapper extends BaseMapper<User> {
}public interface UserService extends IService<User> {}public interface ProductService extends IService<Product> {}
第六步: 創(chuàng)建User和Product實(shí)體類對應(yīng)的service接口的實(shí)現(xiàn)類,使用@DS注解
指定當(dāng)前類或方法要操作的數(shù)據(jù)源
- @Ds注解使用在方法上或類上(使用在類上表示類中所有的方法默認(rèn)都有該注解)
- @Ds注解在類和方法上同時存在則遵循就近原則,方法上指定操作的數(shù)據(jù)源優(yōu)先于類上指定操作的數(shù)據(jù)源
注解 | 結(jié)果 |
---|---|
沒有@DS注解 | 操作默認(rèn)的數(shù)據(jù)源 |
@DS(“dsName”) | dsName為要操作的數(shù)據(jù)源名稱 |
@Service
@DS("master") //t_user表在master數(shù)據(jù)源當(dāng)中
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implementsUserService {
}@Service
@DS("slave_1")//t_product表在slave_1數(shù)據(jù)源當(dāng)中
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
第七步: 編寫Spring Boot主程序的啟動類
@SpringBootApplication
@MapperScan("com.atguigu.mybatisplus.mapper")// 掃描mapper接口所在的包
public class MybatisPlusDatasourceApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusDatasourceApplication.class, args);}
}
第八步: 由于t_user表和t_product在兩個不同的數(shù)據(jù)庫(數(shù)據(jù)源)中,如果通過一個測試方法可以分別獲取到用戶數(shù)據(jù)與商品數(shù)據(jù)成功說明多庫模擬成功
@SpringBootTest
public class MybatisPlusDatasourceApplicationTests {@Autowiredprivate UserService userService;@Autowiredprivate ProductService productService;@Testpublic void testDynamicDataSource(){// 輸出查詢出來的對象System.out.println(userService.getById(1));System.out.println(productService.getById(1));}
}
模擬讀寫分離
實(shí)現(xiàn)讀寫分離的效果
- 寫操作: 增刪改操作的方法加上@DS注解指定操作主庫數(shù)據(jù)源,因?yàn)橹挥兄鲾?shù)據(jù)源才可以把更新的操作同步到從數(shù)據(jù)源
- 讀操作: 查詢操作的方法指定操作從庫數(shù)據(jù)源