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

How to query only some fields by Mybatis Plus select

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to achieve Mybatis Plus select query only part of the field, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Mybatis Plus select query partial fields

The Mybatis Plus select statement queries all fields by default. If you need to specify a field query, you need to use the select method of QueryWrapper.

Selectselect (String... SqlSelect) select (Predicate predicate) select (Class entityClass, Predicate predicate) sets the query field

Description:

The above methods are divided into two categories.

The second method is to filter the query field (except the primary key). The entity attribute in wrapper is required to have a value before the input parameter does not contain class. These two types of methods are called repeatedly for the last time.

Example: specify query primary key, name, age field

Select ("id", "name", "age")

Example: query attributes that begin with test

Select (I-> i.getProperty () .startsWith ("test"))

Example: query the data of all fields except manager_id and create_time in the User object

Select (User.class, info->! info.getColumn (). Equals ("manager_id") & &! info.getColumn (). Equals ("create_time") MyBatis-Plus select, delete 1, Mybatis-Plus query operations 1, query operations commonly used API

According to ID query, the test code is as follows:

/ / Test according to ID query @ Testpublic void testSelectById () {User user = userMapper.selectById (2L); System.out.println (user);}

The test results are as follows:

The batch query test code is as follows:

/ / Test batch query @ Testpublic void testSelectByBatchIds () {List users = userMapper.selectBatchIds (Arrays.asList (1,2,3)); users.forEach (System.out::println);}

The test results are as follows:

The test code for conditional query using map is as follows:

/ / use map operation @ Testpublic void testSelectByMap () {Map map = new HashMap (); map.put ("name", "Harry Oil"); map.put ("age", 18); List list = userMapper.selectByMap (map); list.forEach (System.out::println);}

The test results are as follows:

2. Paging query

Paging is often used in projects. Usually, pageHelpr plug-ins are used for paging, and you can also use the original limit for paging. MP actually has a built-in paging plug-in, and the steps are as follows:

2.1. Register the paging plug-in in the configuration class

/ / register the paging plug-in @ Beanpublic PaginationInterceptor paginationInterceptor () {return new PaginationInterceptor ();}

2.2.Use the built-in Page object of MP directly for paging. The test code is as follows

@ Testpublic void testPage () {/ / Parameter 1: current page, Parameter 2: page size Page page = new Page (1Power5); / / for sorting page.setDesc ("id"); IPage userIPage = userMapper.selectPage (page, null); userIPage.getRecords () .forEach (System.out::println); System.out.println (userIPage.getTotal ());}

The test results are as follows:

2. Deletion of Mybatis-Plus 1. API is commonly used in physical deletion

Delete the test code according to ID as follows:

/ / Test delete @ Testpublic void testDeleteById () {userMapper.deleteById (1L);}

The test results are as follows:

Delete the test code in batches according to ID as follows:

/ / batch delete @ Testpublic void testDeleteBatchId () {userMapper.deleteBatchIds (Arrays.asList (2L) 3L) via id;}

The test results are as follows:

The conditional deletion test code using map is as follows:

@ Testpublic void testDeleteMap () {Map map = new HashMap (); map.put ("name", "Harry Oil"); userMapper.deleteByMap (map);}

The test results are as follows:

2. Logical deletion

Logical deletion is sometimes needed in the work, that is, the data is not removed in the database, but is invalidated by a variable; the general administrator can view the logically deleted data, which can prevent data loss, similar to the Recycle Bin function.

The steps to delete MP logic are as follows:

2.1.Add deleted field to the database

2.2. add @ TableLogic annotation to the attribute corresponding to the entity class

2.3. Register the logical deletion component in the configuration class

/ / Registration logic delete component @ Beanpublic ISqlInjector sqlInjector () {return new LogicSqlInjector ();}

2.4.What is the definition of deleted or undeleted configuration in the application.properties file

Mybatis-plus.global-config.db-config.logic-delete-value=1mybatis-plus.global-config.db-config.logic-not-delete-value=0

2.5. Test logic deletion

The above is all the content of the article "how to query only some fields in Mybatis Plus select". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.

Share To

Development

Wechat

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

12
Report