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 use MyBatis Plus QueryWrapper and LambdaQueryWrapper

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to use MyBatis Plus QueryWrapper and LambdaQueryWrapper". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use MyBatis Plus QueryWrapper and LambdaQueryWrapper" can help you solve the problem.

If we have a banner_item table, we now need to find out all the data through banner_id (query List)

@ Datapublic class BannerItem {private Long id; private String name; private String img; private String keyword; private Integer type; private Long bannerId;} QueryWrapper

The most basic way to use it is as follows

/ / query condition constructor QueryWrapper wrapper = new QueryWrapper (); wrapper.eq ("banner_id", id); / / query operation List bannerItems = bannerItemMapper.selectList (wrapper)

Then we can introduce lambda to prevent us from writing banner_id-like hard code in our code.

QueryWrapper wrapper = new QueryWrapper (); wrapper.lambda (). Eq (BannerItem::getBannerId, id); List bannerItems = bannerItemMapper.selectList (wrapper); LambdaQueryWrapper

To simplify the use of lambda, we can rewrite it into a LambdaQueryWrapper constructor with the following syntax:

LambdaQueryWrapper wrapper = new QueryWrapper () .lambda (); wrapper.eq (BannerItem::getBannerId, id); List bannerItems = bannerItemMapper.selectList (wrapper)

We can simplify QueryWrapper.lambda () again to look like this.

LambdaQueryWrapper wrapper = new LambdaQueryWrapper (); wrapper.eq (BannerItem::getBannerId, id); List bannerItems = bannerItemMapper.selectList (wrapper); chain query

MyBatis-Plus also provides a chained query that works the same as the code above.

But this kind of writing tends to show off skills, readability is not as strong as the above code, you can choose your own way according to your needs.

List bannerItems = new LambdaQueryChainWrapper (bannerItemMapper) .eq (BannerItem::getBannerId, id) .list ()

If you want to query only one record, for example, query the details of a record through id, you can use .one (), for example

BannerItem bannerItem = new LambdaQueryChainWrapper (bannerItemMapper) .eq (BannerItem::getId, id) .one (); that's all for "MyBatis Plus QueryWrapper and how to use LambdaQueryWrapper". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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