多数据源
创建数据库
CREATE DATABASE mybatis_plus_1;
USE mybatis_plus_1;
CREATE TABLE 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)
);
删除表
USE mybatis_plus;
DROP TABLE IF EXISTS product;
插入数据
INSERT INTO product (id, NAME, price) VALUES (1, '外星人', 100);
新建个工程。步骤和之前的一样
导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
修改application.yml
spring:
# 配置数据源信息
datasource:
dynamic:
# 设置默认的数据源或数据源组,默认值即为master
primary: master
# 严格匹配数据源,默认false,true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: "011012"
save_1:
url: jdbc:mysql://localhost:3306/mybatis_plus_1?useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: "011012"
创建文件包pojo
package com.example.pojo;
import lombok.Data;
@Data
public class Product {
private Integer id;
private String name;
private Integer price;
private Integer version;
}
package com.example.pojo;
import com.baomidou.mybatisplus.annotation.*;
import lombok.*;
//所有无参构造和get方法和set方法还有哈希库方法注解(lombok)
//有参构造方法注解(@AllArgsConstructor)
@Data
@TableName("t_user")
public class User {
//将该属性对应的字段指定为主键
@TableId(value = "uid",type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
private Integer sex;
}
创建文件包mapper(都是接口)
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.Product;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
创建文件包Service
package com.example.Sevice;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.Product;
public interface ProductService extends IService<Product> {
}
package com.example.Sevice;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.User;
public interface UserService extends IService<User> {
}
在这个包下创建文件包impl
package com.example.Sevice.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Service;
@Service
@DS("master")
public class UserService extends ServiceImpl<UserMapper, User> implements com.example.Sevice.UserService {
}
package com.example.Sevice.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.Sevice.ProductService;
import com.example.mapper.ProductMapper;
import com.example.pojo.Product;
import lombok.Data;
import org.springframework.stereotype.Service;
@Service
@DS("save_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
写测试文件
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void test1(){
System.out.println(userService.getById(1));
System.out.println(productService.getById(1));
}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!