最简单的SpringBoot+MyBatis多数据源实现

文章详细介绍了如何在SpringBoot项目中结合MyBatis实现多数据源配置,包括数据库准备、环境配置、代码编写,特别是主次数据源的配置和测试。通过创建两个不同的数据源,实现了对不同数据库的读写操作,以此达到数据库的读写分离和压力缓解。
摘要由CSDN通过智能技术生成

最简单的SpringBoot+MyBatis多数据源实现

image-20200708160944615

随着应用用户数量的增加,相应的并发请求的数量也会跟着不断增加,慢慢地,单个数据库已经没有办法满足频繁的数据库操作请求了,在某些场景下,可能会需要配置多个数据源,使用多个数据源(例如实现数据库的读写分离)来缓解系统的压力等,同样的,Springboot官方提供了相应的实现来帮助开发者们配置多数据源,一般分为两种方式(目前所了解到的),分包和AOP。

考虑到mybatis是java开发使用较为频繁的数据库框架,所以使用Springboot+Mybatis来实现多数据源的配置。

1.数据库准备

既然是配置多数据源,那么自然就要先把相应的数据源给准备好,本地新建了两个数据库,如下表:

数据库数据表字段
testdatasource1sys_useruser_id(int), user_name(varchar) user_age(int)
testdatasource2sys_user2同上

并分别插入两条记录,为了方便对比,其中testdatasource1为芳年25岁的张三, testdatasource2为芳年30岁的李四。

2.环境准备

首先新建一个Springboot项目,这里版本是2.1.7.RELEASE,并在pom文件中引入相关依赖:关键依赖如下:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3.代码部分

3.1多数据源配置

首先呢,在Springboot的配置文件中配置的datasourse,和以往不一样的是,因为有两个数据源,所以要指定相关数据库的名称,其中主数据源为primary,次数据源为secondary如下:

#配置主数据库
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/testdatasource1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

##配置次数据库
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/testdatasource2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver


spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

需要注意的是,Springboot2.0 在配置数据库连接的时候需要使用jdbc-url,如果只使用url的话会报
jdbcUrl is required with driverClassName.错误。

新建一个配置类PrimaryDataSourceConfig,用于配置的主数据库相关的bean,代码如下:

@Configuration
//basePackages:接口文件的包路径
@MapperScan(basePackages = "com.jdkcb.mybatisstuday.mapper.one", sqlSessionFactoryRef = "PrimarySqlSessionFactory")
public class PrimaryDataSourceConfig {

    @Bean(name = "PrimaryDataSource")
    // 表示这个数据源是默认数据源
    @Primary//这个一定要加,如果两个数据源都没有@Primary会报错
    @ConfigurationProperties(prefix = "spring.datasource.primary")//配置文件中的前缀
    public DataSource getPrimaryDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "PrimarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("PrimaryDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations( 
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/one/*.xml"));
        return bean.getObject();// 设置mybatis的xml所在位置
    }

    @Bean("PrimarySqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

}

注解说明:

@MapperScan :配置mybatis的接口类放的地方

@Primary :表示使用的是默认数据库,这个一个要加,否则会因为不知道哪个数据库是默认数据库而报错

@ConfigurationProperties:读取application.properties中的配置参数映射成为一个对象,其中prefix表示参数的前缀

大功告成~ ~ 了吗?并没有,然后配置的第二个数据源的配置类,代码如下:

@Configuration
@MapperScan(basePackages = "com.jdkcb.mybatisstuday.mapper.two", sqlSessionFactoryRef = "SecondarySqlSessionFactory")
public class SecondaryDataSourceConfig {

    @Bean(name = "SecondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource getSecondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "SecondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("SecondaryDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/two/*.xml"));
        return bean.getObject();// 设置mybatis的xml所在位置
    }
    @Bean("SecondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("SecondarySqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

剩下的就是编写相应的xml文件和接口类了,代码如下:

@Component
@Mapper
public interface PrimaryUserMapper {
    List<User> findAll();
}
@Component
@Mapper
public interface SecondaryUserMapper {
    List<User> findAll();
}

相关的xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jdkcb.mybatisstuday.mapper.one.PrimaryUserMapper">
    <select id="findAll" resultType="com.jdkcb.mybatisstuday.pojo.User">
                select * from sys_user;
    </select>
</mapper>


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jdkcb.mybatisstuday.mapper.two.SecondaryUserMapper">
    <select id="findAll" resultType="com.jdkcb.mybatisstuday.pojo.User">
                select * from sys_user2;
    </select>

</mapper>

注:其中xml文件在本实例中目录为:resources/mapping

2.测试

编写一个Controller用于测试,因为是测试实例且代码相对来说较为简单,所以这里就不写Service层了。

代码如下:

@RestController
public class UserController {

    @Autowired
    private PrimaryUserMapper primaryUserMapper;
    @Autowired
    private SecondaryUserMapper secondaryUserMapper;
    @RequestMapping("/primary")
    public Object primary(){
        List<User> list = primaryUserMapper.findAll();
        return list;
    }
    
    @RequestMapping("/secondary")
    public Object secondary  (){
        List<User> list = secondaryUserMapper.findAll();
        return list;
    }


}

在浏览器分别输入:http://127.0.0.1:8080/primaryhttp://127.0.0.1:8080/secondary

结果如下:

[{"user_id":1,"user_name":"张三","user_age":25}] //primary 
[{"user_id":1,"user_name":"李四","user_age":30}] //secondary
SpringBootMyBatis实现分区表可以通过使用MyBatis-Plus插件实现MyBatis-Plus是MyBatis的增强工具,在MyBatis的基础上提供了更多的功能和便捷的操作方式,包括分区表的实现。 下面是一个简单的示例,演示如何在SpringBootMyBatis实现分区表: 1. 首先,在pom.xml文件中添加MyBatis-Plus和Druid的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 然后,在application.properties文件中配置Druid数据源和MyBatis-Plus的相关配置: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Druid 数据源配置 spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=5 spring.datasource.druid.test-on-borrow=true # MyBatis-Plus 配置 mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.configuration.cache-enabled=false mybatis-plus.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.default-fetch-size=100 ``` 3. 接下来,在MyBatis的Mapper文件中使用分区表的SQL语句: ``` <select id="getUserById" resultType="com.example.demo.entity.User"> SELECT * FROM user_${id % 10} WHERE id = #{id} </select> ``` 这里的user_${id % 10}表示使用id模10的结果作为分区表的后缀,即将数据按照id模10的结果分散到10个不同的表中。 4. 最后,在SpringBoot应用中调用Mapper方法即可实现分区表的查询: ``` @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.getUserById(id); } ``` 至此,我们已经完成了在SpringBootMyBatis实现分区表的过程。当然,实际场景中可能会更复杂,需要结合具体业务需求进行调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜源Jerry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值