返回值是List集合的情况

//方法
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>

//模糊查询,遍历输出
List<Employee> like = mapper.getEmpsByLastNameLike("%e%");
for (Employee employee : like) {
      System.out.println(employee);
}

返回值是Map的情况

//返回一条记录的map;key就是列名,值就是对应的值
public Map<String, Object> getEmpByIdReturnMap(Integer id);

//映射文件
<select id="getEmpByIdReturnMap" resultType="map">
      select * from tbl_employee where id=#{id}
</select>

//Test
Map<String, Object> map = mapper.getEmpByIdReturnMap(1);

返回值是多个Map的情况

//多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);

//映射文件
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
      select * from tbl_employee where last_name like #{lastName}
</select>

//Test
Map<String, Employee> map = mapper.getEmpByLastNameLikeReturnMap("%r%");

resultMap自定义结果集映射

<mapper namespace="接口的全类名">
如果使用自定义结果集映射的情况下就不需要驼峰命名规则。

##自定义结果集
<!--自定义某个javaBean的封装规则 type:自定义规则的Java类型 id:唯一id方便引用-->
<resultMap type="com.leavescai.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>

//方法
public Employee getEmpById(Integer id);

//映射文件	
<!-- resultMap:自定义结果集映射规则 -->
<select id="getEmpById"  resultMap="MySimpleEmp">
      select * from tbl_employee where id=#{id}
</select>

resultMap自定义结果集映射--多表查询

需求:查询Employee的同时查询员工对应的部门 Employee==Department

//第一种解决方案:
<!-- 联合查询:级联属性封装结果集 -->
<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>

//第二种解决方案:
<!-- 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>

//第三种解决方案:(强大,推荐)
<!-- 使用association进行分步查询:
     1、先按照员工id查询员工信息
     2、根据查询员工信息中的d_id值去部门表查出部门信息
     3、部门设置到员工中;-->
<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>
<!--  public Employee getEmpByIdStep(Integer id);-->
<select id="getEmpByIdStep" resultMap="MyEmpByStep">
      select * from tbl_employee where id=#{id}
      <if test="_parameter!=null">
          and 1=1
      </if>
</select>
association 还可以使用延迟加载(懒加载)(按需加载)
在MyConfig.xml的setting中配置:

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

resultMap自定义结果集映射--collection标签

<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
	<resultMap type="com.leavescai.mybatis.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!-- 
			collection定义关联集合类型的属性的封装规则 
			ofType:指定集合里面元素的类型
		-->
		<collection property="emps" ofType="com.leavescai.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>
	<!-- 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:分段查询 -->
	<resultMap type="com.leavescai.mybatis.bean.Department" id="MyDeptStep">
		<id column="id" property="id"/>
		<id column="dept_name" property="departmentName"/>
		<collection property="emps" 
			select="com.leavescai.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
			column="{deptId=id}" fetchType="lazy"></collection>
	</resultMap>
	<!-- public Department getDeptByIdStep(Integer id); -->
	<select id="getDeptByIdStep" resultMap="MyDeptStep">
		select id,dept_name from tbl_dept where id=#{id}
	</select>

      <!-- 扩展:多列的值传递过去:将多列的值封装map传递;
			column="{key1=column1,key2=column2}"
		fetchType="lazy":表示使用延迟加载;
				- lazy:延迟
				- eager:立即
	 -->
posted on 2020-11-27 09:25  LeavesCai7  阅读(110)  评论(0)    收藏  举报