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

MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)

 更新時(shí)間:2020年12月23日 11:02:30   作者:ITKaven  
這篇文章主要介紹了MyBatis-Plus 查詢指定字段的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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

在這里插入圖片描述

然后創(chuàng)建一個(gè)Spring Boot項(xiàng)目,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

實(shí)體類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ù)庫(kù)中不存在 ,所以不會(huì)插入數(shù)據(jù)庫(kù)中
   * 使用 transient 、 static 修飾屬性也不會(huì)插入數(shù)據(jù)庫(kù)中
   */
  @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> {}

啟動(dòng)類:

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")這個(gè)一定要加上。

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

在這里插入圖片描述

使用MyBatis-Plus 查詢時(shí)指定字段有兩種方法。

一:查詢username包含字符k,并且age要小于35,只需要輸出username、age即可。

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 java.util.Arrays;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

  @Autowired
  private UserMapper userMapper;

  @Test
  public void selectList(){
    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//    QueryWrapper<User> userQueryWrapper = Wrappers.query(); 和上面一樣的效果
    userQueryWrapper.select("username", "age").like("username" , "k").lt("age" , 35);
//    userQueryWrapper.like("username" , "k").lt("age" , 35).select("username", "age"); 
//    把select()放在最后面也可以,但我一般喜歡放在最前面,和sql語(yǔ)法保持一致
    List<User> userList = userMapper.selectList(userQueryWrapper);
    userList.forEach(System.out::println);
  }
}

結(jié)果如下:

在這里插入圖片描述

很顯然,結(jié)果是正確的。

當(dāng)數(shù)據(jù)庫(kù)表有很多列,并且需要輸出大部分列時(shí),用這種方法來(lái)指定字段查詢就很繁瑣,這也是不可避免的。

二:查詢username包含字符k,并且age要大于等于25并且小于等于35、password不能為null,不需要輸出password。

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 java.util.Arrays;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

  @Autowired
  private UserMapper userMapper;

  @Test
  public void selectList2(){
    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//    QueryWrapper<User> userQueryWrapper = Wrappers.query(); 和上面一樣的效果
    userQueryWrapper.select(User.class , e->!e.getColumn().equals("password")).like("username" , "k")
        .between("age" , 25 , 35)
        .isNotNull("password");
    List<User> userList = userMapper.selectList(userQueryWrapper);
    userList.forEach(System.out::println);
  }
}

結(jié)果如下:

在這里插入圖片描述

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

這兩種方法各有優(yōu)缺點(diǎn),可以互補(bǔ)使用。

到此這篇關(guān)于MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 查詢指定字段內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • feign調(diào)用中文參數(shù)被encode編譯的問(wèn)題

    feign調(diào)用中文參數(shù)被encode編譯的問(wèn)題

    這篇文章主要介紹了feign調(diào)用中文參數(shù)被encode編譯的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解Java的MyBatis框架中的事務(wù)處理

    詳解Java的MyBatis框架中的事務(wù)處理

    利用MyBatis框架的配置管理比直接使用JDBC API編寫事務(wù)控制要來(lái)得更加輕松,這里我們就來(lái)詳解Java的MyBatis框架中的事務(wù)處理,尤其是和Spring框架集成后更加exciting
    2016-06-06
  • java 二分法算法的實(shí)例

    java 二分法算法的實(shí)例

    這篇文章主要介紹了java 二分法算法的實(shí)例的相關(guān)資料,希望通過(guò)本文大家能夠掌握二分法,需要的朋友可以參考下
    2017-09-09
  • Spring Boot構(gòu)建系統(tǒng)安全層的步驟

    Spring Boot構(gòu)建系統(tǒng)安全層的步驟

    這篇文章主要介紹了Spring Boot構(gòu)建系統(tǒng)安全層的步驟,幫助大家更好的理解和學(xué)習(xí)使用Spring Boot框架,感興趣的朋友可以了解下
    2021-04-04
  • IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時(shí)候報(bào)decompiled.class file bytecode version:52.0(java 8)錯(cuò)誤的解決辦法

    IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時(shí)候報(bào)decompiled.class file byt

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA引入第三方j(luò)ar包或查看Java源碼的時(shí)候報(bào)decompiled.class file bytecode version:52.0(java 8)錯(cuò)誤的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • SpringBoot集成支付寶支付的實(shí)現(xiàn)示例

    SpringBoot集成支付寶支付的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot集成支付寶支付的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • ArrayList底層操作機(jī)制源碼解析

    ArrayList底層操作機(jī)制源碼解析

    這篇文章主要介紹了ArrayList底層操作機(jī)制源碼解析,當(dāng)創(chuàng)建ArrayList對(duì)象時(shí),如果使用的是無(wú)參構(gòu)造器,則初始elementData容量為0,第1次添加,則擴(kuò)容elementData為10,如需要再次擴(kuò)容,則擴(kuò)容elementData為1.5倍,需要的朋友可以參考下
    2023-09-09
  • Aop動(dòng)態(tài)代理和cglib實(shí)現(xiàn)代碼詳解

    Aop動(dòng)態(tài)代理和cglib實(shí)現(xiàn)代碼詳解

    這篇文章主要介紹了Aop動(dòng)態(tài)代理和cglib實(shí)現(xiàn)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java 括號(hào)匹配問(wèn)題案例詳解

    Java 括號(hào)匹配問(wèn)題案例詳解

    這篇文章主要介紹了Java 括號(hào)匹配問(wèn)題案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot實(shí)現(xiàn)接口參數(shù)加密解密的示例代碼

    SpringBoot實(shí)現(xiàn)接口參數(shù)加密解密的示例代碼

    加密解密本身并不是難事,問(wèn)題是在何時(shí)去處理?SpringMVC?中給我們提供了?ResponseBodyAdvice?和?RequestBodyAdvice,利用這兩個(gè)工具可以對(duì)請(qǐng)求和響應(yīng)進(jìn)行預(yù)處理,非常方便。廢話不多說(shuō),我們一起來(lái)學(xué)習(xí)一下
    2022-09-09

最新評(píng)論