站在巨人的肩膀上

https://blog.csdn.net/liaoxiaohua1981/article/details/6862466

聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);对应的数据库表的列名称;
不同情况需要告诉MyBatis 如何加载一个聚集。MyBatis 可以用两种方式加载:

划重点:collection中的column根据实践结果得出的白话总结

column可以这样写

column="USER_NAME"
也可以这样写
column="{id=id,userName213=user_name}"
value必须是外层查询的结果字段,必须得有,前面的key,你可以在子查询中作为条件参数去where
下面的写法就是分两层,外层的就是查询用户数量,子查询中返回的是每个用户对应的角色集合,这样子做主要是用来做分页的时候用,切记!!!
  <resultMap id="userResultCollection" type="com.cloudwalk.shark.model.User">
<id property="id" column="ID" jdbcType="INTEGER"></id>
<result property="userName" column="user_name" jdbcType="VARCHAR"></result>
<collection property="roleList" select="selectRoles" column="{id=id,userName213=user_name}" ofType="com.cloudwalk.shark.model.Role" >
</collection>
</resultMap> <select id="selectRoles" resultType="com.cloudwalk.shark.model.Role">
select role_name from t_shark_user t join t_shark_role r
where t.id = r.user_id
</select> <select id="queryAllUser" resultMap="userResultCollection">
SELECT u.id, user_name FROM t_shark_user u JOIN t_shark_role r ON u.id = r.user_id GROUP BY u.id,user_name
</select>
如果你是合并到一起写的
 <resultMap id="userResultCollection" type="com.cloudwalk.shark.model.User">
<id property="id" column="ID" jdbcType="INTEGER"></id>
<result property="userName" column="user_name" jdbcType="VARCHAR"></result>
<collection property="roleList" ofType="com.cloudwalk.shark.model.Role" >
<result property="roleName" column="role_name" jdbcType="VARCHAR"></result>
</collection>
</resultMap> <select id="queryAllUser" resultMap="userResultCollection">
SELECT u.id, user_name,role_name FROM t_shark_user u JOIN t_shark_role r ON u.id = r.user_id
</select>

column有没有就无所谓了,我随便乱写也是OK的,因为这样子就只有一个查询语句,column根本就没有任何意义!!!!!

    <resultMap id="userResultCollection" type="com.cloudwalk.shark.model.User">
<id property="id" column="ID" jdbcType="INTEGER"></id>
<result property="userName" column="user_name" jdbcType="VARCHAR"></result>
<collection property="roleList" column="sdfasd" ofType="com.cloudwalk.shark.model.Role" >
<result property="roleName" column="role_name" jdbcType="VARCHAR"></result>
</collection>
</resultMap>

结果是OK的,哈哈

1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活但会将执行多次嵌套的SQL语句。
2. resultMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

两种加载方式格式如下:
1.集合的嵌套查询(select)

<collection property="Java属性名" ofType="另一Java类名" javaType="ArrayList" column="关联主键ID(用于嵌套查询SQL语句传入参数,多个用逗号分开)" select="另一个select映射SQL的ID"/>

<select parameterType="int" resultType="另一Java类名" id="另一个select映射SQL的ID">

SQL语句

<select>

注意:column属性的值必须与相应的SQL查询语句中的列名相同。MyBatis会将第一条SQL语句查询出来的该列的值用于所嵌套的SQL映射语句的入参。因第一条SQL语句查询出来的每个该列的值都将用于执行另一个SQL语句,所以嵌套的SQL语句将被多次执行。

2.集合的嵌套结果(resultMap)

<collection property="Java属性名" ofType="另一Java类名" javaType="ArrayList" resultMap="另一个resultMap的ID"/>

<resultMap="另一个resultMap的ID" type="另一Java类名">

<id property="id" column="关联主键ID"/>

........

</resultMap>

注意:column属性的值必须与相应的SQL查询语句的列名一样。

集合的嵌套查询(select)示例:

