推荐:MyBatis Plus汇总
首先创建一个数据库表,如下图所示:
然后创建一个Spring Boot项目,pom.xml和配置如下:
4.0.0
org.kaven
mybatis-plus
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-webflux
com.baomidou
mybatis-plus-boot-starter
3.4.0
mysql
mysql-connector-java
5.1.49
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
application.yaml配置:
spring:
application:
name: mybatis-plus
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: ITkaven@123
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false
server:
port: 8085
logging:
level:
root: warn
com.kaven.mybatisplus.dao: trace
pattern:
console: '%p%m%n'
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
实体类User:
package com.kaven.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("user")
@Data
public class User {
@TableId
private String id;
@TableField("username")
private String username;
@TableField("password")
private String password;
@TableField("age")
private Integer age;
/**
* 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入数据库中
* 使用 transient 、 static 修饰属性也不会插入数据库中
*/
@TableField(exist = false)
private String phone;
}
Mapper接口UserMapper:
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kaven.mybatisplus.entity.User;
import org.springframework.stereotype.Component;
@Component
public interface UserMapper extends BaseMapper {}
启动类:
package com.kaven.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.kaven.mybatisplus.dao")
public class AppRun {
public static void main(String[] args) {
SpringApplication.run(AppRun.class , args);
}
}
@MapperScan(basePackages = "com.kaven.mybatisplus.dao")
这个一定要加上。
我们先在数据库中添加几行数据,方便演示。
为了使用MyBatis-Plus来实现分页查询,还需要一些配置。
package com.kaven.mybatisplus.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
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
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setDbType(DbType.MYSQL);
paginationInnerInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
项目结构如下图:
现在来使用一下分页查询方法selectPage()
。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperPageTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectPage(){
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.like(User::getUsername , "k");
Page userPage = new Page<>(1 , 2);
IPage userIPage = userMapper.selectPage(userPage , userLambdaQueryWrapper);
System.out.println("总页数: "+userIPage.getPages());
System.out.println("总记录数: "+userIPage.getTotal());
userIPage.getRecords().forEach(System.out::println);
}
}
结果如下:
结果是正确的,可以看到该方法执行了两条sql
,一条是查询总记录数,一条是查询我们需要的数据。
如果我们并不需要总记录数,查询总记录数就完全没有必要,因为它也需要耗时,其实可以设置不查询总记录数。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperPageTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectPage(){
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.like(User::getUsername , "k");
Page userPage = new Page<>(1 , 2 , false);
IPage userIPage = userMapper.selectPage(userPage , userLambdaQueryWrapper);
System.out.println("总页数: "+userIPage.getPages());
System.out.println("总记录数: "+userIPage.getTotal());
userIPage.getRecords().forEach(System.out::println);
}
}
Page
在这里设置为false
即可。
结果如下:
从上图可以看到只有一条sql
,总页数和总记录数都为0
,因为我们设置了不需要去查询这些信息。
再来演示一下selectMapsPage()
的用法。
package com.kaven.mybatisplus.dao;
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperPageTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectMapsPage(){
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.select(User::getUsername).like(User::getUsername , "k");
Page
结果如下:
结果也是正确的,从用法上也可以看出应用场景。
基于可扩展性,MyBatis-Plus还可以使用自定义sql
的方法来实现分页查询,这样便于多表等复杂条件。
修改UserMapper接口:
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.kaven.mybatisplus.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface UserMapper extends BaseMapper {
IPage selectByPage(IPage userPage , @Param(Constants.WRAPPER) Wrapper userWrapper);
}
UserMapper.xml:
也可以使用Mybatis
的注解来代替xml配置文件(如@Select
)。
测试代码:
package com.kaven.mybatisplus.dao;
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperPageTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectByPage(){
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.like(User::getUsername , "k");
Page mapPage = new Page<>(1 , 2 , false);
IPage mapIPage = userMapper.selectByPage(mapPage , userLambdaQueryWrapper);
System.out.println("总页数: "+mapIPage.getPages());
System.out.println("总记录数: "+mapIPage.getTotal());
mapIPage.getRecords().forEach(System.out::println);
}
}
结果如下:
结果也是正确的。