mybatis 一对多条件查询(mybatis collection 多条件查询的实现方法)
类别:数据库 浏览量:2194
时间:2021-10-26 11:06:01 mybatis 一对多条件查询
mybatis collection 多条件查询的实现方法mybatis collection 多条件查询的实现方法
前言:
业务需要通过mybatis 查询返回嵌套集合,嫌多次查询太麻烦,用自带的高级查询解决问题,下边是代码,已测试通过。
说下自己的理解,就是一个主查询结果集里面嵌套了子查询的结果集,可以是多个子查询,每个子查询的条件从主查询结果集中获取,返回值各自定义。collection 标签的property是主查询里面集合的名字,如果有多个就再写个collection,column是子查询参数,单参数直接写主查询结合返回结果,例如直接写上user_id,要是数据库的字段,多条件就封装下,例如{userId=user_id,theme=theme},然后子查询的parameterType写"java.util.Map",多条件查询好像只有mybatis3.0以后才有,看网上资料说的,没验证过,ofType是集合里的对象,select是对应下面的语句
实现代码:
<resultMap id="BaseResultMap" type="web.model.UserMessage" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_id" property="userId" jdbcType="INTEGER" /> <result column="theme" property="theme" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="INTEGER" /> <result column="theme_time" property="themeTime" jdbcType="TIMESTAMP" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> <result column="yn" property="yn" jdbcType="INTEGER" /> </resultMap> <resultMap id="BaseVoResultMap" type="web.model.vo.UserMessageVo" extends="BaseResultMap"> <collection property="userMessageDetailList" column="{userId=user_id,theme=theme}" javaType="java.util.ArrayList" ofType="web.model.UserMessageDetail" select="selectUserMessageDetailById"/> </resultMap> <resultMap id="BaseDetailResultMap" type="web.model.UserMessageDetail" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_message_id" property="userMessageId" jdbcType="INTEGER" /> <result column="sponsor_id" property="sponsorId" jdbcType="INTEGER" /> <result column="user_id" property="userId" jdbcType="INTEGER" /> <result column="user_type" property="userType" jdbcType="INTEGER" /> <result column="provider_id" property="providerId" jdbcType="INTEGER" /> <result column="message" property="message" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="INTEGER" /> <result column="message_time" property="messageTime" jdbcType="TIMESTAMP" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> <result column="yn" property="yn" jdbcType="INTEGER" /> </resultMap> <select id="selectUserMessageById" resultMap="BaseVoResultMap" parameterType="java.lang.Integer"> select * from user_message where user_id = #{userId,jdbcType=INTEGER} </select> <select id="selectUserMessageDetailById" resultMap="BaseDetailResultMap" parameterType="java.util.Map"> select * from user_message_detail where user_id = #{userId,jdbcType=INTEGER} and message = #{theme,jdbcType=VARCHAR} </select>
selectUserMessageById,这个是主查询,VO只多了个 private List<UserMessageDetail> userMessageDetailList,
这个属性,下面是返回值结构截图
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣
- mybatis 分页查询配置(mybatis-plus分页传入参数后sql where条件没有limit分页信息操作)
- mybatis 一对多条件查询(mybatis collection 多条件查询的实现方法)
- idea安装mybatis(IDEA使用mybatis-generator及配上mysql8.0.3版本遇到的bug)
- mybatis动态sql框架(Mybatis4 之Mybatis动态sql的实现代码)
- mybatissql解析(mybatis动态sql实现逻辑代码详解)
- mybatis如何插入空格字符串(MyBatis SQL xml处理小于号与大于号正确的格式)
- mybatis为什么还用mysql(关于MyBatis连接MySql8.0版本的配置问题)
- mybatis执行sql源码解析(mybatis调用sqlserver存储过程返回结果集的方法)
- mybatis批量插入报错(解决myBatis中删除条件的拼接问题)
- mybatis测试出现空指针(Mybatis非配置原因,导致SqlSession was not registered for synchronization异常)
- mybatissql解析(mybatis动态sql常用场景总结)
- 盘古开天地 他创造了世界,谁创造了盘古 盘古是伏羲吗(盘古开天地他创造了世界)
- 关于队徽 你了解这些么 二(关于队徽你了解这些么)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
- 0 1 岁婴儿最强作息指南,照着做养出天使宝宝(01岁婴儿最强作息指南)
- 沪上这16所高校 萌新 礼包开箱 哪一款让你心动(沪上这16所高校萌新)
- 她救了被绑架的他,而这一切竟是一场阴谋...(她救了被绑架的他)
热门推荐
- 如何提高移动网站的用户体验
- mysql数据库基本使用方法(详解MySQL 数据库范式)
- html5 页面向上滑动(html5手机键盘弹出收起的处理)
- mysql是自动commit吗(详解MySQL与Spring的自动提交autocommit)
- mysql join规则(浅谈为什么MySQL不推荐使用子查询和join)
- html5做app流程(html5唤醒APP小记)
- mvc中使用uploadify批量上传
- pyinstaller打包后为什么报错(Pyinstaller打包.py生成.exe的方法和报错总结)
- php-fpm配置文件在哪里(PHP-FPM 设置多pool及配置文件重写操作示例)
- docker-compose 原理(详解docker compose 用法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9