<?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.myapp.mapper.UserMapper">
<select id="getUserList" resultMap="userdetailResult">
select * from t_user where id between 1 and 10
</select> <select id="selectRoles" resultType="com.myapp.domain.Role" parameterType="int">
select * from t_user_role a,t_role b where a.user_id=#{id} and a.role_id=b.id
</select> <resultMap id="userdetailResult" type="User">
<id property="id" column="user_id" />
<result property="name" column="user_name"/>
<result property="createDate" column="create_date"/>
<collection property="roles" ofType="Role" javaType="ArrayList" column="id" select="selectRoles"/>
</resultMap>
</mapper>

集合的嵌套结果(result)示例:

<?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.myapp.mapper.UserMapper">
<select id="getUserList" resultMap="userdetailResult">
SELECT
u.id as user_id,
u.name as user_name,
u.create_date,
r.id as role_id,
r.name as role_name
FROM t_user u
LEFT JOIN t_user_role ur ON(u.id=ur.user_id)
LEFT JOIN t_role r ON(r.id=ur.role_id) where u.id=1
</select> <resultMap id="userdetailResultNew" type="User">
  <id property="id" column="user_id" />
  <result property="name" column="user_name"/>
  <result property="createDate" column="create_date"/>
  <collection property="roles" ofType="Role" javaType="ArrayList">
    <id property="id" column="role_id"/>
    <result property="name" column="role_name"/>
  </collection>
</resultMap> <resultMap id="roleResult" type="Role">
  <id property="id" column="role_id"/>
  <result property="name" column="role_name"/>
</resultMap> <resultMap id="userdetailResult" type="User">
  <id property="id" column="user_id" />
  <result property="name" column="user_name"/>
  <result property="createDate" column="create_date"/>
  <collection property="roles" ofType="Role" javaType="ArrayList" resultMap="roleResult"/>
</resultMap>
</mapper>

如果你只是简单的嵌套,可以像id="userdetailResultNew" 那样将要嵌套的结果直接写在collection子元素中去。

下面关于这个Collection中的column具体什么意思看下下面的教程
===========================================================================================================================================

mybatis collection column 传常量

 
 版权声明:本文为博主原创文章,转载请标明出处哦。 https://blog.csdn.net/sinat_32034679/article/details/78727612

想要在mybatis 的collection关联查询中,添加一个常量:classifyId=1作为参数,原先使用的添加方式为:

<collection property="imageList" column="{aaaId=aaa_id,classifyId='1'}"
javaType="ArrayList"
select="com.fsti.information.dao.ImageManageMapper.queryGoodsImage">
</collection>

会报找不到行:”1 “的错误。

需要将关联的语句改为:

 <resultMap id="GoodsVO" type="com.fsti.aaa.bean.vo.aaaVO" >
<collection property="imageList" column="{aaaId=aaa_id,classifyId=classifyId}"
javaType="ArrayList"
select="com.fsti.information.dao.ImageManageMapper.queryGoodsImage">
</collection>
<collection property="goodsTags" column="{goodsId = goods_id}"
javaType="ArrayList"
select="com.fsti.goods.dao.GoodsTagsReleMapper.queryGoodsTags">
</collection>
</resultMap>

基础查询的语句改为:

<select id="queryAaaVO" resultMap="aaaVO" parameterType="java.util.Map" >
select
<include refid="Base_Column_List" />,
1 as classifyId
from aaa
where
aaa_id=#{aaaId,jdbcType=BIGINT}
</select>
也就是在查询时添加一句 1 as classifyId 

然后,将其作为变量在column中引用即可:classifyId=classifyId

最后给大家看下如果不存在mybatis给的错误提示,这样一下子就能明白了  

"message": "nested exception is org.apache.ibatis.executor.result.ResultMapException:

Error attempting to get column 'aaa_id' from result set.
Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'aaa_id' from result set.
Cause: java.sql.SQLException: Column 'aaa_id' not found.",

