1. MyBatis 映射文件之<select>标签

  1. <select>用来定义查询操作;

    • "id": 唯一标识符,需要和接口中的方法名一致;
    • parameterType: 参数类型,可以不传,MyBatis 会根据 TypeHandler 自动推断;
    • resultType: 返回值类型;使用别名或全类名,如果返回的是集合,定义集合中元素的类型;

      不能和 resultMap 同时使用;
    • resultMap:可以实现高级结果集映射;
// Department.java
public class Department{
private Integer id;
private String departmentName;
private List<Employee> emps; get 和 set 方法(略)
} // Employee.java
public class Employee{
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept; get 和 set 方法(略)
} // EmployeeMapper.java 接口
public interface EmployeeMapper(){ // 查询单个
public Employee getEmpById(Integer id); // 返回 List 集合
public List<Employee> getEmpsByLastNameLike(String lastName); // 返回一条记录,类型为map: key 就是列名,值就是对应的值
public Map<String,Object> getEmpByIdReturnMap(Integer id); // 返回多条记录,封装到一个map中: Map<Integer,Employee>: 键为该条记录的主键,
// 值是封装后的 JavaBean 对象
// @MapKey 用于指定map集合的主键
@MapKey("id")
public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName); // 查询员工和所在部门信息
public Employee getEmpAndDept(Integer id); // 查询员工和所在部门信息(分步查询)
public Employee getEmpByIdStep(Integer id); // 查询某个部门下的具体员工信息
public List<Employee> getEmpsByDeptId(Integer deptId);
} // EmployeeMapper.xml
<!--
resultMap: 自定义结果集映射规则;
type: 自定义规则的 Java 类型
id: 唯一标识,方便引用
-->
<resultMap type="cn.itcast.mybatis.bean.Employee" id="MyEmp"> <!--
id: 指定主键列的封装规则;
column: 指定哪一列; prperty: 指定对应的 JavaBean 属性;
result: 定义普通列封装规则
其他不指定的列会自动封装; 建议: 只要写resultMap,就把全列的映射都写上
-->
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
</resultMap> <select id="getEmpById" resultMap="MyEmp">
select * from tbl_employee where id=#{id}
</select> <select id="getEmpsByLastNameLike" resultType="cn.itcast.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select> <select id="getEmpByIdReturnMap" resultType="map">
select * from tbl_employee where id=#{id}
</select> <select id="getEmpByLastNameLikeReturnMap" resultType="cn.itcast.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select> <!-- 查询某个部门下的具体员工信息 -->
<select id="getEmpsByDeptId" resultType="cn.itcast.mybatis.bean.Employee">
select * from tbl_employee where d_id=#{deptId}
</select> <!--
resultMap 使用场景一: 查询员工的同时查询员工对应的部门
-->
<resultMap type="cn.itcast.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"/> <!-- 第二种方式: 使用 association 标签,可以指定联合的javaBean对象
property="dept": 指定哪个属性是联合的对象;
javaType: 指定这个属性对象的类型(不能省略)
-->
<association property="dept" javaType="cn.itcast.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>
<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> <!-- 第三种方式: 使用 associatioin 进行分步查询
1. 新建 DepartmentMapper 接口, getDeptById(Integer id) 方法
2. 创建对应的映射文件,并在 web.xml 中进行注册
3. 查询员工的同时查询员工对应的部门(分布查询)
3.1 先按照员工 id 查询员工信息;
3.2 根据查询到的员工信息中的 d_id 值去部门表查出部门信息;
3.3 将部门设置到员工中;
-->
<resultMap type="cn.itcast.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="cn.itcast.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</resultMap>
<select id="getEmpByIdStep" resultMap="MyEmpByStep">
select * from tbl_employee where id=#{id}
</select> <!-- 分步查询可以实现延迟加载:
我们每次查询 Employee 对象的时候,都将Employee 和 Department 一起查询出来;
改进: 部门信息在我们使用的时候,再去查询
分步查询的基础之上,在 mybatis-config.xml 中添加两个配置
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
--> <!--
resultMap 中的标签:
鉴别器: <discriminator javaType=""></discriminator>
mybatis 可以使用 discriminator 判断某列的值,然后根据某列的值改变封装行为
以封装 Employee 为例:
如果查询出的是女生,就把部门信息查询出来,否则,不查询;
如果是男生: 把 last_name 这一列的值赋值给 email;
--> <resultMap type="cn.itcast.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 类型; mybatis 已经为 java 类型起了别名
-->
<discriminator javaType="string" column="gender">
<!-- 0 表示女生, resultType: 指定封装的结果类型,不能缺少 -->
<case value="0" resultType="cn.itcast.mybatis.bean.Employee">
<association property="dept"
select="cn.itcast.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case> <!-- 1 表示男生, 如果是男生, 把last_name这一列赋值给 email -->
<case value="1" resultType="cn.itcast.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>
<select id="getEmpByIdStep" resultMap="MyEmpDis">
select * from tbl_employee where id=#{id}
</select> // DpartmentMapper.xml
<!-- 按照 id 查询部门 -->
<select id="getDpetById" resultType="cn.itcast.mybatis.bean.Department">
select id,dept_name departmentName from tbl_dept where id=#{id}
</select> <!--
resultMap 使用场景二: 查询部门的同时,查询出部门对应的所有员工信息
-->
<resultMap type="cn.itcast.mybatis.bean.Department" id="MyDept">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/> <!--
嵌套结果集的方式,使用 collection 标签定义关联的集合类型元素的封装规则
collection: 定义关联集合类型属性的封装规则
ofType: 指定集合里面元素的类型
-->
<collection property="emps" ofType="cn.itcast.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> <!-- 查询部门的同时,查询部门下所有员工 -->
<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 标签中传递多列的值: 将多列的值封装成 map 传递
<collection property="..."
select="..."
colomn="{key1=column1,key2=column2...}"
fetchType="lazy": 默认使用延迟加载, 也可以禁掉:将lazy换为"eager">
</collection>
-->
<resultMap type="cn.itcast.mybatis.bean.Department" id="MyDeptStep">
<id column="id" property="id"/>
<result column="dept_name" property="departmentName"/> <collection property="emps"
select="cn.itcast.mybatis.dao.EmployeeMapper.getEmpsByDeptId"
column="id"/>
</collection>
</resultMap>
<select id="getDeptByIdStep" resultMap="MyDeptStep">
select id,dept_name from tbl_dept where id=#{id}
</select> // 测试类
public class MyBatisTest{
// 1. 获取 SqlSessionFactory 对象(略) @Test
public void test() throws IOException{
// 2. 获取 SqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); // 3. 获取 SqlSession 实例
SqlSession openSession = sqlSessionFactory.openSession(); try{
// 4. 获取接口的实现类对象
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); // 5. 查询,返回类型为 List
List<Employee> list = mapper.getEmpsByLastNameLike("%e%");
for(Employee emp : list){
System.out.println(emp);
} // 返回类型为 Map
Map<String,Object> map = mapper.getEmpByIdReturnMap(1);
System.out.println(map); Map<String,Employee> map = mapper.getEmpByLastNameLikeReturnMap("%e%");
System.out.println(map); // 查询员工的同时查询所在部门
Employee emp = mapper.getEmpAndDept(2);
System.out.println(emp); }finally{
openSession.close();
}
}
}

