Mybatis的association使用子查询结果错误的问题解决
作者:陌殇殇
本文主要介绍了Mybatis的association使用子查询结果错误的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.li.application.mapper.LgsProfitForecastMapper"> <resultMap type="LgsProfitForecast" id="LgsProfitForecastResult"> <association property="predictionCompletionDateStr" select="selectPredictionCompletionDateStr" column="type" javaType="string"/> </resultMap> <select id="selectProfitForecastList" resultMap="LgsProfitForecastResult"> SELECT * FROM lgs_profit_forecast </select> <select id="selectPredictionCompletionDateStr" resultType="string"> select GROUP_CONCAT(prediction_completion_date) from ( select prediction_completion_date from lgs_profit_forecast where type = #{type} limit 4) a </select> </mapper>
需要子查询日期进行拼接
测试结果如图
问题1:association传入的column列自动映射为null
这个目前不清楚原因
解决办法
可以通过手动配置映射解决
<resultMap type="LgsProfitForecast" id="LgsProfitForecastResult"> <result property="type" column="type"/> <association property="predictionCompletionDateStr" select="selectPredictionCompletionDateStr" column="type" javaType="string"/> </resultMap>
测试:
可见赋值正常,但是子查询结果还是有问题
问题2:association子查询结果全部相同
查看控制台日志发现
实体类是int类型,但是传入参数自动变成boolean类型
数据库测试
存的是数字,但可以用boolean类型进行查询
查看数据表字段
结论
mybatis的association在传入字段时会自动进行数据库的类型映射,而tinyint标准的类型映射为boolean类型。所以1,2,3参数映射全部为true,导致结果一致,且tinyint列在查询时可以直接传入boolean类型查询,会自动映射为0和1
解决
1.修改为int或者varchar类型
2.tinyint的长度给大于1的数
3.数据库jdbc连接添加参数tinyInt1isBit=false
测试
到此这篇关于Mybatis的association使用子查询结果错误的问题解决的文章就介绍到这了,更多相关Mybatis的association 子查询 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- Mybatis中collection和association的使用区别详解
- mybatis利用association或collection传递多参数子查询
- Mybatis之association和collection用法
- 在Mybatis中association标签多层嵌套的问题
- mybatis中一对一关系association标签的使用
- MyBatis中association的基本使用方法
- mybatis的association传递参数问题示例
- Mybatis中一对多(collection)和一对一(association)的组合查询使用
- MyBatis的collection和association的使用解读
- mybatis中association标签的使用解读
- MyBatis使用嵌套查询collection和association的实现