Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A detailed tutorial on how to integrate mybatis-plus with SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how SpringBoot integrates mybatis-plus advanced tutorials, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Introduction to wapper:

Wrapper: conditional construction abstract class, top parent class. Four methods are provided in the abstract class. The source code is displayed on the west side.

AbstractWrapper: the where condition used to encapsulate query conditions and generate sql

AbstractLambdaWrapper: the Lambda syntax uses Wrapper unified processing to parse lambda to get column.

LambdaQueryWrapper: if you look at the name, you can see that it is the query Wrapper used for Lambda syntax.

LambdaUpdateWrapper: Lambda update wrapper Wrapper

QueryWrapper: the Entity object encapsulates the operation class, not the lambda syntax

UpdateWrapper: Update conditional encapsulation for Entity object update operations

Conditional constructor AbstractWrapper

In the previous section we completed the mybatis-plus-based CRUD operation, and in this section we learned to use AbstractWrapper, the conditional constructor in mybatis-plus.

What is AbstractWrapper

The parent classes of QueryWrapper (LambdaQueryWrapper) and UpdateWrapper (LambdaUpdateWrapper) are used to generate the where condition of sql, and the entity attribute is also used to generate the where condition of sql.

Note: there is no association between the where conditions generated by entity and the where conditions generated using each api

First, let's introduce AbstractWrapper. The following figure shows an inheritance structure of AbstractWrapper:

2. QueryWrapper (LambdaQueryWrapper)

Because there are too many methods, I will not repeat them one by one. If you want to see all the methods and usage, you can see the introduction on the official website or see my example code.

1. QueryWrapper usage example

Query users whose id is between 1-30 and whose gender is male and whose name is test:

@ SpringBootTest@SuppressWarnings ("unchecked") public class UserWrapperTest {@ Autowired UserMapper userMapper; @ Test public void TestQueryWrapper () {QueryWrapper userQueryWrapper = new QueryWrapper (); userQueryWrapper.like ("name", "test"); userQueryWrapper.eq ("sex", "male"); userQueryWrapper.between ("id", 1pm 30); userMapper.selectList (userQueryWrapper) .forEach (System.out::println);}}

Note: eq is an abbreviation for equals. The two parameters of this method, column and value, indicate that the value of column and value should be equal.

Here, the like method queries the records that contain the word "test" in name; the between method has three parameters, column, value1, and value2, which means that the value of column should be between value1 and value2.

Note that column is the field corresponding to the data table, not the entity class attribute field.

Running log

2. LambdaQueryWrapper usage example

LambdaQueryWrapper is a conditional constructor object in mybatis plus, but you need to use Lambda syntax and Wrapper

@ Test public void TestLambdaQueryWrapper () {LambdaQueryWrapper lambdaWrapper = new LambdaQueryWrapper (); lambdaWrapper.like (User::getName, "test"); lambdaWrapper.eq (User::getSex, "male"); lambdaWrapper.between (User::getId,1,30); userMapper.selectList (lambdaWrapper) .forEach (System.out::println);}

Running log

3. UpdateWrapper (LambdaUpdateWrapper)

When we update or delete, sometimes if the where condition is complex, we can use UpdateWrapper to construct the condition.

Some of their methods are the same as the QueryWrapper (LambdaQueryWrapper) above. Here's a demonstration of the difference.

1. UpdateWrapper usage example

Change the user name that contains test1 to test1.1:

@ Test public void TestUpdateWrapper () {UpdateWrapper updateWrapper = new UpdateWrapper (); / / inherits from AbstractWrapper, and its own internal attribute entity is also used to generate where conditions / / and LambdaUpdateWrapper, which can be obtained through the new UpdateWrapper () .lambda () method! User user = User.builder (). Build (); / / modify statement updateWrapper.set ("name", "test1.1"); / / conditional updateWrapper.like ("name", "test1"); userMapper.update (user, updateWrapper);}

Running log

2. LambdaUpdateWrapper usage example @ Test public void TestLambdaUpdateWrapper () {LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper (); / / LambdaUpdateWrapper updateWrapper = new UpdateWrapper (). Lambda (); User user = User.builder (). Build (); / / modify statement updateWrapper.set (User::getName, "test1.1"); / / conditional updateWrapper.like (User::getName, "test1") UserMapper.update (user, updateWrapper);}

Plug-ins for mybatis-plus

Mybatis-plus provides many easy-to-use plug-ins that are easy to configure and easy to use. Let's take a look at how the plug-in for MP is used.

I. paging plug-in

Neither the selectPage method of BaseMapper nor the selectPage method provided by AR is physical paging. You need to configure the paging plug-in before it is physical, so let's see how to configure this plug-in now. Paging query

1. Configure the paging plug-in import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration / * @ program: springboot * @ description: * @ author: King * @ create: 2021-09-08 13:18 * / @ Configurationpublic class MybatisPlusConfig {/ * New paging plug-in, one ease and two slow follow the rules of mybatis, you need to set MybatisConfiguration#useDeprecatedExecutor = false to avoid cache problems * 3.4.3 do not set * / @ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor () Interceptor.addInnerInterceptor (new PaginationInnerInterceptor (DbType.H2)); return interceptor;}}

This configuration is excerpted from the official example

2. After the test code @ Autowired UserMapper userMapper; @ Test public void Test2 () {/ / has configured the paging plug-in, use the selectpage method, / / but now it is the real physical paging, with limit in the sql statement Page page = new Page (1,10); IPage selectPage = userMapper.selectPage (page, null); System.out.println (selectPage) System.out.println ("= relevant paging information ="); System.out.println ("Total number:" + selectPage.getTotal ()); System.out.println ("current Page number:" + selectPage.getCurrent ()); System.out.println ("Total pages:" + selectPage.getPages ()); System.out.println ("display number per Page:" + selectPage.getSize ()) System.out.println ("whether there is a previous page:" + page.hasPrevious ()); System.out.println ("whether there is a next page:" + page.hasNext ()); System.out.println ("query results:"); List list = selectPage.getRecords (); list.forEach (o-> System.out.println (o)) / / page.getRecords () .forEach (o-> System.out.println (o)); / / you can also set the query results into page.setRecords (list) in the page object; / / you can also call relevant methods through page to obtain relevant paging information, and you can also set the query results back to the page object to facilitate front-end use. }

Running result

Creating a new SqlSession

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17aa8a11] was not registered for synchronization because synchronization is not active

JDBC Connection [HikariProxyConnection@1963906615 wrapping com.mysql.cj.jdbc.ConnectionImpl@5190010f] will not be managed by Spring

= > Preparing: SELECT COUNT (*) FROM User

= > Parameters:

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report