15、mybatis一对多关联查询 collection定义关联集合封装规则及懒加载
生活随笔
收集整理的這篇文章主要介紹了
15、mybatis一对多关联查询 collection定义关联集合封装规则及懒加载
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1、collection定義關(guān)聯(lián)集合封裝規(guī)則單步查詢
- 1)、Dept增加集合屬性
- 2)、DeptMapper增加查詢接口
- 3)、DeptMapper.xml增加collection配置
- 4)、Test
- 5)、Result
- 2、collection定義關(guān)聯(lián)集合封裝規(guī)則分步查詢
- 6)、EmployeeMapper
- 7)、EmployeeMapper.xml
- 8)、DeptMapper.xml
- 9)、Test
1、collection定義關(guān)聯(lián)集合封裝規(guī)則單步查詢
1)、Dept增加集合屬性
package com.mi.pojo;import java.util.List;public class Dept {private int id;private String deptName;private List<Employee> emps;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}@Overridepublic String toString() {return "Dept{" +"id=" + id +", deptName='" + deptName + '\'' +", emps=" + emps +'}';} }2)、DeptMapper增加查詢接口
package com.mi.dao;import com.mi.pojo.Dept;public interface DeptMapper {public Dept getDeptById(Integer id);public Dept getDeptByIdPlus(Integer id); }3)、DeptMapper.xml增加collection配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--指定為接口的全類名--> <mapper namespace="com.mi.dao.DeptMapper"><select id="getDeptById" resultType="com.mi.pojo.Dept">select id,dept_name deptName from dept where id = #{id}</select><!--collection嵌套結(jié)果集的方式,定義關(guān)聯(lián)的集合類型元素的封裝規(guī)則--><resultMap id="myDept" type="com.mi.pojo.Dept"><id column="id" property="id"/><result column="dept_name" property="deptName"/><!--collection定義關(guān)聯(lián)集合類型的屬性的封裝規(guī)則ofType:指定集合里面元素的類型--><collection property="emps" ofType="com.mi.pojo.Employee"><id column="e_id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/></collection></resultMap><!--查詢部門的時候?qū)⒉块T對應(yīng)的所有員工信息也查詢出來--><select id="getDeptByIdPlus" resultMap="myDept">select d.id,d.dept_name,e.id e_id,e.last_name,e.genderfrom dept d left join employee eon d.id = e.d_idwhere d.id = #{id}</select> </mapper>4)、Test
@Testpublic void getDeptByIdPlus() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept dept = mapper.getDeptByIdPlus(1);System.out.println(dept); // System.out.println(employee.getDept());}finally {sqlSession.close();}}5)、Result
DEBUG 09-13 18:16:31,851 ==> Preparing: select d.id,d.dept_name,e.id e_id,e.last_name,e.gender from dept d left join employee e on d.id = e.d_id where d.id = ? (BaseJdbcLogger.java:143) DEBUG 09-13 18:16:31,931 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:143) DEBUG 09-13 18:16:31,964 <== Total: 2 (BaseJdbcLogger.java:143) Dept{id=1, deptName='開發(fā)部', emps=[Employee{id=3, lastName='jerry', gender='0'}, Employee{id=5, lastName='kite', gender='1'}]}Process finished with exit code 0文章目錄
- 1、collection定義關(guān)聯(lián)集合封裝規(guī)則單步查詢
- 1)、Dept增加集合屬性
- 2)、DeptMapper增加查詢接口
- 3)、DeptMapper.xml增加collection配置
- 4)、Test
- 5)、Result
- 2、collection定義關(guān)聯(lián)集合封裝規(guī)則分步查詢
- 6)、EmployeeMapper
- 7)、EmployeeMapper.xml
- 8)、DeptMapper.xml
- 9)、Test
2、collection定義關(guān)聯(lián)集合封裝規(guī)則分步查詢
6)、EmployeeMapper
package com.mi.dao;import com.mi.pojo.Employee; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param;import java.util.List; import java.util.Map;public interface EmployeeMapper {public Employee getEmpByIdStep(Integer id);public List<Employee> getEmpsByDeptId(Integer deptId); }7)、EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--指定為接口的全類名--> <mapper namespace="com.mi.dao.EmployeeMapper"><select id="getEmpsByDeptId" resultType="com.mi.pojo.Employee">select * from employee where d_id = #{deptId}</select> </mapper>8)、DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--指定為接口的全類名--> <mapper namespace="com.mi.dao.DeptMapper"><resultMap id="myDeptStep" type="com.mi.pojo.Dept"><id column="id" property="id"/><result column="dept_name" property="deptName"/><collection property="emps" select="com.mi.dao.EmployeeMapper.getEmpsByDeptId" column="id" fetchType="lazy"><!--lazy 表示延遲加載--><!--如果是多個參數(shù)可以寫為column={key1=column1,key2=column2}例如column = {deptId=id}--></collection></resultMap><select id="getDeptByIdStep" resultMap="myDeptStep">select id,dept_name from dept where id = #{id}</select> </mapper>9)、Test
@Testpublic void getDeptByIdStep() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept dept = mapper.getDeptByIdStep(1);System.out.println(dept); // System.out.println(employee.getDept());}finally {sqlSession.close();}}文章目錄
- 1、collection定義關(guān)聯(lián)集合封裝規(guī)則單步查詢
- 1)、Dept增加集合屬性
- 2)、DeptMapper增加查詢接口
- 3)、DeptMapper.xml增加collection配置
- 4)、Test
- 5)、Result
- 2、collection定義關(guān)聯(lián)集合封裝規(guī)則分步查詢
- 6)、EmployeeMapper
- 7)、EmployeeMapper.xml
- 8)、DeptMapper.xml
- 9)、Test
總結(jié)
以上是生活随笔為你收集整理的15、mybatis一对多关联查询 collection定义关联集合封装规则及懒加载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14、mybatis多表关联查询 ass
- 下一篇: 16、mybatis动态sql 批量插入