mybatis 映射结果集

映射结果集

1、全局setting设置
autoMappingBehavior默认是PARTIAL ,开启自动映射 的功能。唯一的要求是列名和javaBean属性名一致
如果autoMappingBehavior设置为null则会取消自动映射
数据库字段命名规范,POJO属性符合驼峰命名法,如 A_COLUMN aColumn,我们可以 开启自动驼峰命名规 则映射功能mapUnderscoreToCamelCase=true。
2、resultType用来自动映射javabean对象,往往对象中 只有基本数据类型 如果有引用数据类型通常使用 自定义resultMap
3、自定义resultMap(重难点),实现高级结果集映射,往往在javabean对象中有引用类型。
 

Select元素来定义查询操作

Id:唯一标识符。
– 用来引用这条语句,需要和接口的方法名一致

parameterType:参数类型。
– 可以不传,MyBatis会根据TypeHandler自动推断

resultType:返回值类型。
别名或者全类名,如果返回的是集合,定义集合中元素的类型。不能和resultMap同时使用。
– 常使用自动映射
 
resultMap:也是返回值类型与resultType相似
– 外部自定义的返回值

– 手动实现自定义结果集映射
 

以resultType举例

javabean:

public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
}

数据库:

Dao层接口:

public interface EmployeeMapperSelect {
	//测试:resultType
	public Employee getEmployeeById(Integer id);	
	public List<Employee> getEmpsByLastNameLike(String lastName);	
	public Map<String, Object> getEmpByIdReturnMap(Integer id);		
	//mybatis封装这个map的时候使用哪个属性作为主键
	@MapKey("id")
	public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);
}

案例:
1)简单查询

<select id="getEmployeeById" resultType="com.bean.Employee">
 	 select * from tbl_employee where id=#{id}
 </select>
//简单查询
@Test
public void test01() throws IOException{
	SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
	SqlSession openSession=sqlSessionFactory.openSession();
	EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
	Employee employee=mapper.getEmployeeById(16);
	System.out.println(employee);	
}

2)模糊查询返回List<Employee>

<select id="getEmpsByLastNameLike"	resultType="com.bean.Employee">
 	select * from tbl_employee where last_name like #{lastName}
</select>
@Test
public void test02() throws IOException{
	SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
	SqlSession openSession=sqlSessionFactory.openSession();
	EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
	List<Employee> employee=mapper.getEmpsByLastNameLike("%a%");
	System.out.println(employee);	
}

3)Map返回单条记录->key是数据库列名,value是对应的值

<select id="getEmpByIdReturnMap"	resultType="map">
 	select * from tbl_employee where id=#{id}
</select>
@Test
public void test03() throws IOException{
	SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
	SqlSession openSession=sqlSessionFactory.openSession();
	EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
	Map<String, Object> map=mapper.getEmpByIdReturnMap(14);
	System.out.println(map);	
}

4)Map返回多条记录->key是该条记录的键(自定义),value是封装后的javabean

如何指定key?在dao层接口上添加注解-->告诉mybatis封装这个map的时候使用哪个属性作为键。
例如:@MapKey("id") 用封装employee后中的id属性作为键。

@MapKey("id")
public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);
<select id="getEmpByLastNameLikeReturnMap"	resultType="com.bean.Employee">
    select * from tbl_employee where last_name like #{lastName}
</select>  
@Test
public void test04() throws IOException{
	SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
	SqlSession openSession=sqlSessionFactory.openSession();
	EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
	Map<Integer, Employee> map=mapper.getEmpByLastNameLikeReturnMap("%a%");
	System.out.println(map);		
}

以resultMap举例(重难点):

加大难度后的需求1:联表查询部门信息

新的数据库字段:

新的javabean字段:

public class Employee2 {
	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;
    .......
}

Dao层接口():

//测试:resultMap
public Employee2 getEmpAndDept(Integer id);
public Employee2 getEmpAndDeptAssociation(Integer id);
public Employee2 getEmpByIdStep(Integer id);

手动设置映射关系-->指定数据库字段名与javabean字段名 封装规则: 

<id />为主键属性
<result />为其余属性

1)嵌套结果集(级联属性封装结果集,例如dept.id):

<!-- id  last_name gender did dept_name -->
<resultMap type="com.bean.Employee2" id="MyDifEmp">
	<id column="id" property="id"/>
	<result column="last_name" property="lastName"/>
	<result column="email" property="email"/>
	<result column="gender" property="gender"/>
	<result column="did" property="dept.id"/>	
	<result column="dept_name" property="dept.departmentName"/>
</resultMap>

<select id="getEmpAndDept" resultMap="MyDifEmp">
 	select e.id id,e.last_name last_name,e.email email,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>

id=17,sql语句执行结果: 

@Test
public void test05() throws IOException{
	SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
	SqlSession openSession=sqlSessionFactory.openSession();
	EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
	Employee2 employee2=mapper.getEmpAndDept(17);
	System.out.println(employee2);	
	System.out.println(employee2.getDept());	
}

2) 嵌套结果集(association定义单个对象封装规则)

association可以指定联合的javabean对象:
property:指定哪个属性是联合对象
javaType:指定这个属性对象的类型

<resultMap type="com.bean.Employee2" id="MyDifEmp2">
	<id column="id" property="id"/>
	<result column="last_name" property="lastName"/>
	<result column="email" property="email"/>
	<result column="gender" property="gender"/>
		
	<association property="dept" javaType="com.bean.Department">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
	</association>
