Mybatis中《insert》返回map、list及嵌套、association和collection分布查询及延迟加载(懒加载)(按需加载)

本文目录类容目录结构:

一、select返回值

  • 1、返回值记录封装为List
  • 2、返回值记录封装为map

二、select_resultMap

  • 1、自定义结果映射规则
  • 2、关联查询_级联属性封装结果,即<result column="did" property="dept.id"/>的方式
  • 3、association嵌套结果集定义关联对象封装规则
  • 4、association分步查询
  • 5、分步查询&延迟加载(懒加载)(按需加载)
  • 6、collection定义关联集合封装规则
  • 7、collection分步查询&延迟加载
  • 8、分步查询传递多列值&fetchType
  • 9、discriminator鉴别器(不常用)

1、数据库表:

员工表:

部门表:

2、新建实体

为了节省空间省略了set,get方法和构造函数

员工表:

@Alias("emp")
public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private Department dept;
}

部门表: 

public class Department {
	
	private Integer id;
	private String departmentName;
	private List<Employee> emps;
}

一、select返回值

1、返回值记录封装为List

public List<Employee> getEmpsByLastNameLike(String lastName);

resultType:如果返回的是一个集合,要写集合中元素的类型

<!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
<!--resultType:如果返回的是一个集合,要写集合中元素的类型  -->
<select id="getEmpsByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
	select * from tbl_employee where last_name like #{lastName}
</select>

2、返回值记录封装为map

2.1 多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
     @MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key

@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);  -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
	select * from tbl_employee where last_name like #{lastName}
</select>

2.2  返回一条记录的map;key就是列名,值就是对应的值

public Map<String, Object> getEmpByIdReturnMap(Integer id);
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id);  -->
<select id="getEmpByIdReturnMap" resultType="map">
	select * from tbl_employee where id=#{id}
</select>

二、resultMap

<resultMap>的作用:对select查询的结果按指定的规则进行封装

1、自定义结果映射规则

public Employee getEmpById(Integer id);
<!-- resultMap:自定义结果集映射规则;  -->
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById"  resultMap="MySimpleEmp">
	select * from tbl_employee where id=#{id}
</select>

<resultMap>:自定义某个javaBean的封装规则,type:自定义规则的Java类型,id:唯一id方便引用

<id>和<result>指定主键列的封装规则,id定义主键会底层有优化; column:指定哪一列; property:指定对应的javaBean属性

<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
	<!--指定主键列的封装规则
	id定义主键会底层有优化;
	column:指定哪一列
	property:指定对应的javaBean属性
	  -->
	<id column="id" property="id"/>
	<!-- 定义普通列封装规则 -->
	<result column="last_name" property="lastName"/>
	<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
	<result column="email" property="email"/>
	<result column="gender" property="gender"/>
</resultMap>

2、关联查询_级联属性封装结果,即<result column="did" property="dept.id"/>的方式

场景:查询Employee的同时查询员工对应的部门,即查询Employee时里面的Department也有数据;见实体Employee的结构

public Employee getEmpAndDept(Integer id);
<select id="getEmpAndDept" resultMap="MyDifEmp">
	SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
	d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
	WHERE e.d_id=d.id AND e.id=#{id}
</select>
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
	<id column="id" property="id"/>
	<result column="last_name" property="lastName"/>
	<result column="gender" property="gender"/>
	<result column="did" property="dept.id"/>
	<result column="dept_name" property="dept.departmentName"/>
</resultMap>

3、association嵌套结果集定义关联对象封装规则

场景:查询Employee的同时查询员工对应的部门,即查询Employee时里面的Department也有数据;见实体Employee的结构

<association>:定义关联的单个对象的封装规则,可以指定联合的javaBean对象;

                        property="dept":指定哪个属性是联合的对象;javaType:指定这个属性对象的类型[不能省略]

public Employee getEmpAndDept(Integer id);
<select id="getEmpAndDept" resultMap="MyDifEmp">
	SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
	d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
	WHERE e.d_id=d.id AND e.id=#{id}
</select>
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
	<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.atguigu.mybatis.bean.Department">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
	</association>
</resultMap>

4、association分步查询

public Employee getEmpByIdStep(Integer id);
 <select id="getEmpByIdStep" resultMap="MyEmpByStep">
 	select * from tbl_employee where id=#{id}
 </select>

<association>:定义关联对象的封装规则
                      select:表明当前属性是调用select指定的方法查出的结果
                      column:指定将哪一列的值传给这个方法

 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 

 <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
 	<id column="id" property="id"/>
 	<result column="last_name" property="lastName"/>
 	<result column="email" property="email"/>
 	<result column="gender" property="gender"/>
 	<!-- association定义关联对象的封装规则
 		select:表明当前属性是调用select指定的方法查出的结果
 		column:指定将哪一列的值传给这个方法
 		
 		流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
 	 -->
	<association property="dept" 
 		select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
 		column="d_id">
	</association>
 </resultMap>

