14、mybatis多表关联查询 association定义关联对象封装规则及懒加载
生活随笔
收集整理的這篇文章主要介紹了
14、mybatis多表关联查询 association定义关联对象封装规则及懒加载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1、使用association單步查詢
- 1)、EmployeeMapper
- 2)、EmployeeMapper.xml
- 3)、Test
- 2、使用association進行分步查詢
- 4)、DeptMapper
- 5)、DeptMapper.xml
- 6)、mybatis-config.xml
- 7)、EmployeeMapper
- 8)、EmployeeMapper.xml
- 9)、Test
- 3、懶加載
- 10)mybatis-config.xml
1、使用association單步查詢
1)、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 getEmpAndDept(Integer id); }2)、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"><!--使用association定義單個對象的封裝--><resultMap id="myEmpAndEmp2" type="com.mi.pojo.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!--association可以指定聯合的javaBean對象property="dept" 指定哪個屬性是聯合的對象javaType:指定這個屬性對象的類型[不能省略]--><association property="dept" javaType="com.mi.pojo.Dept"><result column="d_id" property="id"/><result column="dept_name" property="deptName"/></association></resultMap><!--關聯查詢員工及部門--><select id="getEmpAndDept" resultMap="myEmpAndEmp2">select e.id,e.last_name, e.gender,d.id d_id,d.dept_name dept_namefrom employee e ,dept dwhere e.d_id = d.id and e.id = #{id}</select> </mapper>3)、Test
@Testpublic void getEmpAndDept() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現類對象//會為接口自動創建一個代理對象,代理對象去執行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Employee employee = mapper.getEmpAndDept(3);System.out.println(employee);System.out.println(employee.getDept());}finally {sqlSession.close();}}2、使用association進行分步查詢
文章目錄
- 1、使用association單步查詢
- 1)、EmployeeMapper
- 2)、EmployeeMapper.xml
- 3)、Test
- 2、使用association進行分步查詢
- 4)、DeptMapper
- 5)、DeptMapper.xml
- 6)、mybatis-config.xml
- 7)、EmployeeMapper
- 8)、EmployeeMapper.xml
- 9)、Test
- 3、懶加載
- 10)mybatis-config.xml
4)、DeptMapper
package com.mi.dao;import com.mi.pojo.Dept;public interface DeptMapper {public Dept getDeptById(Integer id); }5)、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"><select id="getDeptById" resultType="com.mi.pojo.Dept">select id,dept_name deptName from dept where id = #{id}</select></mapper>6)、mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="mapper/EmployeeMapper.xml"/><mapper resource="mapper/DeptMapper.xml"/></mappers> </configuration>7)、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); }8)、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"><!--使用association進行分步查詢1、先按照員工ID查詢員工信息2、根據查詢員工信息中的d_id值去部門表查出部門信息3、部門設置到員工中--><resultMap id="myEmpAndDeptStep" type="com.mi.pojo.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!--association定義關聯對象封裝規則select :表明當前屬性是調用select指定的方法查出的結果column : 指定將哪一列的值傳給這個方法流程:使用select 指定的方法(傳入column指定的這列參數的值)查出對象,并封裝給property--><association property="dept" select="com.mi.dao.DeptMapper.getDeptById" column="d_id"></association></resultMap><select id="getEmpByIdStep" resultMap="myEmpAndDeptStep" >select * from employee e where id = #{id}</select> </mapper>9)、Test
@Testpublic void getEmpByIdStep() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現類對象//會為接口自動創建一個代理對象,代理對象去執行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Employee employee = mapper.getEmpByIdStep(3);System.out.println(employee);System.out.println(employee.getDept());}finally {sqlSession.close();}}3、懶加載
10)mybatis-config.xml
在association進行分步查詢的基礎上,增加懶加載配置
修改部分:
<!--延遲加載的全局開關--><setting name="lazyLoadingEnabled" value="true"/><!--任何方法的調用都會加載該對象的所有屬性--><setting name="aggressiveLazyLoading" value="false"/>全部配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/><!--Oracle數據庫需要指定數據為空的時候值為null,否則默認為OTHER,Oracle不支持--><setting name="jdbcTypeForNull" value="NULL"/><!--延遲加載的全局開關--><setting name="lazyLoadingEnabled" value="true"/><!--任何方法的調用都會加載該對象的所有屬性--><setting name="aggressiveLazyLoading" value="false"/></settings><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="mapper/EmployeeMapper.xml"/><mapper resource="mapper/DeptMapper.xml"/></mappers> </configuration>文章目錄
- 1、使用association單步查詢
- 1)、EmployeeMapper
- 2)、EmployeeMapper.xml
- 3)、Test
- 2、使用association進行分步查詢
- 4)、DeptMapper
- 5)、DeptMapper.xml
- 6)、mybatis-config.xml
- 7)、EmployeeMapper
- 8)、EmployeeMapper.xml
- 9)、Test
- 3、懶加載
- 10)mybatis-config.xml
總結
以上是生活随笔為你收集整理的14、mybatis多表关联查询 association定义关联对象封装规则及懒加载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 13、mybatis多表关联查询级联属性
- 下一篇: 15、mybatis一对多关联查询 co