MyBatis映射文件(二)
目录
一、select元素
1.参数介绍
2.resultType
(1)返回实体对象
(2)返回List集合
(3)返回Map集合,object>
(4)返回Map<主键类型,实体类>集合
3.setting设置自动映射
4.自定义resultMap映射
5.association
(1)联合查询
(2)association-嵌套结果集
(3)association-分步查询&延迟加载
(4)Collection-集合类型&嵌套结果集
(5)Collection-分布查询&延迟加载
(6)discriminator鉴别器
一、select元素
1.参数介绍
select元素用来定义查询语句
id:唯一标识符
用来引用这条语句,需要和接口的方法名一致
parametertype:参数类型
可以不传,MyBatis会根据TypeHandler自动推断
resultType:返回值类型
别名或全类名,如果返回的是集合,定义集合中元素的类型。不能和resultMap同时使用。
2.resultType
(1)返回实体对象
<!-- public Employee getEmpByMap(Map<String, Object> map); --><select id="getEmpByMap" resultType="com.itheima.domain.Employee">select * from ${tableName} where id=${id} and last_name=#{lastName}</select>
(2)返回List集合
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); --><!--resultType:如果返回的是一个集合,要写集合中元素的类型 --><select id="getEmpsByLastNameLike" resultType="com.itheima.domain.Employee">select * from tbl_employee where last_name like #{lastName}</select>
(3)返回Map<String,Object>集合
键值分别对应列名和对应的值
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); --><select id="getEmpByIdReturnMap" resultType="map">select * from tbl_employee where id=#{id}</select>
(4)返回Map<主键类型,实体类>集合
键值分别对应主键名和对应的实体类
<!--
public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);
--><select id="getEmpByLastNameLikeReturnMap"resultType="com.itheima.domain.Employee">select * from tbl_employee where last_name like #{lastName}</select>
@MapKey注解:可以指定封装这个map的时候使用哪个属性作为主键
@MapKey("lastName")public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
3.setting设置自动映射
autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一致
如果autoMappingBehavior设置为null则会取消自动映射
数据库字段命名规范,POJO属性符合驼峰命名法,如A_COLUMN===aColumn,我们可以开启自动驼峰命名规则映射功能,mapUnderscoreToCamelCase=true。
4.自定义resultMap映射
(1)属性
type:自定义规则的Java类型
id:唯一标识,用于标识一个result map
autoMapping:如果设置这个属性,MyBatis将会为这个ResultMap开启或者关闭自动映射。这个属性会覆盖全局的属性 autoMappingBehavior。默认值为:unset。
<resultMap type="com.itheima.domain.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><!-- resultMap:自定义结果集映射规则; --><!-- public Employee getEmpById(Integer id); --><select id="getEmpById" resultMap="MySimpleEmp">select * from tbl_employee where id=#{id}</select>
5.association
场景:
查询Employee的同时查询员工对应的部门
(1)联合查询
级联属性封装结果集
<resultMap type="com.itheima.domain.Employee" id="MyDifEmp"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!-- dept为Employee的 Department dept成员变量名Department有id和departmentName属性--><result column="did" property="dept.id"/><result column="dept_name" property="dept.departmentName"/></resultMap>
(2)association-嵌套结果集
<resultMap type="com.itheima.domain.Employee" id="MyDifEmp2"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!-- association可以指定联合的javaBean对象property="dept":指定哪个属性是联合的对象javaType:指定这个属性对象的类型[不能省略]--><association property="dept" javaType="com.itheima.domain.Department"><id column="did" property="id"/><result column="dept_name" property="departmentName"/></association></resultMap>
(3)association-分步查询&延迟加载
先按照员工id查询员工信息
根据查询出来的d_id去部门表查出部门信息
部门设置到员工中
<resultMap type="com.itheima.domain.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.itheima.domain.DepartmentMapper.getDeptById"column="d_id"></association>
开启延迟加载和属性按需加载需要进行如下配置:
<!--lazyLoadingEnabled:是否开启延迟加载功能aggressiveLazyLoading:对任意延迟属性的调用会使带有延迟加载属性的对象完整加载
--><settings> <setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/>
</settings>
(4)Collection-集合类型&嵌套结果集
<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --><resultMap type="com.itheima.domain.Department" id="MyDept"><id column="did" property="id"/><result column="dept_name" property="departmentName"/><!-- collection定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型--><collection property="emps" ofType="com.itheima.domain.Employee"><!-- 定义这个集合中元素的封装规则 --><id column="eid" property="id"/><result column="last_name" property="lastName"/><result column="email" property="email"/><result column="gender" property="gender"/></collection></resultMap>
(5)Collection-分布查询&延迟加载
<resultMap type="com.itheima.domain.Department" id="MyDeptStep"><id column="id" property="id"/><id column="dept_name" property="departmentName"/><collection property="emps" select="com.itheima.domain.EmployeeMapperPlus.getEmpsByDeptId"column="{id}" fetchType="lazy"></collection><!--多列的值传递过去:将多列的值封装map传递;column="{key1=column1,key2=column2}"fetchType="lazy":表示使用延迟加载;- lazy:延迟- eager:立即-->
</resultMap>
(6)discriminator鉴别器
<!-- <discriminator javaType=""></discriminator>鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为封装Employee:如果查出的是女生:就把部门信息查询出来,否则不查询;如果是男生,把last_name这一列的值赋值给email;--><resultMap type="com.itheima.domain.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类型 --><discriminator javaType="string" column="gender"><!--女生 resultType:指定封装的结果类型;不能缺少。/resultMap--><case value="0" resultType="com.itheima.domain.Employee"><association property="dept" select="com.itheima.domain.DepartmentMapper.getDeptById"column="d_id"></association></case><!--男生 ;如果是男生,把last_name这一列的值赋值给email; --><case value="1" resultType="com.itheima.domain.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></resultMap>
MyBatis映射文件(二)相关推荐
- Mybatis映射文件SQL语句模糊查询,#和$的区别和注意事项
Mybatis映射文件SQL语句模糊查询 1. "%"#{value}"%" 在参数中不需要添加 %_ 推荐使用 2. '%${value}%' 在参数中不需要 ...
- MyBatis映射文件(一)
目录 一.MyBatis映射文件 1.insert.update.delete元素 2.主键生成方式 3.参数(Parameters)传递 (1)单个参数 (2)多个参数 (3)命名参数 (4)POJ ...
- 3 了解MyBatis映射文件
3.1 认识映射文件: 介绍: MyBatis的映射文件主要用于定义业务操作命令,实现对象与数据表字段的映射,并控制对最终结果的封装.MyBatis是针对SQL构建,相对具有相同功能的JDBC ...
- Mybatis映射文件!CDATA[[]] 转义问题
有没有觉得在mybatis映射文件写>(大于),<(小于)的时候贼麻烦,还得把"<",">"换位为"<", & ...
- MyBatis映射文件如何给数据库的int类型字段“插入”string类型记录
MyBatis映射文件如何给数据库的int类型字段"插入"string类型记录. 数据库中对应字段名称和类型如下: 给数据库的int类型字段"插入"string ...
- 利用Maven逆向工程生成mybatis映射文件
一,pom.xml 注意修改逆向工程配置文件的路径 <build><pluginManagement><plugins><plugin><grou ...
- Mybatis 映射文件
Mapper 就是"映射"的意思,Mapper 文件时 Mybatis 中的 SQL 语句的配置文件,其会在运行时加载 SQL 语句并映射相应参数.在 Mybatis 的全局配置文 ...
- 配置MyBatis映射文件以及核心配置文件模板
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.db.properties模板 二.映射文件mapper.xml模板 三.---------MyBatis核心文件 ...
- oracle 监听拒绝连接,报mybatis映射文件错误Listener refused the connection with the following error: ORA-12519, TNS
以下错误是:数据库连接数问题. 仔细检查映射文件.xml文件正确 后来发现是数据库最大连接数已满 修改之alter system set processes = 4000 scope = spfile ...
- MyBatis映射文件1(增删改、insert获取自增主键值)
增删改 Mybatis为我们提供了<insert>.<update>.<delete>标签来对应增删改操作 在接口中写增删改的抽象方法 void addEmp(Em ...
最新文章
- 用mapXtreme Java开发web gis应用 (上)
- 990. Satisfiability of Equality Equations
- 【Flask】Nginx / Gunicorn入门:部署你的Flask项目
- mysql自增id获取失败
- Android Studio apk 打包流程
- 背包——变向背包(hdu2546,1114,1203,2189)
- c语言jam的随机数,c – 如何重置随机数引擎?
- chrome 版本 29.0.1547.76 m 解决打开新标签页后的恶心页面的问题
- 通过JDBC和Hibernate对Clob和Blob的操作
- caffe 基础教程
- GetLastError错误码大全
- CSC7156 SOP-7
- linux ftp服务器登录异常解决
- jieba库词频统计_jieba库的使用与词频统计
- python怎么读取dat文件_小白也能学会系列:用python文件读写代码实例!(简单案例)...
- Java中new一个对象的步骤:
- Diango做blog.docx
- 计算机数学公式画爱心教程,几何画板如何绘制爱心?几何画板爱心函数教程
- 功能测试几种方法简单介绍
- elasticsearch 基础 —— Parent-Child 父子关系(5.X老版本)