mybatis 一对多映射踩过的坑
实体类Sharing(省略get set):
public class Sharing implements Serializable { private Long sharingId; private Long userId; private Integer sharingTypeId; private String sharingDescribe; private String sharingUrl; private Integer sharingState; }
实体类SharingType(省略get set):
public class SharingType implements Serializable{ private Integer sharingTypeId; private Long userId; private String sharingName; private Integer sharingTypeState; private List<Sharing> sharingList; }
SharingMapper.xml:
<resultMap id="BaseResultMap" type="com.lemon.boboke.entity.Sharing"> <id column="sharing_id" jdbcType="BIGINT" property="sharingId" /> <result column="user_id" jdbcType="BIGINT" property="userId" /> <result column="sharing_type_id" jdbcType="INTEGER" property="sharingTypeId" /> <result column="sharing_describe" jdbcType="VARCHAR" property="sharingDescribe" /> <result column="sharing_url" jdbcType="VARCHAR" property="sharingUrl" /> <result column="sharing_state" jdbcType="INTEGER" property="sharingState" /> </resultMap>
SpringTypeMapper.xml:
<resultMap id="BaseResultMap" type="com.lemon.boboke.entity.SharingType"> <id column="sharing_type_id" jdbcType="INTEGER" property="sharingTypeId" /> <result column="user_id" jdbcType="BIGINT" property="userId" /> <result column="sharing_name" jdbcType="VARCHAR" property="sharingName" /> <result column="sharing_type_state" jdbcType="INTEGER" property="sharingTypeState" /> <collection property="sharingList" resultMap="com.lemon.boboke.dao.SharingMapper.BaseResultMap"/> </resultMap>
踩坑的地方(返回类型:resultType="com.lemon.boboke.entity.SharingType"):
<select id="selectAllGroupByType" resultType="com.lemon.boboke.entity.SharingType"> SELECT sht.*,shar.sharing_id,shar.sharing_describe,shar.sharing_url,shar.sharing_state FROM `sharing_type` AS sht inner JOIN `sharing` AS shar ON sht.sharing_type_id = shar.sharing_type_id WHERE sht.user_id = (SELECT `user_id` FROM `user` WHERE `user`.user_account = #{userAccount}) AND sht.sharing_type_state = 0 AND shar.sharing_state = 0 </select>
因为是多表查询,返回结果应该是新的映射BaseResultMap
本文来自博客园,作者:可可西里(lemon),转载请注明原文链接:https://www.cnblogs.com/ldl326308/p/12720353.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现