A case study of binding between dao layer and xml parameters in mybatis 3.5.0/mybatis plus 3.x
This article will explain in detail the case study of binding between dao layer and xml parameters in mybatis 3.5.0/mybatis plus 3.x. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Mode 1 (not recommended) single parameter mode
@ Mapperpublic interface UserDao extends BaseMapper {User selectList1 (String username, String phone);} select * from user where username= # {arg0} and phone=# {arg1}
Note: select * from user where username= # {0} and phone=# {1} is incorrect in the new version and an error will be reported:
Nested exception is org.apache.ibatis.binding.BindingException: Parameter'0' not found. Available parameters are [arg1, arg0, param1, param2]
If you look at the error message, you can see that it can still be done.
Select * from user where username= # {param1} and phone=# {param2}
Mode 2 (recommended) entity class object mode
This method is compatible with the previous version and has no change.
@ Mapperpublic interface UserDao extends BaseMapper {User selectList1 (@ Param ("username") String username,@Param ("phone") String phone);} select * from user where username= # {username} and phone=# {phone}
Or
Select * from user where username= # {username} and phone=# {phone}
Both of these ways are fine.
Mode 3 converts to map,map type parameters using the bean utility class (recommended)
Bean utility class
Import com.alibaba.fastjson.JSONObject;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern Public class QueryEntityTools {public static Map objectToMap (Object obj) {if (obj = = null) {return null;} Map map = new HashMap (); try {Field [] declaredFields = obj.getClass () .getDeclaredFields () For (Field field: declaredFields) {field.setAccessible (true); map.put (field.getName (), field.get (obj));} catch (Exception e) {} return map } / * convert entities to Map * * @ param bean * @ return * / public static Map beanToMap (Object bean) {Class type = bean.getClass (); Map returnMap = new HashMap () Try {BeanInfo beanInfo = Introspector.getBeanInfo (type); PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors (); for (int I = 0; I)
< propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); if (readMethod != null) { Object result = readMethod.invoke(bean, new Object[0]); if (result != null) { returnMap.put(propertyName, result); } else { returnMap.put(propertyName, ""); } } } } } catch (Exception e) { returnMap.put("error", e.getMessage()); } return returnMap; } /** * 将实体转换成Json * * @param bean * @return */ public static JSONObject beanToJson(Object bean) { Class type = bean.getClass(); JSONObject json = new JSONObject(); try { BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); if (readMethod != null) { Object result = readMethod.invoke(bean, new Object[0]); if (result != null) { json.put(propertyName, result); } else { json.put(propertyName, ""); } } } } } catch (Exception e) { json.put("error", e.getMessage()); } return json; } /** * 将map转换成Json */ public static JSONObject mapToJson(Map map) { JSONObject json = new JSONObject(); for (String key : map.keySet()) { json.put(key, map.get(key)); } return json; } /** * 将map转换成实体 * * @param map * @param bean * @return */ public static T mapToBean(Map map, Class bean) { T t = null; try { BeanInfo beanInfo = Introspector.getBeanInfo(bean); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); t = bean.newInstance(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (map.containsKey(key)) { Object value = map.get(key); Method setter = property.getWriteMethod(); if (checkType(value, property.getPropertyType())) { setter.invoke(t, value); } } } } catch (Exception e) { e.printStackTrace(); } return t; } private static boolean checkType(Object a, Class b) { Class type = bean.getClass(); Map returnMap = new HashMap(); try { BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); Object result = readMethod.invoke(bean, new Object[0]); if (result != null && !result.equals("")) { returnMap.put(camelToUnderline(propertyName), result); } } } } catch (Exception e) { e.getMessage(); // returnMap.put("error", e.getMessage()); // returnMap = null; } return returnMap; } /** * 将camel Map转成Column Map * * @param bean * @return */ public static Map camelMapToColumnMap(Map beanMap) { Map returnMap = new HashMap(); try { for (String key : beanMap.keySet()) { returnMap.put(camelToUnderline(key), beanMap.get(key)); } } catch (Exception e) { e.printStackTrace(); } return returnMap; } /** * 字符串下划线转驼峰 * * @param line * @return */ public static String underlineToCamel(String line) { if (line == null || "".equals(line)) { return ""; } StringBuffer sb = new StringBuffer(); Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?"); Matcher matcher = pattern.matcher(line); while (matcher.find()) { String word = matcher.group(); Character c = word.charAt(0); sb.append(matcher.start() == 0 ? Character.toLowerCase(c) : Character.toUpperCase(c)); int index = word.lastIndexOf('_'); if (index >0) {sb.append (word.substring (1, index). ToLowerCase ());} else {sb.append (word.substring (1). ToLowerCase ());}} return sb.toString () } / * string hump underlined * * @ param line * @ return * / public static String camelToUnderline (String line) {if (line = = null | | ".equals (line)) {return" } Character c = line.charAt (0); String f = line.substring (1); line = String.valueOf (c). ToUpperCase (). Concat (f); StringBuffer sb = new StringBuffer (); Pattern pattern = Pattern.compile ("[Amurz] ([Amurz\\ d] +)?) Matcher matcher = pattern.matcher (line); while (matcher.find ()) {String word = matcher.group (); sb.append (word.toUpperCase ()); sb.append (matcher.end () = = line.length ()? ":" _ ") } return sb.toString ();}}
Impl code
@ Override public User selectList1 (User user) {Map map = QueryEntityTools.beanToMap (user); return baseMapper.selectList1 (map);}
Mappper
@ Mapperpublic interface UserDao extends BaseMapper {User selectList1 (Map map);}
Xml
Select * from user where username= # {username} and phone=# {phone}
Or
Select * from user where username= # {username} and phone=# {phone} case study on binding between dao layer and xml parameters in mybatis 3.5.0/mybatis plus 3.x ends here. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.