In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "MyBatis-Plus selectMaps, selectObjs, selectCount, selectOne how to use" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "MyBatis-Plus selectMaps, selectObjs, selectCount, selectOne how to use" article.
First create a database table, as shown in the following figure:
Then create a Spring Boot project, pom.xml and configure as follows:
4.0.0 org.kaven mybatis-plus 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter- Test org.springframework.boot spring-boot-starter-webflux com.baomidou mybatis-plus-boot-starter 3.4.0 mysql mysql-connector-java 5.1.49 org.projectlombok Lombok org.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=falseserver: port: 8085logging: level: root: warn com.kaven.mybatisplus.dao: trace pattern: console:'% p%m%n'mybatis-plus: mapper-locations: classpath:mappers/*.xml
Entity class 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") @ Datapublic class User {@ TableId private String id; @ TableField ("username") private String username; @ TableField ("password") private String password; @ TableField ("age") private Integer age / * * use @ TableField (exist = false), indicating that the field does not exist in the database, so it will not be inserted into the database * using transient and static decorated attributes will not be inserted into the database * / @ TableField (exist = false) private String phone;}
Mapper API UserMapper:
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);}}
Startup class:
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") this must be added.
Let's first add a few rows of data to the database to facilitate the demonstration.
SelectMaps
Take a look at the source code:
/ * query all records according to the Wrapper condition * * @ param queryWrapper entity object encapsulation operation class (can be null) * / List selectMaps (@ Param (Constants.WRAPPER) Wrapper queryWrapper)
Method returns a value of type List, which is useful when we only need a small number of properties of the object or properties that the object does not have.
Let's not use selectMaps to implement only some of the properties of the object.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperMapsTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectList () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.select (User.class, e->! e.getColumn () .equals ("password")) .lt ("age", 50); List userList = userMapper.selectList (userQueryWrapper); userList.forEach (System.out::println);}}
The results are as follows:
Although the result is correct, the output user data has attributes that are null, which is not good when there are many attributes that are not needed.
Use selectMaps to implement only some of the properties of the object.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperMapsTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectMaps () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.select (User.class, e->! e.getColumn () .equals ("password")) .lt ("age", 50); List mapList = userMapper.selectMaps (userQueryWrapper); mapList.forEach (System.out::println);}}
The results are as follows:
The result is also correct, and there are no property values that are null.
Let's demonstrate how selectMaps returns attributes that don't exist in our entity class.
Group by age, and each set of age sum should be less than 60, and output average age, maximum age, minimum age.
This example may not be very good, but it shows the convenience of selectMaps, because the average age, maximum age, and minimum age are attributes that are not found in the User entity class.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperMapsTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectMaps2 () {QueryWrapper userQueryWrapper = Wrappers.query () UserQueryWrapper.select ("avg (age) avg_age", "min (age) min_age", "max (age) max_age") .groupBy ("age") .groupBy ("sum (age) < {0}", 60); List mapList = userMapper.selectMaps (userQueryWrapper); mapList.forEach (System.out::println);}}
The results are as follows:
The result is correct, so take a look at the output sql statement.
SelectObjs
Look at the source code:
/ * * query all records according to Wrapper conditions *
Note: only the value of the first field is returned
* * @ param queryWrapper entity object encapsulates the operation class (can be null) * / List selectObjs (@ Param (Constants.WRAPPER) Wrapper queryWrapper)
You can see from the comments in the source code that only the value of the first field is returned, which can be understood by demonstrating this method.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperOtherTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectObjs () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.select ("username", "age"). LikeRight ("username", "k"). Le ("age", 30); List objectList = userMapper.selectObjs (userQueryWrapper); objectList.forEach (System.out::println);}}
The results are as follows:
Obviously, the result is correct and only username is returned.
SelectCount
Look at the source code:
/ * query the total number of records according to the Wrapper condition * * @ param queryWrapper entity object encapsulation operation class (can be null) * / Integer selectCount (@ Param (Constants.WRAPPER) Wrapper queryWrapper)
This method actually returns the number of records that meet the criteria.
Let's demonstrate.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperOtherTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectCount () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.likeRight ("username", "k") .le ("age", 30); Integer count = userMapper.selectCount (userQueryWrapper); System.out.println (count);}}
The results are as follows:
The result is also correct.
SelectOne
Let's also take a look at the source code:
/ * query a record according to the entity condition * * @ param queryWrapper entity object encapsulation operation class (which can be null) * / T selectOne (@ Param (Constants.WRAPPER) Wrapper queryWrapper)
This method returns an entity, so if multiple pieces of data match, an error will be reported.
Let's demonstrate how to report an error.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperOtherTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectOneError () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.like ("username", "k"); User user = userMapper.selectOne (userQueryWrapper); System.out.println (user);}}
The error is as follows:
Let's demonstrate the correct situation again.
Package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.toolkit.Wrappers;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.HashMap;import java.util.List;import java.util.Map @ RunWith (SpringRunner.class) @ SpringBootTestpublic class UserMapperOtherTest {@ Autowired private UserMapper userMapper; @ Testpublic void selectOne () {QueryWrapper userQueryWrapper = Wrappers.query (); userQueryWrapper.like ("username", "kaven"); User user = userMapper.selectOne (userQueryWrapper); System.out.println (user);}}
The results are as follows:
The result is correct.
The above is about the content of this article on "how to use selectMaps, selectObjs, selectCount and selectOne of MyBatis-Plus". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.