java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringCloud MybatisPlus MySQL多数据源

SpringCloud集成MybatisPlus实现MySQL多数据源配置方法

作者:不惑_

本文详细介绍了SpringCloud集成MybatisPlus实现MySQL多数据源配置的方法,包括在application.properties中配置多数据源,配置MybatisPlus,创建Mapper接口和使用多数据源等步骤,此外,还解释了每一个配置项目的含义,以便读者更好地理解和应用

引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.15</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

配置多数据源

在application.properties中配置多数据源:

spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.master.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.master.username=root
spring.datasource.master.password=root
spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.slave.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.slave.username=root
spring.datasource.slave.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.pool.init-size=10
spring.datasource.pool.max-size=20
spring.datasource.pool.min-size=5
spring.datasource.pool.max-wait=30000
spring.datasource.filters=stat,wall,log4j
spring.datasource.log-enabled=true
spring.datasource.log-prefix=druid.log
spring.datasource.stat-view-servlet.enabled=true
spring.datasource.stat-view-servlet.url-pattern=/druid/*
spring.datasource.web-stat-filter.enabled=true
spring.datasource.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
spring.datasource.max-total=20
spring.datasource.max-idle=10
spring.datasource.min-idle=5
spring.datasource.time-between-eviction-runs-millis=60000
spring.datasource.min-evictable-idle-time-millis=300000
spring.datasource.validation-query=SELECT 1 FROM dual
spring.datasource.test-on-borrow=true
spring.datasource.test-on-return=true
spring.datasource.test-while-idle=true
spring.datasource.remove-abandoned=true
spring.datasource.remove-abandoned-timeout=60000
spring.datasource.log-abandoned=true

配置解释

这是一个Spring Boot应用程序中用于配置数据库连接的属性文件。以下是每个配置项目的解释:

配置MybatisPlus

在application.properties中配置MybatisPlus:

mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.global-config.id-type=auto
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
mybatis-plus.global-config.base-namespace=test
mybatis-plus.global-config.mapper-namespace=test.mapper

配置MybatisPlus解释

这是一个MyBatis Plus(通常简称为MyBatis+或MP)的配置文件,用于配置MyBatis Plus在Spring Boot应用程序中的行为。以下是每个配置项目的解释:

配置Mapper

创建一个Mapper接口,例如UserMapper:

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

使用多数据源

在需要使用多数据源的地方,使用@MapperScan注解指定Mapper所在包路径:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @MapperScan("com.example.demo.mapper")
    public class MyApp {
        // ...
    }
}

CRUD示例

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User save(User user) {
        return userMapper.save(user);
    }
    @Override
    public User update(User user) {
        return userMapper.updateById(user);
    }
    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }
    @Override
    public void delete(Long id) {
        userMapper.deleteById(id);
    }
}
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping
    public User save(@RequestBody User user) {
        return userService.save(user);
    }
    @PutMapping("/{id}")
    public User update(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.update(user);
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        userService.delete(id);
    }
}

使用不同数据源

要使用不同的数据源查询,可以在Mapper接口中使用@MapperScan注解指定需要使用的数据源,例如:

@MapperScan("com.example.demo.mapper.master")
public interface UserMapperMaster extends BaseMapper<User> {
}
@MapperScan("com.example.demo.mapper.slave")
public interface UserMapperSlave extends BaseMapper<User> {
}

然后在需要使用不同数据源的地方,使用@Autowired注解注入对应的Mapper接口,例如:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapperMaster userMapperMaster;
    @Autowired
    private UserMapperSlave userMapperSlave;
    @Override
    public User save(User user) {
        return userMapperMaster.save(user);
    }
    @Override
    public User update(User user) {
        return userMapperMaster.updateById(user);
    }
    @Override
    public User findById(Long id) {
        return userMapperMaster.selectById(id);
    }
    @Override
    public void delete(Long id) {
        userMapperMaster.deleteById(id);
    }
}

要在某个方法上使用不同的数据源,可以在该方法上使用@MapperScan注解指定需要使用的数据源,例如:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapperMaster userMapperMaster;
    @Autowired
    private UserMapperSlave userMapperSlave;
    @Override
    public User save(User user) {
        return userMapperMaster.save(user);
    }
    @Override
    public User update(User user) {
        return userMapperMaster.updateById(user);
    }
    @Override
    public User findById(Long id) {
        return userMapperMaster.selectById(id);
    }
    @Override
    public void delete(Long id) {
        userMapperMaster.deleteById(id);
    }
    @MapperScan("com.example.demo.mapper.slave")
    @Override
    public User findByIdSlave(Long id) {
        return userMapperSlave.selectById(id);
    }
}

到此这篇关于SpringCloud集成MybatisPlus实现MySQL多数据源配置的文章就介绍到这了,更多相关SpringCloud MybatisPlus MySQL多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文