mybatis 分页_springboot+mybatis多数据源配置(附源码,pagehelper分页配置)

测试的工作很大一部分时间都会花费在造测试数据上,尤其对于分布式系统。这种系统一般都是很多小组维护各自的模块,作为下游系统,如果要验证一些业务流程,数据必须来自上游系统的真实数据,所以我们维护了一个帮助测试同事造数的平台,这其中就涉及到 “多数据源的配置”

mybatis工作原理

1. 创建SqlSessionFactoryBuilder对象,调用build(inputstream)方法读取并解析配置文件,返回SqlSessionFactory对象

2. 由SqlSessionFactory创建SqlSession 对象

3. 调用SqlSession中的api,传入Statement Id和参数,最后调用jdbc执行SQL语句,封装结果返回

实例

先配置两个库 teacher 、student

student.jdbc.url=jdbc:mysql://127.0.0.1:3306/student?serverTimezone=UTC&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNulljdbc.student.name=rootjdbc.student.password=root        teacher.jdbc.url=jdbc:mysql://127.0.0.1:3306/teacher?serverTimezone=UTC&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNulljdbc.teacher.name=rootjdbc.teacher.password=root

这里以student的Mybatis配置为例,

这里需要注意多数据源的配置需要指定的 主数据源的,需要添加

@Primary,如果不配置主数据源,启动会报错

@Configuration@MapperScan(basePackages = {"com.zl.demo.mapper.student"}, sqlSessionFactoryRef = "studentSqlSessionFactory")public class StudentMybatisConfig {​​    @Autowired    private AppConfigBean appConfigBean;​​    @Bean(name = "studentDataSource")    DataSource mockDataSource() throws SQLException {        DruidDataSource dataSource = new DruidDataSource();        dataSource.setUrl(appConfigBean.getStudentJdbcUrl());        dataSource.setDriverClassName(appConfigBean.getJdbcDriverClassName());        dataSource.setUsername(appConfigBean.getJdbcStudentName());        dataSource.setPassword(appConfigBean.getJdbcStudentPassword());        dataSource.setFilters("stat");        List initSqls = new ArrayList<>();        initSqls.add("set names utf8mb4");        dataSource.setConnectionInitSqls(initSqls);        dataSource.setMaxActive(appConfigBean.getJdbcMaxActive());        dataSource.setInitialSize(appConfigBean.getJdbcInitialSize());        return dataSource;    }​​    @Bean(name = "studentSqlSessionFactory")    @Primary    public SqlSessionFactory studentSqlSessionFactory() throws SQLException {        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();        factoryBean.setDataSource(mockDataSource());        factoryBean.setTypeAliasesPackage("com.zl.deml.entity.student");        // 分页插件        PageHelper pageHelper = new PageHelper();        Properties properties = new Properties();        properties.setProperty("reasonable", "false");        //properties.setProperty("supportMethodsArguments", "true");        properties.setProperty("returnPageInfo", "check");        properties.setProperty("params", "pageNum=start;count=countSql");        pageHelper.setProperties(properties);        // 添加插件        factoryBean.setPlugins(new Interceptor[]{pageHelper});        // 添加XML目录        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        try {            factoryBean                    .setMapperLocations(resolver.getResources("classpath*:/mybatis/student/*.xml"));            return factoryBean.getObject();        } catch (Exception e) {            e.printStackTrace();            throw new RuntimeException(e);        }    }​​    @Bean(name = "studentTransactionManager")    public DataSourceTransactionManager studentTransactionManager() {        try {            return new DataSourceTransactionManager(mockDataSource());        } catch (SQLException e) {            throw new RuntimeException(e);        }    }}

其中有一段是对pageHelper 分页的配置,不需要的话可以删除

测试数据

多数据源整合测试数据

67f548bed62a4e6a84dec1d22dd5d02b

分页的测数据

93b132ca2d78418e9e99da660d5f8eae

实例代码已上传github

https://github.com/627886474/mybatis-multidata

如果对你有帮助的话

欢迎微信搜索:可乐的测试之路

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,可以使用两种方式配置 MyBatis 分页插件: 1. 使用 PageHelper 自动配置 PageHelper 是一个开MyBatis 分页插件,支持多种数据库,使用起来非常方便。在 Spring Boot 中,可以使用 PageHelper 的自动配置功能,只需要在 pom.xml 中引入 PageHelper 依赖,然后在 application.properties 或 application.yml 中添加配置即可。 例如,在 pom.xml 中添加如下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 然后在 application.properties 或 application.yml 中添加如下配置: ```properties # 开启分页插件 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ``` 2. 使用 MyBatis-Plus MyBatis-Plus 是一个开MyBatis 增强工具包,其中包含了分页插件。在 Spring Boot 中,可以使用 MyBatis-Plus 的自动配置功能,只需要在 pom.xml 中引入 MyBatis-Plus 依赖,然后在 application.properties 或 application.yml 中添加配置即可。 例如,在 pom.xml 中添加如下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> ``` 然后在 application.properties 或 application.yml 中添加如下配置: ```properties # 开启分页插件 mybatis-plus.configuration.properties.pagehelper.helperDialect=mysql ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值