文章目录
- pom
- 配置
- 示例代码
pom
<dependencies><!--mybatisPlus集成SpringBoot起步依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--MySQL 驱动依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!--druid 数据连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.20</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.3.6</version></dependency></dependencies>
配置
配置文件
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=falsespring.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 {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}
示例代码
实体类
@TableName(“tableName”)
@TableField
@TableId
Mapper
参考代码
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 {@Resourceprivate UserMapper mapper;/*** 分页操作 Mysql MybatisPlus*/@Testpublic void page() {IPage page = new Page(2, 2);IPage iPage = mapper.selectPage(page, null);System.out.println(iPage.getRecords());}/*** PGSQL 自定义SQL分页*/@Testpublic void pagePg() {IPage page = new Page(1, 1);IPage iPage = mapper.selectAll(page);System.out.println(iPage.getRecords());}/*** 分页加排序降序*/@Testpublic 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 表达式*/@Testpublic 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 表达式*/@Testpublic 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 表达式*/@Testpublic 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());}@Testpublic 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 加分组*/@Testpublic 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 加分组*/@Testpublic 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);}
}