千家信息网

mybatis 3.5.0/mybatis plus 3.x中dao层与xml参数绑定的案例分析

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章将为大家详细讲解有关mybatis 3.5.0/mybatis plus 3.x中dao层与xml参数绑定的案例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相
千家信息网最后更新 2025年12月02日mybatis 3.5.0/mybatis plus 3.x中dao层与xml参数绑定的案例分析

这篇文章将为大家详细讲解有关mybatis 3.5.0/mybatis plus 3.x中dao层与xml参数绑定的案例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

方式1(不推荐) 单个参数方式

@Mapperpublic interface UserDao extends BaseMapper {   User selectList1(String username, String phone);}

注意:与网上所说的select * from user where username= #{0} and phone=#{1}在新版本中是不对的,会报错误:

nested exception is org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

查看报错报错信息可以看出还可以这样做

方式2(推荐) 实体类对象方式

该方法和之前版本是兼容的,没有变化

@Mapperpublic interface UserDao extends BaseMapper {    User selectList1(@Param("username") String username,@Param("phone") String phone);}

或者

 

这两种方式都可以的

方式3 使用bean工具类转成map,map类型参数(推荐)

bean工具类

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;        }        /**         * 将实体转换成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 oc = a.getClass();                if (oc == b || oc.getSuperclass() == b || checkInterfaces(oc.getInterfaces(), b)) {                        return true;                } else {                        return false;                }        }        private static boolean checkInterfaces(Class[] cArray, Class b) {                boolean tag = false;                for (Class c : cArray) {                        if (c == b) {                                tag = true;                                break;                        }                }                return tag;        }        /**         * Map List To Bean List         * @param bean         * @return         */        public static  List mapListToBeanList(List> listMap, Class bean) {                List beanList = new ArrayList(listMap.size());                for (Map map : listMap) {                        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();                                                setter.invoke(t, value);                                        }                                }                        } catch (Exception e) {                                e.printStackTrace();                        }                        beanList.add(t);                }                return beanList;        }        /**         * 将实体转换成Map         * 将key按驼峰规则转换成下划线分隔(字母大写)         * @param bean         * @return         */        public static Map beanToColumnMap(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();                                        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();        }        /**         * 字符串驼峰转下划线         *          * @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("[A-Z]([a-z\\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代码

 @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

 

或者

关于mybatis 3.5.0/mybatis plus 3.x中dao层与xml参数绑定的案例分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

方式 实体 参数 下划线 驼峰 推荐 案例 案例分析 分析 内容 字符 字符串 工具 文章 更多 知识 篇文章 不对 不错 代码 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全监测整改措施 工业园区网络技术服务怎么样 大数据时代信息网络安全 电脑服务器管理共享文件夹 软件开发公司购销合同做清单 数据库连接端口号 安全狗服务器在哪里 c 从数据库读出照片 科技互联网有些什么上市公司 明日之后服务器炸了有什么奖励 网络技术与安全专业就业怎样 广州志腾互联网科技和阿道夫 英雄联盟韩国几个服务器 软件开发与运维主要内容 湖南衡阳网络技术公司 如何提高数据库的写入速度 sql数据库名称 格式 大话西游2各服务器时间 2022年网络安全重保活动 java记录数据库更新操作 贵州视点科技数据通信网络技术 从事软件开发英文 数据库连接端口号 美信服务器连接异常怎么办 战地1 服务器连不上 学生网络安全宣传标语 sql代理服务器提示密码 后勤保障部网络安全工作会议 荧光pcr分析软件开发 宣城系统软件开发外包
0