Recently in use Mybatis-Plus, Um. , It's delicious !!! Let's talk about it today Mybatis-Plus How to use them

<>1, Condition query (QueryWrapper)

if , We need to check SQL The statement is as follows :
SELECT * FROM user_info WHERE 1=1 AND age = 20
Then the corresponding code can be :
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.eq(
"age", 20); List<UserInfo> list = userInfoMapper.selectList(queryWrapper );
The above is the query user table , Users older than 20 User information for

<>2, Condition query (QueryWrapper lambda)
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda
().eq(UserInfo::getAge, 20); List<UserInfo> list = userInfoMapper.selectList(
queryWrapper);
That's it QueryWrapper Of lambda Expression writing , This can avoid writing wrong field names , Or not converted to hump writing SQL error

<>3, Condition query (LambdaQueryWrapper)
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getAge, 20); List<UserInfo> list = userInfoMapper.
selectList(queryWrapper );
LambdaQueryWrapper And QueryWrapper Of lambda The writing method is basically consistent

<>4, Paging query
// Condition query LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getAge, 20); // Paging object Page<UserInfo> queryPage = new
Page<>(page, limit); // Paging query IPage<UserInfo> iPage = userInfoMapper.selectPage(
queryPage, queryWrapper); // Total data Long total = iPage.getTotal(); // user data List<
UserInfo> list = iPage.getRecords();
The above is paging query , If no conditional query is required ,queryWrapper Can be null

<>5, Paging query ( Joint table )

When we need to associate table pagination query , The first 3 We can't meet our needs any more , Then, we need to perform the linked table paging query

Suppose we need it SQL The statement is as follows :
SELECT a.*, b.`name` AS sex_text FROM user_info a LEFT JOIN user_sex b ON ( a.
sex= b.id ) WHERE 1 = 1 AND a.age > 20
Then we need to do the following :

* 1, newly build UserInfoVO.java import com.zyxx.entity.UserInfo; import lombok.Data;
@Data public class UserInfoVO extends UserInfo { // Gender private String sexText; }
* 2,UserInfoMapper.java in IPage<UserInfoVO> list(Page<UserInfoVO> page, @Param
(Constants.WRAPPER) Wrapper<UserInfoVO> queryWrapper);
* 3,UserInfoMapper.xml in <select id="list" resultType="com.zyxx.vo.UserInfoVO"
> SELECT a.*, b.`name` AS sex_text FROM user_info a LEFT JOIN user_sex b ON (
a.sex = b.id ) ${ew.customSqlSegment}</select>
* 4,UserInfoServiceImpl.java in // Condition query LambdaQueryWrapper<UserInfoVO>
queryWrapper= new LambdaQueryWrapper<>(); queryWrapper.eq(UserInfo::getAge, 20);
// Paging object Page<UserInfoVO> queryPage = new Page<>(page, limit); // Paging query IPage<
UserInfoVO> iPage = userInfoMapper.list(queryPage , queryWrapper); // Total data Long
total= iPage.getTotal(); // user data List<UserInfoVO> list = iPage.getRecords();
The above is paging query ( Joint table ) The operation of , This should be used more often

<>6,AND and OR

queryWrapper The default is according to and To connect , But in our business needs , I'm sure it will be used or To write SQL

* 1, primary
Suppose we need it SQL The statement is as follows : SELECT a.* FROM user_info a WHERE 1 = 1 AND a.id <> 1 AND (
a.`name` = 'jack' OR a.phone = '13888888888' )
So we can write like this :
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>(); // AND
a.id <> 1 queryWrapper.ne(UserInfo::getId, "1"); // AND ( a.`name` = 'jack' OR
a.phone = '13888888888' ) queryWrapper.and(i -> i.eq(UserInfo::getName, "jack").
or().eq(UserInfo::getPhone, "13888888888")); // Query results List<UserInfo> list =
userInfoMapper.selectList(queryWrapper);
The above is the primary and and or Combination

* 2, complex
Suppose we need it SQL The statement is as follows : SELECT a.* FROM user_info a WHERE 1 = 1 AND a.id <> 1 AND (
(a.`name` = 'jack' AND a.category = 1) OR (a.phone = '13888888888' OR a.category
= 2) )
So we can write like this :
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>(); // AND
a.id <> 1 queryWrapper.ne(UserInfo::getId, "1"); // AND ( (a.`name` = 'jack'
AND a.category = 1) OR (a.phone = '13888888888' OR a.category = 2) )
queryWrapper.and(i -> (i.and(j -> j.eq(UserInfo::getName, "jack").eq(UserInfo::
getCategory, 1))).or(j -> j.eq(UserInfo::getPhone, "13888888888").eq(UserInfo::
getCategory, 2))); // Query results List<UserInfo> list = userInfoMapper.selectList(
queryWrapper);
That's what makes the complex and and or Combination

