欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用

 更新時間:2020年12月23日 14:20:07   作者:ITKaven  
這篇文章主要介紹了MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

首先創(chuàng)建一個數(shù)據(jù)庫表,如下圖所示:

在這里插入圖片描述

然后創(chuàng)建一個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) ,表示該字段在數(shù)據(jù)庫中不存在 ,所以不會插入數(shù)據(jù)庫中
  * 使用 transient 、 static 修飾屬性也不會插入數(shù)據(jù)庫中
  */
 @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")這個一定要加上。

我們先在數(shù)據(jù)庫中添加幾行數(shù)據(jù),方便演示。

在這里插入圖片描述

我之前介紹的條件構(gòu)造器都沒有使用過condition參數(shù)。從AbstractWrapper<T, String, QueryWrapper<T>>的源碼可以看到很多方法都有condition參數(shù),它是一個布爾型的參數(shù),意思就是是否將該sql語句(像in()、like())加在總sql語句上,如下圖所示。

在這里插入圖片描述

首先我們自己來實現(xiàn)一個和condition參數(shù)一樣功能的方法。

查詢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;
 }
}

結(jié)果如下:

在這里插入圖片描述

結(jié)果是正確的。

我們使用condition參數(shù)來實現(xiàn)一下。

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;
 }
}

結(jié)果如下:

在這里插入圖片描述

結(jié)果也是正確的。

到此這篇關(guān)于MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用的文章就介紹到這了,更多相關(guān)MyBatis-Plus condition參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中使用?POI的示例代碼

    SpringBoot中使用?POI的示例代碼

    這篇文章主要介紹了SpringBoot中使用POI的實例詳解,包括引入poi的jar包和創(chuàng)建excel的實例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • java使用RestTemplate封裝post請求方式

    java使用RestTemplate封裝post請求方式

    這篇文章主要介紹了java使用RestTemplate封裝post請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringCache框架加載/攔截原理詳解

    SpringCache框架加載/攔截原理詳解

    這篇文章主要介紹了SpringCache框架加載/攔截原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 通過Java實現(xiàn)獲取表的自增主鍵值

    通過Java實現(xiàn)獲取表的自增主鍵值

    這篇文章主要為大家詳細(xì)介紹了如何通過Java實現(xiàn)獲取表的自增主鍵值,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的可以了解一下
    2023-06-06
  • springBoot整合redis做緩存具體操作步驟

    springBoot整合redis做緩存具體操作步驟

    緩存主要是將數(shù)據(jù)存在計算機(jī)的內(nèi)存當(dāng)中,以便于在使用的時候是可以實現(xiàn)快速讀取使用,它的快也是相對于硬盤讀取而言,這篇文章主要給大家介紹了關(guān)于springBoot整合redis做緩存的具體操作步驟,需要的朋友可以參考下
    2024-04-04
  • springboot+EHcache 實現(xiàn)文章瀏覽量的緩存和超時更新

    springboot+EHcache 實現(xiàn)文章瀏覽量的緩存和超時更新

    這篇文章主要介紹了springboot+EHcache 實現(xiàn)文章瀏覽量的緩存和超時更新,問題描述和解決思路給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-04-04
  • java類的加載過程以及類加載器的分析

    java類的加載過程以及類加載器的分析

    這篇文章給大家詳細(xì)講述了java類的加載過程以及類加載器的相關(guān)知識點內(nèi)容,有需要的朋友可以學(xué)習(xí)下。
    2018-08-08
  • HashMap原理的深入理解

    HashMap原理的深入理解

    這篇文章主要介紹了對HashMap原理的理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot使用ip2region獲取地理位置信息的方法

    SpringBoot使用ip2region獲取地理位置信息的方法

    這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Java基礎(chǔ)之throw和throws的示例詳解

    Java基礎(chǔ)之throw和throws的示例詳解

    throw是用來拋出一個具體的異常實例,而throws是用來聲明方法可能會拋出哪些類型的異常,是對調(diào)用者的一種通知和要求,這篇文章主要介紹了Java基礎(chǔ):throw和throws的詳解,需要的朋友可以參考下
    2024-06-06

最新評論