此时,需要使用部门mapper中的方法:

public Department getDeptById(Integer id);
<!--public Department getDeptById(Integer id);  -->
<select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
	select id,dept_name departmentName from tbl_dept where id=#{id}
</select>

5、分步查询&延迟加载(懒加载)(按需加载)
原来我们每次查询Employee对象的时候,都将一起查询出来;现在部门信息在我们使用的时候再去查询;
做法:在分段查询的基础之上,全局配置文件中,加上两个配置:

<settings>
	<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
	<setting name="lazyLoadingEnabled" value="true"/>
	<setting name="aggressiveLazyLoading" value="false"/>
</settings>

说明:执行第一个打印语句后,第二句发现还用到了部门,才去查部门的信息并执行了sql语句; 

6、collection定义关联集合封装规则

场景:查出的部门中对应一个list的员工集合:

public Department getDeptByIdPlus(Integer id);
<!-- public Department getDeptByIdPlus(Integer id); -->
<select id="getDeptByIdPlus" resultMap="MyDept">
	SELECT d.id did,d.dept_name dept_name,
			e.id eid,e.last_name last_name,e.email email,e.gender gender
	FROM tbl_dept d
	LEFT JOIN tbl_employee e
	ON d.id=e.d_id
	WHERE d.id=#{id}
</select>

嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则

collection:定义关联集合类型的属性的封装规则 
ofType:指定集合里面元素的类型

<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
	<id column="did" property="id"/>
	<result column="dept_name" property="departmentName"/>
	<!-- 
		collection定义关联集合类型的属性的封装规则 
		ofType:指定集合里面元素的类型
	-->
	<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
		<!-- 定义这个集合中元素的封装规则 -->
		<id column="eid" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
	</collection>
</resultMap>

7、collection分步查询&延迟加载

场景:查出的部门中对应一个list的员工集合:

public Department getDeptByIdStep(Integer id);
<!-- public Department getDeptByIdStep(Integer id); -->
<select id="getDeptByIdStep" resultMap="MyDeptStep">
	select id,dept_name from tbl_dept where id=#{id}
</select>
<!-- collection:分段查询 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
	<id column="id" property="id"/>
	<id column="dept_name" property="departmentName"/>
	<collection property="emps" 
		select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
		column="id"></collection>
</resultMap>

注意,这里面会引用员工部门的语句,则 在员工的mapper中写上对应的查询方法:

public List<Employee> getEmpsByDeptId(Integer deptId);
<!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
<select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
	select * from tbl_employee where d_id=#{deptId}
</select>

全局配置了延迟加载后,此处也会起作用,即用到的时候才会去查询数据库:

8、collection分步查询传递多列值(多关联条件)&fetchType

基于7的场景,collection分步查询时,当包含集合对象有多个关联条件,此时collection中的传参方法如下

<collection property="emps" 
	select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
	column="{dId=id,key2=column2}">
</collection>

即使配置了全局加载,此处也可以用 fetchType修改

fetchType="lazy":表示使用延迟加载;- eager:表示使用立即加载

<collection property="emps" 
	select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
	column="{dId=id,key2=column2}" fetchType="lazy">
</collection>

9、discriminator鉴别器(不常用)

<discriminator javaType=""></discriminator>
鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
场景:封装Employee:
          如果查出的是女生:就把部门信息查询出来,否则不查询;
          如果是男生,把last_name这一列的值赋值给email;

对部门信息进行分布查询:

public Employee getEmpByIdStep(Integer id);
 <select id="getEmpByIdStep" resultMap="MyEmpDis">
 	select * from tbl_employee where id=#{id}
 	<if test="_parameter!=null">
 		and 1=1
 	</if>
 </select>
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpDis">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--
	column:指定判定的列名
	javaType:列值对应的java类型  -->
<discriminator javaType="string" column="gender">
	<!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
	<case value="0" resultType="com.atguigu.mybatis.bean.Employee">
		<association property="dept" 
	 		select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
	 		column="d_id">
 		</association>
	</case>
	<!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
	<case value="1" resultType="com.atguigu.mybatis.bean.Employee">
 		<id column="id" property="id"/>
	 	<result column="last_name" property="lastName"/>
	 	<result column="last_name" property="email"/>
	 	<result column="gender" property="gender"/>
	</case>
</discriminator>
</resultMap>

测试:

结果:女生,则查出了部门

 结果:男生,赋了值并没有查出部门

猜你喜欢

转载自blog.csdn.net/jinhaijing/article/details/84173959