In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use reflection to generate the relevant knowledge of QueryWrapper dynamic conditions in MyBatisPlus, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use reflection to generate QueryWrapper dynamic conditions in MyBatisPlus. Let's take a look.
1. problem
Code such as the following is often used in MyBatisPlus to construct query conditions:
QueryWrapper queryWrapper = new QueryWrapper (); if (userQuery.getId ()! = null) {queryWrapper.eq ("id", userQuery.getId ());} if (userQuery.getName ()! = null) {queryWrapper.like ("name", userQuery.getName ());} if (userQuery.getNickname ()! = null) {queryWrapper.like ("nickname", userQuery.getNickname ()) } if (userQuery.getAccount ()! = null) {queryWrapper.eq ("account", userQuery.getAccount ());} if (userQuery.getGender ()! = null) {queryWrapper.eq ("gender", userQuery.getGender ());} if (userQuery.getBirth2 ()! = null) {queryWrapper.ge ("birth", userQuery.getBirth2 ());} if (userQuery.getBirth3 ()! = null) {queryWrapper.le ("birth", userQuery.getBirth3 ()) } if (userQuery.getTel ()! = null) {queryWrapper.eq ("tel", userQuery.getTel ());} if (userQuery.getEmail ()! = null) {queryWrapper.eq ("email", userQuery.getEmail ());} if (userQuery.getQq ()! = null) {queryWrapper.eq ("qq", userQuery.getQq ());} if (userQuery.getWechat ()! = null) {queryWrapper.eq ("wechat", userQuery.getWechat ()) } if (userQuery.getHomePage ()! = null) {queryWrapper.eq ("home_page", userQuery.getHomePage ());} / / .baseMapper.selectPage (page, queryWrapper)
A large amount of such code can be generated through the reflection technology shown below, once and for all.
two。 Optimization scheme public class APP {public static void main (String [] args) throws Exception {QueryWrapper queryWrapper = new QueryWrapper (); Dept dept = Dept.builder () .deptno (1234) / / .dname ("sales") .loc ("NEWYORK") .build (); Method [] methods = Dept.class.getDeclaredMethods () For (Method method: methods) {String methodName = method.getName (); if (methodName.contains ("get")) {Object res = method.invoke (dept, null); if (res! = null) {String columnName = getColumnName (methodName); queryWrapper.eq (columnName,res) } System.out.println (queryWrapper); / / set a breakpoint here to view the value saved to QueryWrapper} / / get the field name of the corresponding table public static String getColumnName (String name) {StringBuffer sb = new StringBuffer (); name = name.substring (3, name.length ()) according to the name of the String name method. / / remove the get prefix char [] chars = name.toCharArray (); for (int I = 0; I
< chars.length; i++) { char item = chars[i]; if (item >65 & & item < 90) {/ / Capital letters sb.append ("_"); sb.append ((char) (item+32));} else {sb.append (item);}} return sb.deleteCharAt (0) .toString () }} this is the end of the article on "how to use reflection to generate QueryWrapper dynamic conditions in MyBatisPlus". Thank you for reading! I believe you all have a certain understanding of "how to use reflection to generate QueryWrapper dynamic conditions in MyBatisPlus". If you want to learn more, you are 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.
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.