MyBatis嵌套Collection

站在巨人的肩膀上

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.",

转载于:https://www.cnblogs.com/longxok/p/10905975.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MyBatisCollection多层嵌套可以通过使用<collection>标签来实现。每一层都可以使用<collection>标签,并且可以通过select属性指定查询语句。例如: ``` <resultMap id="nestedResultMap" type="Parent"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="children" ofType="Child"> <id property="id" column="child_id"/> <result property="name" column="child_name"/> <collection property="grandchildren" ofType="Grandchild"> <id property="id" column="grandchild_id"/> <result property="name" column="grandchild_name"/> </collection> </collection> </resultMap> <select id="selectNested" resultMap="nestedResultMap"> SELECT * FROM parent LEFT JOIN child ON parent.id = child.parent_id LEFT JOIN grandchild ON child.id = grandchild.child_id </select> ``` 希望这个回答对您有帮助! ### 回答2: Mybatis是一个Java持久层框架,它支持ORM方式,通过XML或注解的配置方式,将关系数据库中的数据映射到Java对象中,同时提供了灵活的SQL编写方式,使得开发者可以灵活地进行自定义SQL操作。 在Mybatis中,Collection多层嵌套是指在一个实体类中,如果有某个属性对应的数据库列是一个集合或数组类型,则可以通过MybatisCollection标签来实现该属性的映射。同样,如果这个集合或数组中的元素也是一个实体对象,则可以通过在Collection标签中再次使用Mybatis的Nested-select标签来实现多层嵌套的映射。 例如,一个学生有多个选课记录,每个选课记录又包含一个课程和教师信息。如果我们要把这些信息映射到Java对象中,则可以定义一个Student实体对象,其中包含一个名为courses的集合属性,courses中的元素是一个Course对象,Course对象又包含一个Teacher对象。 在映射配置文件中,我们可以使用Collection标签来映射这个courses属性,同时可以在Collection标签中再次使用Nested-select标签来映射Course对象中的teacher属性。具体的配置方式如下: ``` <resultMap id="studentMap" type="Student"> <result property="id" column="id" /> <result property="name" column="name" /> <collection property="courses" ofType="Course"> <result property="id" column="course_id" /> <result property="name" column="course_name" /> <nestedSelect select="selectTeacherByCourseId" resultMap="teacherMap" /> </collection> </resultMap> <resultMap id="teacherMap" type="Teacher"> <result property="id" column="teacher_id" /> <result property="name" column="teacher_name" /> </resultMap> <select id="selectStudentById" resultMap="studentMap"> select * from student where id = #{id} </select> <select id="selectTeacherByCourseId" resultMap="teacherMap"> select * from teacher where id in ( select teacher_id from course_teacher where course_id = #{id} ) </select> ``` 通过这种方式,我们就可以轻松地实现多层嵌套的属性映射,使得实体类中的属性可以与数据库中的多个表关联,提高了数据查询和操作的灵活性。 ### 回答3: MyBatis是一个流行的Java持久化框架,在操作数据库时提供了方便和灵活性。当我们需要操作具有多层嵌套结构的数据时,MyBatiscollection标签就派上了用场。 collection标签有以下属性: - property:指定属性名称; - ofType:指定集合元素的类型; - select:指定关联查询的SQL语句; - resultMap:指定结果映射的ID; - javaType:指定Java类型; - column:指定表中的列名。 由于MyBatiscollection标签有那么多属性,所以有时可能会感到混乱。在多层嵌套结构中,我们需要按照我们的需求设置标签中的属性。 假设我们有一个宠物店应用程序,其中包含如下实体类: ```java public class Pet { private Long id; private String name; private String breed; private List<Toy> toys; // 省略getter和setter方法 } public class Toy { private Long id; private String name; // 省略getter和setter方法 } ``` 我们需要从数据库中获取所有宠物和每个宠物所拥有的玩具。在这种情况下,我们需要使用collection标签。 ```xml <!-- 查询所有的宠物和联合查询玩具 --> <select id="getAllPets" resultMap="petResultMap"> SELECT p.id, p.name, p.breed, t.id AS toy_id, t.name AS toy_name FROM pet p LEFT JOIN toy t ON t.pet_id = p.id </select> <!-- 配置resultMap --> <resultMap id="petResultMap" type="Pet"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="breed" column="breed"/> <!-- 用collection标签映射pet有many的toy集合 --> <collection property="toys" ofType="Toy" resultMap="toyResultMap"/> </resultMap> <resultMap id="toyResultMap" type="Toy"> <id property="id" column="toy_id"/> <result property="name" column="toy_name"/> </resultMap> ``` 在上面的示例中,我们定义了两个resultMap来映射查询结果。 petResultMap将查询到的数据映射到Pet实体类中,并使用collection标签映射每个Pet对象的toys属性为一个玩具列表。```ofType="Toy"```属性告诉MyBatis将toys属性映射为Toy对象的列表。Collection标签中resultMap属性设置为```toyResultMap```告诉MyBatis将查询结果映射到Toy对象上。 以上就是MyBatis collection多层嵌套的简单介绍。深入理解和熟练掌握这个标签不仅可以让我们更好地利用MyBatis操作数据,而且可以提高我们的编码效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值