MybatisPlus集成baomidou-dynamic,多数据源配置使用、MybatisPlus分页分组等操作示例

文章目录

  • pom
  • 配置
  • 示例代码

pom

 
        
        
            com.baomidou
            mybatis-plus-boot-starter
             3.4.2
        
        
        
            mysql
            mysql-connector-java
             8.0.27
        
        
        
            com.alibaba
            druid-spring-boot-starter
              1.1.20
        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
             3.4.1
        
        
            org.postgresql
            postgresql
             42.3.6
        
    

配置

配置文件

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.datasource.dynamic.primary=mysql
spring.datasource.dynamic.strict=false
spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://192.168.0.111:3306/database?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false¤tSchema=public
spring.datasource.dynamic.datasource.mysql.username=root
spring.datasource.dynamic.datasource.mysql.password=123456
spring.datasource.dynamic.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.mysql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dynamic.datasource.mysql.druid.maxActive=300
spring.datasource.dynamic.datasource.mysql.druid.initialSize=20
spring.datasource.dynamic.datasource.mysql.druid.maxWait=6000
spring.datasource.dynamic.datasource.mysql.druid.minIdle=20
spring.datasource.dynamic.datasource.mysql.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.dynamic.datasource.mysql.druid.minEvictableIdleTimeMillis=30000
spring.datasource.dynamic.datasource.mysql.druid.validationQuery=select 'x'
spring.datasource.dynamic.datasource.mysql.druid.testWhileIdle=true
spring.datasource.dynamic.datasource.mysql.druid.testOnBorrow=true
spring.datasource.dynamic.datasource.mysql.druid.testOnReturn=false

spring.datasource.dynamic.datasource.postgresql.url= jdbc:postgresql://127.0.0.1:5432/database?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false¤tSchema=public
spring.datasource.dynamic.datasource.postgresql.username=postgres
spring.datasource.dynamic.datasource.postgresql.password=123456
spring.datasource.dynamic.datasource.postgresql.driverClassName=org.postgresql.Driver
spring.datasource.dynamic.datasource.postgresql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dynamic.datasource.postgresql.druid.maxActive=300
spring.datasource.dynamic.datasource.postgresql.druid.initialSize=20
spring.datasource.dynamic.datasource.postgresql.druid.maxWait=6000
spring.datasource.dynamic.datasource.postgresql.druid.minIdle=20
spring.datasource.dynamic.datasource.postgresql.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.dynamic.datasource.postgresql.druid.minEvictableIdleTimeMillis=30000
spring.datasource.dynamic.datasource.postgresql.druid.validationQuery=select 'x'
spring.datasource.dynamic.datasource.postgresql.druid.testWhileIdle=true
spring.datasource.dynamic.datasource.postgresql.druid.testOnBorrow=true
spring.datasource.dynamic.datasource.postgresql.druid.testOnReturn=false

配置类

解决分页失效问题

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.ais.**.mapper.**")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

示例代码

实体类
@TableName(“tableName”)
@TableField
@TableId
MybatisPlus集成baomidou-dynamic,多数据源配置使用、MybatisPlus分页分组等操作示例_第1张图片
Mapper
MybatisPlus集成baomidou-dynamic,多数据源配置使用、MybatisPlus分页分组等操作示例_第2张图片
参考代码

import com.UserEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MybatisTest {
    @Resource
    private UserMapper mapper;

    /**
     * 分页操作 Mysql  MybatisPlus
     */
    @Test
    public void page() {
        IPage page = new Page(2, 2);
        IPage iPage = mapper.selectPage(page, null);
        System.out.println(iPage.getRecords());

    }

    /**
     * PGSQL 自定义SQL分页
     */
    @Test
    public void pagePg() {
        IPage page = new Page(1, 1);
        IPage iPage = mapper.selectAll(page);
        System.out.println(iPage.getRecords());
    }

    /**
     * 分页加排序降序
     */
    @Test
    public void sort() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        //降序
        queryWrapper.orderByDesc("center_Id");
        //升序
//        queryWrapper.orderByAsc("center_Id");
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * 分页加排序降序 lambda 表达式
     */
    @Test
    public void lambdaSort() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        //降序
        queryWrapper.orderByDesc(Entity::getCenterId);
        //升序
//        queryWrapper.orderByAsc(Entity::getCenterId);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * in条件过滤 lambda 表达式
     */
    @Test
    public void selectIn() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        List<Long> ids = new ArrayList<>();
        ids.add(1l);
        ids.add(2l);
        ids.add(3l);
        ids.add(4l);
        queryWrapper.in(Entity::getCenterId, ids);
//        queryWrapper.notIn(Entity::getCenterId, ids);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }


    /**
     * in条件过滤 lambda 表达式
     */
    @Test
    public void selectInSql() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.inSql(Entity::getCenterId, "select center_Id from center where center_Id>5");
//        queryWrapper.notIn(Entity::getCenterId, ids);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    @Test
    public void select() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * max 加分组
     */
    @Test
    public void selectMax() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(center_id) as center_id").groupBy("created_By");
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * count 加分组
     */
    @Test
    public void selectCount() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(center_id) as count").groupBy("created_By");
        mapper.selectPage(page, queryWrapper);
    }
}

你可能感兴趣的:(数据库,java,mybatis)