首先创建一个数据库表,如下图所示:
然后创建一个Spring Boot项目,pom.xml和配置如下:
4.0.0 org.kaven mybatis-plus1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.3.4.RELEASE 1.8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-webfluxcom.baomidou mybatis-plus-boot-starter3.4.0 mysql mysql-connector-java5.1.49 org.projectlombok lombokorg.springframework.boot spring-boot-maven-plugin
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 @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")这个一定要加上。
我们先在数据库中添加几行数据,方便演示。
我之前介绍的条件构造器都没有使用过condition参数。从AbstractWrapper
首先我们自己来实现一个和condition参数一样功能的方法。
查询username包含字符k,并且age属于[22 , 40 , 30 ]。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperConditionTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
String username = "k";
List ageList = Arrays.asList(22 , 40 , 30);
List userList = userMapper.selectList(condition(username , ageList));
userList.forEach(System.out::println);
}
public QueryWrapper condition(String username , List ageList){
QueryWrapper userQueryWrapper = new QueryWrapper<>();
if(!StringUtils.isEmpty(username)){
userQueryWrapper.like("username" , username);
}
if(!CollectionUtils.isEmpty(ageList)){
userQueryWrapper.in("age" , ageList);
}
return userQueryWrapper;
}
}
结果如下:
结果是正确的。
我们使用condition参数来实现一下。
package com.kaven.mybatisplus.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperConditionTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectList(){
String username = "k";
List ageList = Arrays.asList(22 , 40 , 30);
List userList = userMapper.selectList(condition(username , ageList));
userList.forEach(System.out::println);
}
public QueryWrapper condition(String username , List ageList){
QueryWrapper userQueryWrapper = new QueryWrapper<>();
// if(!StringUtils.isEmpty(username)){
// userQueryWrapper.like("username" , username);
// }
// if(!CollectionUtils.isEmpty(ageList)){
// userQueryWrapper.in("age" , ageList);
// }
userQueryWrapper.like(!StringUtils.isEmpty(username) , "username" , username)
.in(!CollectionUtils.isEmpty(ageList) , "age" , ageList);
return userQueryWrapper;
}
}
结果如下:
结果也是正确的。
到此这篇关于MyBatis-Plus条件构造器之condition参数的使用的文章就介绍到这了,更多相关MyBatis-Plus condition参数内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