</resultMap>
<select id="getEmpAndDeptAssociation" resultMap="MyDifEmp2">
 	 select e.id id,e.last_name last_name,e.email email,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>
<select id="getDeptById" resultType="com.bean.Department">
	select id,dept_name departmentName from tbl_dept where id=#{id}
</select>

3)分步查询(association定义单个对象封装规则)

思路:
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、将部门设置到员工当中

新添加的Dao层:

package com.dao;

import com.bean.Department;

public interface DepartmentMapper {
	
	public Department getDeptById(Integer id);//此案例的方法
	
	public Department getDeptByIdPlus(Integer id);

	public Department getDeptByIdStep(Integer id);
}

新添加的DepartmentMapperMapper.xml:

<select id="getDeptById" resultType="com.bean.Department">
	select id,dept_name departmentName from tbl_dept where id=#{id}
</select>

执行流程:调用用getEmpByIdStemp方法,返回员工信息,再使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性。
分步的核心: property(属性值)调用select(查询方法)利用column(传递参数) 

 	 <resultMap type="com.bean.Employee2" id="MyEmpByStep">
		<id column="id" property="id"/>
		<result column="last_name" property="lastName"/>
		<result column="email" property="email"/>
		<result column="gender" property="gender"/>
		<!-- property(属性值)调用select(查询方法)利用column(传递参数) -->
		<!-- 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 -->
		 <association property="dept" 
		 	select="com.dao.DepartmentMapper.getDeptById"
		 	column="d_id">
		 </association>
	 </resultMap>

    <select id="getEmpByIdStep" resultMap="MyEmpByStep">
		select * from tbl_employee where id=#{id}
    </select>
	//分步查询association
	@Test
	public void test06() throws IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
		Employee2 employee2=mapper.getEmpByIdStep(17);
		System.out.println(employee2);	
		System.out.println(employee2.getDept());	
	}

可以分析得出:发送了两条sql语句,只要执行就是两条,有时候可能不需要部门信息,这样容易带来不必要的资源消耗,下面将介绍延迟加载技术

4)分步查询的延迟加载
实质就是-按需加载,减少资源消耗。

在案例3)中稍作修改:
1、添加两个全局配置

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

2、测试类少写一行:

	//分步查询(延迟加载)
	@Test
	public void test07() throws IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		EmployeeMapperSelect mapper=openSession.getMapper(EmployeeMapperSelect.class);
		Employee2 employee2=mapper.getEmpByIdStep(17);
		System.out.println(employee2.getEmail());	
		//System.out.println(employee2.getDept());	
	}

结果分析:只发送了一条sql语句!因为我们现在不需要Department相关信息,因此分步查询第二条语句不会发送。

再次加大难度需求2:查询部门对应的所有员工信息也查询出来

增加后的Dao层(DepartmentMapper):

public interface DepartmentMapper {
	//association 演示分步查询
	public Department getDeptById(Integer id);
	//嵌套查询
	public Department getDeptByIdPlus(Integer id);
    //分步查询
	public Department getDeptByIdStep(Integer id);
}

修改后的javabean:

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

5)嵌套的方式(collection定义关联的集合类型的属性封装规则)

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

	<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
	<resultMap type="com.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!-- 
			collection定义关联集合类型的属性的封装规则 
			ofType:指定集合里面元素的类型
		-->
		<collection property="emps" ofType="com.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>
	<!-- 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)
	@Test
	public void test08() throws IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class);
		Department department=mapper.getDeptByIdPlus(1);
		System.out.println(department);	
		System.out.println(department.getEmps());	
	}
	//分步查询返回带有的集合(collection)
	@Test
	public void test09() throws IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class);
		Department department=mapper.getDeptByIdStep(1);
		System.out.println(department);	
		System.out.println(department.getEmps());	
	}


 6)分步查询(collection)

EmployeeMapperSelect.java多添加一行(Dao层)

public Employee2 getEmpsByDeptId(Integer id);//测试collection分步查询
 	 <!-- collection分步查询 -->
	<select id="getEmpsByDeptId" resultType="com.bean.Employee2">
			select * from tbl_employee where d_id=#{deptId};
 	 </select>
	<!-- collection:分段查询 -->
	<resultMap type="com.bean.Department" id="MyDeptStep">
		<id column="id" property="id"/>
		<id column="dept_name" property="departmentName"/>
		<collection property="emps" 
			select="com.dao.EmployeeMapperSelect.getEmpsByDeptId"
			column="{deptId=id}" >
		</collection>
	</resultMap>
	<!-- public Department getDeptByIdStep(Integer id); -->
	<select id="getDeptByIdStep" resultMap="MyDeptStep">
		select id,dept_name from tbl_dept where id=#{id}
	</select>
	//分步查询返回带有的集合(collection)
	@Test
	public void test09() throws IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class);
		Department department=mapper.getDeptByIdStep(1);
		System.out.println(department);	
		System.out.println(department.getEmps());	
	}

 

扩展:
1)分步查询中传递多个值得时候

<collection property=" " select=" " column="{key1=column1,key2=column2}" >

2)分步查询中局部开启延迟加载

fetchType="lazy" :表示延迟加载
fetchType="lazy":表示立即查询

<collection property=" " select=" " column="{key1=column1,key2=column2}" fetchType="lazy">

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值