<select id="getCarsWithCollection" resultMap="superCarResult"> select c1.carid,c1.cartype,c2.enginetype,c2.enginecylinders from cars c1,cars c2 where c1.carid=c2.carid </select>
<resultMap type="tk.mybatis.springboot.model.Brakes" id="brakesResult"> <result column="brakesType" property="type"/> </resultMap>
<resultMap type="tk.mybatis.springboot.model.SuperCar" id="superCarResult"> <id column="carid" property="id"/> <result column="cartype" property="type"/> <association property="brakes" resultMap="brakesResult"/> <collection property="engines" javaType="ArrayList" ofType="tk.mybatis.springboot.model.Engine"> <result column="enginetype" property="type"/> <result column="enginecylinders" property="cylinders"/> </collection> </resultMap>
聚集元素用來處理“一對多”的關系。需要指定映射的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中對象的類型ofType(Java實體類);對應的數據庫表的列名稱;
不同情況需要告訴MyBatis 如何加載一個聚集。MyBatis 可以用兩種方式加載:
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子元素中去。