MyBatis嵌套Collection的更多相关文章

  1. mybatis的collection查询问题以及使用原生解决方案的结果

    之前在springboot+mybatis环境的坑和sql语句简化技巧的第2点提到,数据库的一对多查询可以一次查询多级数据,并且把拿到的数据按id聚合,使父级表和子级表都有数据. 但是这种查询,必然要 ...

  2. coding++:mybatis 嵌套查询子查询column传多个参数描述

    mybatis 嵌套查询子查询column传多个参数如下: 2.代码示例 备注:注意,相同颜色的单词都是有关联的 <resultMap id="blogResult" typ ...

  3. Mybatis之collection嵌套查询mapper文件写法

    mapper.xml写法举例 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...

  4. mybatis嵌套map或者map嵌套的parameterType

    Spring的重要注解 https://www.cnblogs.com/rolandlee/p/11014923.html 一:首先是map嵌套: 例1: 例2: 总结: paramterType无论 ...

  5. mybatis使用collection查询集合属性规则

    接上篇mybatis使用associaton进行分步查询 相关的类还是上篇中的类. 查询部门的时候将部门对应的所有员工信息也查询出来 DepartmentMapper.xml <!--嵌套结果集 ...

  6. Mybatis之collection与association标签

    collection与association标签的功能就是为了解决查询条件映射到一个类或一个集合上,适用于对于多对一,一对多的映射结果,现在我们就探究其具体使用吧. 环境搭建: 数据库搭建 CREAT ...

  7. mybatis 嵌套查询与懒加载

    懒加载:对于页面有很多静态资源的情况下(比如网商购物页面),为了节省用户流量和提高页面性能,可以在用户浏览到当前资源的时候,再对资源进行请求和加载. fetchType="lazy" ...

  8. MyBatis中collection (一对一,一对多)

    MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...

  9. mybatis ForEach Collection集合等规范解析(转)

    转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...

随机推荐

  1. Visitor Pattern

    1.Visitor模式:将更新(变更)封装到一个类中(访问操作),并由待更改类提供一个接收接口,则可在不破坏类的前提下,为类提供增加新的新操作. 2.Visitor模式结构图 Visitor模式的关键 ...

  2. ubuntu搭建mysql

    步骤1 – 安装MySQL 在 Ubuntu 16.04 中,默认情况下,只有最新版本的 MySQL 包含在 APT 软件包存储库中.在撰写本文时,那是 MySQL 5.7 要安装它,只需更新服务器上 ...

  3. javascript 正则表达式 详细入门教程

    1.什么是正则表达式 定义: 一个用来搜索.匹配.处理一些符合特定语法规则的一个强大的字符串处理工具. 用途: 进行特定字符和字符串的搜索 替换字符串中指定的字符或字符串 验证字符串是否符合需求 2. ...

  4. Label标签 自动触发onclick,点击内部的Input

    最近项目遇到了一个bug,点击外层元素会直接触发元素内部的input框.(外层元素用的是label包裹的).找了很久才发现是label标签造成的. label定义和用法: label 标签为 inpu ...

  5. jquery特效(7)—弹出遮罩层且内容居中

    上周写了几个小特效,其中有个点击按钮弹出遮罩层的特效,下面来看最终实现的效果: 由于是测试的程序,所以我未加关闭的按钮. 一.主体程序 <!DOCTYPE html> <html&g ...

  6. hdu1052 田忌赛马 —— 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...

  7. World Finals 2017 (水题题解)

    看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...

  8. 【转】澄清P问题、NP问题、NPC问题

    首先,原文链接.(这篇文章让我第一次有了感谢腾讯,感谢微信,感谢微信公众号的冲动.总之,非常感谢作者的分享.) 然后:结论图如下 担心万一哪天原网站把这篇文章下线,所以原文内容复制过来. 澄清P问题. ...

  9. CodeForces669E:Little Artem and Time Machine(CDQ分治)(或者用map+树状数组优美地解决)

    Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of cour ...

  10. ietester

    ietest 最好安装在默认的C 装在其他的地方会报错