MyBatis 映射文件详解
1. MyBatis 映射文件之<select>
标签
<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 映射文件详解的更多相关文章
- MyBatis 映射文件详解(六)
MyBatis 配置文件类型 MyBatis配置文件有两种类型,如下: 全局配置文件(如 mybatis-config.xml) Mapper XML 映射文件(如 UserMapper.xml) 上 ...
- Mybatis SQL映射文件详解
Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...
- Hibernate配置文件和映射文件详解
Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...
- mybatis核心文件详解
MyBatis配置文件详解 configuration 这是配置文件的根元素标签,所有的其他元素都要在这个标签下使用. environments 用于管理所有环境,并可以指定默认使用哪个环境,通 ...
- Mybatis学习(三)————— 映射文件详解
前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...
- Mybatis(三) 映射文件详解
前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...
- MyBatis的SQL语句映射文件详解
SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用 < ...
- Mybatis的配置文件和映射文件详解
一.Mybatis的全局配置文件 1.SqlMapConfig.xml是mybatis的全局配置文件,配置内容如下: properties(属性) settings(全局配置参数) typeAlias ...
- MyBatis映射配置文件详解
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...
随机推荐
- Java并发编程(三):并发模拟(工具和Java代码介绍)
并发模拟工具介绍 ① Postman : Http请求模拟工具 从图上我们可以看出,Postman模拟并发其实是分两步进行操作的.第一步:左边的窗口,在窗口中设置相关接口以及参数,点击运行进行第二步. ...
- linux中的系统服务--daemon
简单的说,系统为了某些功能必须要提供一些服务 (不论是系统本身还是网络方面),这个服务就称为 service . 但是 service 的提供总是需要程序的运行吧!否则如何运行呢?所以达成这个 ser ...
- tensorflow 之模型的保存与加载(一)
怎样让通过训练的神经网络模型得以复用? 本文先介绍简单的模型保存与加载的方法,后续文章再慢慢深入解读. #!/usr/bin/env python3 #-*- coding:utf-8 -*- ### ...
- python之字符串处理
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: strformat.py #A ...
- HTTP Range header
http://stackoverflow.com/questions/3303029/http-range-header *************************** 58 down vot ...
- flume+kafka+storm打通过程
0.有的地方我已经整理成脚本了,有的命令是脚本里面截取的 1.启动hadoop和yarn $HADOOP_HOME/sbin/start-dfs.sh;$HADOOP_HOME/sbin/start- ...
- STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
Ⅰ.写在前面 上一篇文章是“STM32F10x_模拟I2C读写EEPROM”,讲述使用IO口模拟I2C总线通信,对EEPROM(AT24Xxx)进行读写操作的过程. 上一篇文章主要内容:I2C协议.模 ...
- js 控制按钮点击后不可用
<input type="button" id="btn" value="免费获取验证码" /> <script type ...
- K-NN算法 学习总结
1. K-NN算法简介 K-NN算法 ( K Nearest Neighbor, K近邻算法 ), 是机器学习中的一个经典算法, 比较简单且容易理解. K-NN算法通过计算新数据与训练数据特征值之间的 ...
- 在mybatis 中批量加载mapper.xml
可以直接加载一个包文件名,将这个包里的所有*mapper.xml文件加载进来. 指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载: 必须按一定的标准:即xml文件和 ...