mybatis之巢狀查詢association和collection
阿新 • • 發佈:2018-11-26
Mybatis association是用於一對一和多對一,而collection是用於一對多的關係
一. Association
1. 巢狀查詢實現association一對一
public class Card implements Serializable{ private Integer id; private String code; //省略set和get方法. } public class Card implements Serializable{ private Integer id; private String code; //省略set和get方法. } package com.glj.mapper; import com.glj.poji.Card; public interface CardMapper { Card selectCardById(Integer id); } package com.glj.mapper; import com.glj.poji.Person; public interface PersonMapper { Person selectPersonById(Integer id); }
<?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.glj.mapper.CardMapper"> <select id="selectCardById" parameterType="int" resultType="com.glj.poji.Card"> select * from tb_card where id = #{id} </select> </mapper> <?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.glj.mapper.PersonMapper"> <resultMap type="com.glj.poji.Person" id="personMapper"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <association property="card" column="card_id" select="com.glj.mapper.CardMapper.selectCardById" javaType="com.glj.poji.Card"> </association> </resultMap> <select id="selectPersonById" parameterType="int" resultMap="personMapper"> select * from tb_person where id = #{id} </select> </mapper>
2.連線查詢實現多對一
package com.glj.pojo; import java.io.Serializable; import java.util.List; public class Clazz implements Serializable{ private Integer id; private String code; private String name; //班級與學生是一對多的關係 private List<Student> students; //省略set/get方法 } package com.glj.pojo; import java.io.Serializable; import java.util.List; public class Clazz implements Serializable{ private Integer id; private String code; private String name; //班級與學生是一對多的關係 private List<Student> students; //省略set/get方法 } package com.glj.mapper; import com.glj.pojo.Student; public interface StudentMapper { Student selectStudentById(Integer id); }
<?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.glj.mapper.StudentMapper">
<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
select s.id,s.name,s.sex,s.age,c.id cid,c.code,c.name
from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
</select>
<select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
select * from tb_student where clazz_id = #{id}
</select>
<resultMap type="com.glj.pojo.Student" id="studentResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<association property="clazz" javaType="com.glj.pojo.Clazz">
<id property="id" column="cid"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
</association>
</resultMap>
</mapper>
二. Collection
1.巢狀查詢實現一對多
接上邊的學生班級例項
//ClazzMapper介面
package com.glj.mapper;
import com.glj.pojo.Clazz;
public interface ClazzMapper {
Clazz selectClazzById(Integer id);
}
<?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.glj.mapper.ClazzMapper">
<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
select * from tb_clazz where id = #{id}
</select>
<resultMap type="com.glj.pojo.Clazz" id="clazzResultMap">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
<!-- property: 指的是集合屬性的值, ofType:指的是集合中元素的型別 -->
<collection property="students" ofType="com.glj.pojo.Student"
column="id" javaType="ArrayList"
fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
</collection>
</resultMap>
</mapper>
2.連線查詢實現一對多
<!-- 獲取指定使用者的地址列表(user表-address表:1對多關係) collection start-->
<resultMap type="User" id="userMap">
<id property="id" column="userId"/>
//property是屬性名 ofType是List《Address》的類物件
<collection property="addressList" ofType="Address">
<id property="id" column="a_id"/>
<result property="postCode" column="postCode"/>
<result property="addressContent" column="addressContent"/>
</collection>
</resultMap>
<select id="getAddressListByUserId" parameterType="User" resultMap="userMap">
select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id}
</select>
<!-- collection end -->
總結:
association和collection均可以使用連線查詢和巢狀查詢的方法實現,連線查詢可以減少select查詢次數,巢狀查詢可以使程式結構看起來更清晰,而且子查詢可以複用。
需要特別注意的是分頁查詢在用連線查詢實現collection時,應該先查詢分頁條目,再進行連線查詢獲取關聯子條目,否則分頁會基於子條目進行,查詢結果會出現資料條目不匹配的情況。
參考文章:
https://www.cnblogs.com/xuerong/p/5000233.html
https://blog.csdn.net/baidu_38116275/article/details/78622669