MyBatis-Plus 查詢指定字段的實現(xiàn)
首先創(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ù),方便演示。

使用MyBatis-Plus 查詢時指定字段有兩種方法。
一:查詢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語法保持一致
List<User> userList = userMapper.selectList(userQueryWrapper);
userList.forEach(System.out::println);
}
}
結(jié)果如下:

很顯然,結(jié)果是正確的。
當數(shù)據(jù)庫表有很多列,并且需要輸出大部分列時,用這種方法來指定字段查詢就很繁瑣,這也是不可避免的。
二:查詢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)缺點,可以互補使用。
到此這篇關(guān)于MyBatis-Plus 查詢指定字段的實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 查詢指定字段內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud對服務內(nèi)某個client進行單獨配置的操作步驟
我們的微服務項目用的是springCloud,某個微服務接口因為數(shù)據(jù)處理量大,出現(xiàn)了接口超時的情況,我們需要單獨修改這一個feignClient的超時時間,所以本文介紹了SpringCloud對服務內(nèi)某個client進行單獨配置的操作步驟,需要的朋友可以參考下2023-10-10
Java中struts2和spring MVC的區(qū)別_動力節(jié)點Java學院整理
這篇文章主要介紹了Java中struts2和spring MVC的區(qū)別,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09
在idea中設(shè)置項目編碼格式為UTF-8的操作方法
idea中的默認編碼為GBK,在開發(fā)過程中一般將編碼格式改為UTF-8,所以本文給大家介紹了在idea中設(shè)置項目編碼為UTF-8的操作方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-12-12
SpringData Repository Bean方法定義規(guī)范代碼實例
這篇文章主要介紹了SpringData Repository Bean方法定義規(guī)范代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
SpringCloud實戰(zhàn)之Feign聲明式服務調(diào)用
這篇文章主要介紹了SpringCloud實戰(zhàn)之Feign聲明式服務調(diào)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解
在Spring Boot應用的開發(fā)中,不管是對底層數(shù)據(jù)庫操作,對業(yè)務層操作,還是對控制層操作,都會不可避免的遇到各種可預知的,不可預知的異常需要處理,如果每個處理過程都單獨處理異常,那么系統(tǒng)的代碼耦合度會很高,工作量大且不好統(tǒng)一,以后維護的工作量也很大2022-10-10
springboot 用監(jiān)聽器統(tǒng)計在線人數(shù)案例分析
這篇文章主要介紹了springboot 用監(jiān)聽器統(tǒng)計在線人數(shù)案例分析,質(zhì)是統(tǒng)計session 的數(shù)量,思路很簡單,具體實例代碼大家參考下本文2018-02-02