参考资料

MyBatis 映射文件详解的更多相关文章

  1. MyBatis 映射文件详解(六)

    MyBatis 配置文件类型 MyBatis配置文件有两种类型,如下: 全局配置文件(如 mybatis-config.xml) Mapper XML 映射文件(如 UserMapper.xml) 上 ...

  2. Mybatis SQL映射文件详解

    Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...

  3. Hibernate配置文件和映射文件详解

    Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...

  4. mybatis核心文件详解

    MyBatis配置文件详解 configuration  这是配置文件的根元素标签,所有的其他元素都要在这个标签下使用. environments   用于管理所有环境,并可以指定默认使用哪个环境,通 ...

  5. Mybatis学习(三)————— 映射文件详解

    前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...

  6. Mybatis(三) 映射文件详解

    前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...

  7. MyBatis的SQL语句映射文件详解

    SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用 < ...

  8. Mybatis的配置文件和映射文件详解

    一.Mybatis的全局配置文件 1.SqlMapConfig.xml是mybatis的全局配置文件,配置内容如下: properties(属性) settings(全局配置参数) typeAlias ...

  9. MyBatis映射配置文件详解

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...

随机推荐

  1. Unity编辑器下获取动画的根运动状态并修改

    我最初想直接修改.anim文件 但通过后来得到的信息,其实根运动状态储存在FBX.meta文件里,转出的.anim文件虽然也有根运动的信息但是算是塌陷过的,无法进行开关操作. 这是我针对有根运动.an ...

  2. atitit. 管理哲学 大毁灭--- 如何防止企业的自我毁灭

    atitit. 管理哲学 大毁灭---  如何防止企业的自我毁灭 1. 为什么企业组织的生命力 普遍不如国家组织的长久 2 2. 企业的不稳定因子如下:: 2 3. 决策制度 2 3.1. 我们老大说 ...

  3. [css]后台管理系统布局

    知识点: 绝对定位+overflowhidden 整体思路 三大块 pg-header---需要固定 (height:48px) pg-content menu 右侧菜单-需要固定(width:200 ...

  4. 使用Nginx Lua实现redis高性能http接口

    使用Nginx Lua实现redis高性能http接口 时间 -- :: 峰云就她了 原文 http://xiaorui.cc/2015/01/27/使用nginx-lua实现redis高性能http ...

  5. 通过exists判断数据,并查找存在的数据

    ----通过exists判断数据,并查找存在的数据---以scott用户的emp 及dept表为例 select * from emp; select * from dept; ---查找emp表中的 ...

  6. C# 一个长度为100的int数组,插入1-100的随机数,不能重复,如何写

    int[] intArr = new int[100]; ArrayList myList = new ArrayList(); Random rnd = new Random(); while (m ...

  7. Archive for required library xx cannot be read or is not a valid ZIP file

    原因:maven下载的jar包有问题,导致maven编译的时候出错 解决方法:找到jar包所在的文件路径,在网上重新下载个相同版本的jar包,问题解决

  8. 跟着百度学PHP[14]-PDO的预处理语句2

    在$sql = $pdo -> prepare("insert into users(gold,user,password) values(?,?,?)"):条语句我们不仅仅 ...

  9. spring的数据源

    Spring提供了两个这样的数据源(都位于org.springframework.jdbc.datasource程序包里):        DriverManagerDataSource:在每个连接请 ...

  10. Java 设计模式01 - 简单工厂模式

    先要学习设计模式之前的先看看一些基础 UML类图简单说明 可以先看看我的这篇博客: UML类图简单说明,学习编程思路的必会技能 接下来才是重点,开始我们的旅程吧. 一.UML类图展示 我们要用简单工厂 ...