温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

condition参数如何在MyBatis-Plus中使用

发布时间:2020-12-23 16:52:52 来源:亿速云 阅读:712 作者:Leah 栏目:开发技术

这篇文章给大家介绍condition参数如何在MyBatis-Plus中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

首先创建一个数据库表,如下图所示:

condition参数如何在MyBatis-Plus中使用

然后创建一个Spring Boot项目,pom.xml和配置如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>org.kaven</groupId>  <artifactId>mybatis-plus</artifactId>  <version>1.0-SNAPSHOT</version>  <parent>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-parent</artifactId>   <version>2.3.4.RELEASE</version>   <relativePath/>  </parent>  <properties>   <java.version>1.8</java.version>  </properties>  <dependencies>   <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter</artifactId>   </dependency>   <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>   </dependency>   <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-webflux</artifactId>   </dependency>   <dependency>    <groupId>com.baomidou</groupId>    <artifactId>mybatis-plus-boot-starter</artifactId>    <version>3.4.0</version>   </dependency>   <dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>5.1.49</version>   </dependency>   <dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>   </dependency>  </dependencies>  <build>   <plugins>    <plugin>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-maven-plugin</artifactId>    </plugin>   </plugins>  </build> </project>
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<User> {}

启动类:

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参数如何在MyBatis-Plus中使用

我之前介绍的条件构造器都没有使用过condition参数。从AbstractWrapper<T, String, QueryWrapper<T>>的源码可以看到很多方法都有condition参数,它是一个布尔型的参数,意思就是是否将该sql语句(像in()like())加在总sql语句上,如下图所示。

condition参数如何在MyBatis-Plus中使用

首先我们自己来实现一个和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<Integer> ageList = Arrays.asList(22 , 40 , 30);   List<User> userList = userMapper.selectList(condition(username , ageList));   userList.forEach(System.out::println);  }  public QueryWrapper<User> condition(String username , List<Integer> ageList){   QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();   if(!StringUtils.isEmpty(username)){    userQueryWrapper.like("username" , username);   }   if(!CollectionUtils.isEmpty(ageList)){    userQueryWrapper.in("age" , ageList);   }   return userQueryWrapper;  } }

结果如下:

condition参数如何在MyBatis-Plus中使用

结果是正确的。

我们使用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<Integer> ageList = Arrays.asList(22 , 40 , 30);   List<User> userList = userMapper.selectList(condition(username , ageList));   userList.forEach(System.out::println);  }  public QueryWrapper<User> condition(String username , List<Integer> ageList){   QueryWrapper<User> 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;  } }

结果如下:

condition参数如何在MyBatis-Plus中使用

关于condition参数如何在MyBatis-Plus中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI