登录
    Technology changes quickly but people's minds change slowly.

关于hutool 工具数据库查询结果映射不到对应resulthandler的问题

javaweb笔记 破玉 2831次浏览 0个评论

简介

  hutool 是一个很甜美的java开源工具包,简化了我们的开发工作。由几个大佬维护在github上,最近在项目中有些许小功能迫在眉睫要实现,于是想到了hutool进行快速开发。
hutool项目的地址 https://github.com/looly/hutool/

遇到的问题

   使用hutool连接数据库进行查询,经过调试发现jdbc可以查到结果,但是无法映射成beanlist,经过对hutool 源码的分析,发现在HandlerHelper中有这么一段代码如下:

	final T bean = ReflectUtil.newInstanceIfPossible(beanClass);
		//忽略字段大小写
		final Map propMap = BeanUtil.getBeanDesc(beanClass).getPropMap(true);
		String columnLabel;
		PropDesc pd;
		Method setter = null;
		Object value = null;
		for (int i = 1; i <= columnCount; i++) {
			columnLabel = meta.getColumnLabel(i);
			//驼峰命名风格
			pd = propMap.get(StrUtil.toCamelCase(columnLabel));
			setter = (null == pd) ? null : pd.getSetter();
			if(null != setter) {
				value = getColumnValue(rs, columnLabel,  meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
				ReflectUtil.invokeWithCheck(bean, setter, new Object[] {value});
			}
		}

就是上面代码注释中驼峰命名风格的那个地方,它将数据库中的字段名称转化为了 驼峰命名

将下划线方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
例如:hello_world=》helloWorld

然后代码:

pd = propMap.get(StrUtil.toCamelCase(columnLabel));

就去获取该属性,比如通过转换后的“”helloWorld“”去获取,但是如果我们的bean中命名不规范,比如bean中为hello_word,就会导致无法获取相应的属性,也就映射为空了。
所以针对以上问题,我们要规范我们的开发,代码格式要规范,最好采用阿里云java开发规范
参考 : https://www.cnblogs.com/hark0623/p/6374505.html


华裳绕指柔, 版权所有丨如未注明 , 均为原创|转载请注明关于hutool 工具数据库查询结果映射不到对应resulthandler的问题
喜欢 (4)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址