So, in the future, we'll meet more complicated ones , You can continue with this rule SQL

<>7,@TableLogic

This note is Mybatis-Plus Logical deletion flag for , General comments are on the attributes of entities , If my user information table , Use a field (del_flag) Used to indicate the deletion status of data , as follows :
@ApiModelProperty(value = " deleted state (0-- Not deleted 1-- Deleted )") @TableField("del_flag")
@TableLogic private Integer delFlag;
Mybatis-Plus Default use 0 Indicates that it is valid ,1 Indicates that it is invalid , Of course, it can also be customized , There are two ways :

* 1, On the notes @ApiModelProperty(value = " deleted state (0-- Not deleted 1-- Deleted )") @TableField("del_flag"
) @TableLogic(value = "1", delval = "0") private Integer delFlag;
value = “” Default original value ,delval = “” Deleted value

* 2, configuration file # mybatis to configure mybatis-plus: # Global configuration global-config: db-config: # Tombstone global fields
( Default none Set to scan entity fields automatically ) logic-delete-field: delFlag # Tombstone global values ( default 1, Indicates that it has been deleted ) logic-delete
-value: 1 # The global value is not deleted logically ( default 0, Indicates not deleted ) logic-not-delete-value: 0
that , When we look up the data , It will automatically add query conditions for us
AND del_flag = 0
At this time, the logical deletion takes effect , When we execute userInfoMapper.removeById(id) Time , It will not be physically deleted , Actually executed SQL as follows :
UPDATE user_info set del_flag = 1 where id = #{id}
be careful : I'm here xml In the file SQL No logical deletion conditions will be added automatically

<>7, Specify query fields (select)

When we only need to query a few fields in the table , If the table data is large , We should not query all the fields in the table , If , What we need SQL as follows :
SELECT id, `name`, phone FROM user_info WHERE 1 = 1 AND age = 20
We just need to query the age equal to 20 Of users of id,name,phone, therefore , It can be written like this
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>(); // Query only
id,name,phone queryWrapper.select(UserInfo::getId, UserInfo::getName, UserInfo::
getPhone); // The query criteria are :age = 20 queryWrapper.eq(UserInfo::getAge, 20); List<
UserInfo> list = userInfoMapper.selectList(queryWrapper );
such , We need to query the data of all the fields in the table , Greatly shorten the query time

<>8, Query a piece of data (getOne)

getOne yes service Methods in , When we use getOne When a query returns a piece of data , We often don't use primary keys ID Go to the inquiry , If you use a primary key ID
Query a piece of data , We should use it :
UserInfo userInfo = userInfoService.getById(id);
If our user list has openId field , for example : Wechat users , This must be the only one , We need to use openId Query a user information , We usually use :
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getOpenId, openId); UserInfo userInfo =
userInfoService.getOne(queryWrapper);
But there are some risks , That is, if the same openId When there are two or more pieces of data , An exception will be thrown , So we need this :
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getOpenId, openId); UserInfo userInfo =
userInfoService.getOne(queryWrapper, false);
It can be seen that , One was passed in false parameter , The meaning of this parameter is :

* according to Wrapper, Query a record
* There are several result Do you want to throw an exception
By looking at the source code :
@Override public T getOne(Wrapper<T> queryWrapper, boolean throwEx) { if (
throwEx) { return baseMapper.selectOne(queryWrapper); } return SqlHelper.
getObject(log, baseMapper.selectList(queryWrapper)); }
If the input is true( Default value ), The query will not be processed , Otherwise, paging query is performed , Look again getObject() Method :
/** * from list Take the first data and return the corresponding data List Single result of generics in * * @param list ignore * @param <E> ignore
* @return ignore */ public static <E> E getObject(Log log, List<E> list) { if (
CollectionUtils.isNotEmpty(list)) { int size = list.size(); if (size > 1) { log.
warn(String.format("Warn: execute Method There are %s results.", size)); }
return list.get(0); } return null; }
The first data is returned by default , So when we query for multiple data , The first item is returned by default

If you find a deficiency in your reading , Welcome to leave a message !!!

Technology
©2019-2020 Toolsou All rights reserved,
Forbes China Auto rich list : He xiaopengdi 11 Li Xiangdi 14 Li Bindi 15 Huawei's limited old rivals benefit the most ? More and more people divide the cake ! Output string at specified position ( Detailed analysis )TypeScript- Polymorphism PHP Printout logPostgreSQL: Nine . Indexes el-select At the same time label and value value use VS2019 “Windows Desktop applications ” Module creation Win32 window npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test_vue_0613@1.0.0 dev: Solutions to errors vue use THREE.js Create a cube that you can control