当前位置: 代码网 > it编程>编程语言>Java > Mybatis的association使用子查询结果错误的问题解决

Mybatis的association使用子查询结果错误的问题解决

2025年07月07日 Java 我要评论
代码<?xml version="1.0" encoding="utf-8"?><!doctype mapper public "-//mybatis.org//dtd mapper

代码

<?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 子查询 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com