In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Mybatis Plus uses queryWrapper to achieve complex query", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Mybatis Plus use queryWrapper how to achieve complex query" bar!
Using queryWrapper to implement complex query / / mp implementation is responsible for query operation @ Test public void testSelectQuery () {/ / 1, creating QueryWrapper object QueryWrapper wrapper = new QueryWrapper () / / 2, set the condition / / ge greater than or equal to gt greater than le less than or equal to lt eq through QueryWrapper, ne is not equal to between like fuzzy query / / orderByDesc last splicing / / query age > = 30, the name of the first parameter field, the second parameter setting value wrapper.ge ("age", 30); List users = userMapper.selectList (wrapper) System.out.println (users); / / eq wrapper.eq ("name", "Tian"); List users1 = userMapper.selectList (wrapper); System.out.println (users1); wrapper.ne ("name", "Tian"); List users2 = userMapper.selectList (wrapper); System.out.println (users2) / / between// query age 20-30 wrapper.between ("age", 20jue 30); List users3 = userMapper.selectList (wrapper); System.out.println (users3); / / like wrapper.like ("Tian", "T"); List users4 = userMapper.selectList (wrapper); System.out.println (users4); / / orderByDesc wrapper.orderByAsc ("age") List users5 = userMapper.selectList (wrapper); System.out.println (users5); / / last splicing wrapper.last ("limit 1"); List users6 = userMapper.selectList (wrapper); System.out.println (users6); / / specify the column QueryWrapper select = wrapper.select ("id", "name") to be queried;} custom queryWrapper implementation query
I wanted to use QueryMapper instead of xml, so I rewrote QueryMapper in order to achieve that.
The original mybatis query method, such as querying a user, then the query method passes a user, then judges the attributes of the user in mapper.xml, and then splices the corresponding sql statements to realize the query at one time. Although you have a mybatisX code generator, or write your own code generator, it still feels a bit difficult to use with multiple tables. So, I use reflection to write a myAllEq method of QueryWrapper, which can pass in an entity class and directly query all attributes.
Declaration feed package com.maoyan.quickdevelop.common.core.domain;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import com.fasterxml.jackson.annotation.JsonFormat;import com.maoyan.quickdevelop.common.core.domain.dqabstract.DqStatusDispose;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import javax.validation.constraints.Email;import javax.validation.constraints.Size;import java.io.Serializable;import java.util.Date / * * @ author cat face * @ date on 2021-5-27 21:28 * user entity class * / @ Data@NoArgsConstructor@AllArgsConstructor@TableName (value = "dq_user") public class DqUser extends DqStatusDispose implements Serializable {private static final long serialVersionUID = 1L; / * * user ID * / * primary key uses TableId annotation, otherwise MybatisPlus uses id by default to query * / @ TableId (value = "user_id") private Long userId / * * user account * / @ TableField (value = "user_name") private String userName; / * * user nickname * / @ TableField (value = "nick_name") private String nickName; / * * user mailbox * / @ Email (message = "incorrect mailbox format") @ TableField (value = "email") private String email / * * Mobile number * / @ Size (min = 0, max = 11, message = "Mobile number cannot exceed 11 characters") @ TableField (value = "phone_number") private String phonenumber; / * * user gender 0 = male, 1 = female, 2 = unknown * / @ TableField (value = "sex") private String sex; / * * user profile * / @ TableField (value = "avatar") private String avatar / * * password * / @ TableField (value = "password") private String password; / * * account status (0 normal 1 disabled) * / @ TableField (value = "status") private String status; / * * deletion flag (0 means 1 means deletion, not intended for use) * / @ TableField (value = "delFlag") private String delFlag / * * Last login IP * / @ TableField (value = "loginIp") private String loginIp; / * * Last login time * / @ TableField (value = "loginDate") private Date loginDate; / * * user role ID * / @ TableField (value = "role") private String role; / * * Personality signature * * / @ TableField (value = "signature") private String signature / * * creation time * / @ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; / * * Update time * / @ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime;}
In my user entity class, I use lombak, which contains serialVersionUID.
Annotations are used to map database field names
Core code / * TODO Custom traversal query method * * @ param object * @ return com.baomidou.mybatisplus.core.conditions.query.QueryWrapper * @ author Cat face * @ date 9:20 * / public QueryWrapper myAllEq (T object) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {String getMethodName = "; Field field = null Class aClass = object.getClass (); Field [] declaredFields = aClass.getDeclaredFields (); / / QueryWrapper queryWrapper = new QueryWrapper (); / / the first one is serialVersionUID (rounding) I is 1 to bypass / / get all fields for of object (int I = 1; I < declaredFields.length; ifields +) {field = declaredFields [I] / / the member variable in the class is private, so you must do this operation field.setAccessible (true) / / you can't get the value directly, you can get the value by constructing the get method or calling the get method of the class / / get the method name through the containsIgnoreCase method of StringUtils / / through the above method, there is loop nesting Performance needs to be optimized / / so take stitching get method name / * * @ author cat face * @ date / / get attribute name * for (Field field: * declaredFields) {* System.out.println (field.getName ()) * methodNameFirst = field.getName (). Substring (0Power1). ToUpperCase (); * methodName = field.getName (); * methodName = "get" + methodNameFirst+methodName.substring (1); * System.out.println ("method name:" + methodName) * * / / optimize * System.out.println ("Optimization:" + "get" + field.getName (). Substring (0Magne1). ToUpperCase () + field.getName () .substring (1)) * 5:18 in the afternoon * @ param object * @ return com.baomidou.mybatisplus.core.conditions.query.QueryWrapper * / / get the get method name getMethodName = "get" + declaredFields [I] .getName (). Substring (0,1). ToUpperCase () + declaredFields [I] .getName (1); System.out.println (getMethodName) / / execute the get method to get the attribute value / / execute the method Method method = aClass.getMethod (getMethodName, null); Object getValue = method.invoke (object, null); / / System.out.println ("output:" + invoke) / / System.out.println ("Optimization:" + "get" + declaredFields [I] .getName (). Substring (0Power1). ToUpperCase () + declaredFields [I] .getName (). Substring (1); / / if the value is not empty, add if (getValue! = null) {/ / mapping / / return super.eq (, field.get (object)) to the eq / / get the value of its annotations / / later, you can use query method annotations to solve the problems required by different queries System.out.println ("value of table:" + field.getAnnotation (TableField.class). Value ()); System.out.println ("get get value:" + getValue.toString ()) MyQueryWrapper.this.eq (field.getAnnotation (TableField.class). Value (), getValue.toString ());}} return MyQueryWrapper.this;}
Can be used
/ / get all the method names / / but in the same order as in the class, so you can build the get method / / property name in the first uppercase, followed by get, get method name Method [] methods = aClass.getMethods (); for (Method method: methods) {System.out.println (method.getName ()) If (StringUtils.containsIgnoreCase (method.getName (), "get") & & StringUtils.containsIgnoreCase (method.getName (), "username") {System.out.println ("value:" + method.getName ());}}
StringUtils.containsIgnoreCase method to get the get method, but it will cause loop nesting, which has a certain impact on performance.
Therefore, I use the method of concatenating get method names to reduce the impact of useless loops.
According to the main points above, for starts at 1, because the first one is serialVersionUID, so to skip it. Starting with 1.
For xml, you can use queries such as like. Mine is applicable to the = query, so later, you will use custom comments on the attributes of the user class to determine the query method used in this field. For a field, some places want to use = to check, some places want to use like to check, then you can set the annotation priority to solve.
Thank you for reading, the above is the content of "how Mybatis Plus uses queryWrapper to achieve complex query". After the study of this article, I believe you have a deeper understanding of how Mybatis Plus uses queryWrapper to achieve complex query, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.