笔记要点

出错分析与总结

在全局配置中,映射dao包下的全部:

<mapper>
<package name="com.dao"/>
</mappers>

工程组织

1.定义接口

package com.dao;
import com.bean.*;
public interface EmployeeMapperPlus { public Employee getEmpAndDept(Integer id);//关联查询
public Employee getEmpAndDept2(Integer id); //关联查询 ,使用association
public Employee getEmpById(Integer id); }

2.定义XML映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.EmployeeMapperPlus">
<!--ResultMap ;自定义结果集映射规则;
type: 自定义规则的Java类型;id: 唯一的标识,方便引用-->
<resultMap id="MySimpleEmp" type="com.bean.Employee">
<!--指定主键列的封装规则,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> <!--场景1 ,方法1:使用级联属性的方式
查询Employee的同时查询员工对应的部门Employee.dept=Department.id;
输出员工对应的部门的全部信息:
id last_name gender did dept_name
-->
<resultMap id="MyDifEmp" type="com.bean.Employee">
<!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性-->
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
<!--public Employee getEmpAndDept2(Integer id); //association 定义封装规则!--> <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,email
FROM tbl_employee e,tbl_dept d
WHERE e.`d_id`=d.`id` AND e.id=#{id};
</select> <!--方法2:-使用association可以指定联合的javaBean的对象
-->
<resultMap id="MyDifEmp2" type="com.bean.Employee">
<!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性-->
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/> <!--使用association可以指定联合的javaBean的对象;(定义单个对象的封装规则!)
property="dept";指定那个属性是联合的对象;javaType="dept";指定那个属性对象的类型;-->
<association property="dept" javaType="com.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association> </resultMap>
<!--public Employee getEmpAndDept2(Integer id); //关联查询--> <select id="getEmpAndDept2" resultMap="MyDifEmp2">
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,email
FROM tbl_employee e,tbl_dept d
WHERE e.`d_id`=d.`id` AND e.id=#{id};
</select> </mapper>

3.编写测试代码

package com.test.resultMap;

import com.bean.Employee;
import com.dao.EmployeeMapperPlus;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream; public class test_tp30 {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test06() throws Exception{ SqlSession openSession = getSqlSessionFactory().openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
System.out.println("--------多表关联查询,级联属性的封装结果!--------");
Employee employee = mapper.getEmpAndDept(1);
System.out.println(employee);
System.out.println("--------多表关联查询,使用association进行连接!--------");
Employee employee2 = mapper.getEmpAndDept2(1);
System.out.println(employee2);
openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
}finally {
openSession.close();
}
}
}

测试结果

--------多表关联查询,级联属性的封装结果!--------
DEBUG 12-01 10:37:03,938 ==> Preparing: 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,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,958 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,968 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'}
--------多表关联查询,使用association进行连接!--------
DEBUG 12-01 10:37:03,969 ==> Preparing: 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,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,969 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,970 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'} Process finished with exit code 0

Mybatis3.1-[tp-30-31]-select_resultMap_关联查询_级联属性封装结果__association定义关联对象封装规则的更多相关文章

  1. mybatis_09关联查询_一对一

    复杂查询时,单表对应的po类已不能满足输出结果集的映射. 所以有些时候就需要关联查询_一对一:通过条件查询结果每个字段都唯一 一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合 方 ...

  2. JavaWeb_(Mybatis框架)关联查询_六

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  3. mybatis_11关联查询_多对多

    一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合   映射思路 pojo类设计思路 v  将用户信息映射到user中. v  在user类中添加订单列表属性List<Ord ...

  4. mybatis_10关联查询_一对多

    在使用mybatis框架的时候,很多时候需要一个查询结果里的属性包含多个对象,即一条查询结果有属性是集合,这个时候就需要使用collection标签 模型里面有集合 案例: 第一步:在Orders中添 ...

  5. 【mysql】关联查询_子查询_排序分组优化

    1. 关联查询优化 1.1 left join 结论: ①在优化关联查询时,只有在被驱动表上建立索引才有效! ②left join 时,左侧的为驱动表,右侧为被驱动表! 1.2 inner join ...

  6. Mybatis之关联查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  8. Mybatis框架学习总结-表的关联查询

    一对一关联 创建表和数据:创建一张教师表和班级表,这里假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系. CREATE TABLE teacher( t_id INT PRIM ...

  9. MyBatis:一对多关联查询

    MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...

随机推荐

  1. CentOS7.6安装Pycharm并添加快捷方式

    1.以用户身份登录jiangshan 并建立/home/jiangshan/pycharm文件夹2.下载 pycharm-community-anaconda-2019.1.3.tar.gz 放置在/ ...

  2. CSS3 实现别样图型

    1.爱心 利用 div 的正方形和伪类的圆型组合而成 <!DOCTYPE html> <html> <head> <meta charset="UT ...

  3. [Visual Studio] - 使用 Fiddler 时,禁止监控 VSHub 请求的方法

    背景 VS + Fiddler 调试 WebAPI,监控请求包含大量 VSHub Request.http://localhost:49161/vshub/bb195f2e0d5c4765b9411f ...

  4. Scratch(二)来不及解释了,马上开始编程游戏

    来来来,上一期你们都经过了”HelloWorld”神咒的加持,已入编程大门,我们今天就开始一边做游戏,一边熟悉Scratch. “我只是切出去抢了个红包,一回来就到了编程游戏的环节了?” 对,你没跑错 ...

  5. Scratch编程:躲开鲨鱼(五)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这是一款简单的小游戏,实现了用鼠标控制一条小海星在水里游 ...

  6. springcloud 1.5 与 springcloud 2.0 配置区别

    eureka配置区别: 1.5:${spring.cloud.client.ipAddress}:${server.port} 2.0:${spring.cloud.client.ip-address ...

  7. springboot + shiro 构建权限模块

    权限模块基本流程 权限模块的基本流程:用户申请账号和权限 -->登陆认证 -->安全管控模块认证 -->调用具体权限模块(基于角色的权限控制) --> 登陆成功 -->访 ...

  8. javascript序列化表单追加参数

    js序列化表单后追加参数方式: 追加参数:token,status var data = $.param({"token":token, "status":st ...

  9. C#-Parallel

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Li ...

  10. Nginx的启动、停止等命令

    Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...