mybatis学习 十三 resultMap标签 一对一
1 .<resultMap>标签
写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时select标签中,select语句的返回字段要与resultType属性指定的类的属性名称一致,此时成为自动映射,但是当要接收查询接口的类的属性名称,与select语句的返回字段不一致时,mybatis时无法自动进行映射的,此时需要程序员手动指定映射,此时就用到resultMap标签。
2. <resultMap>标签的使用
使用<resultMap>标签时,<select>标签不写 resultType 属性,而是使用 resultMap 属性引用<resultMap>标签. <resultMap>标签中的type属性的值就是select语句返回字段要映射的类
<resultMap type="teacher" id="mymap">
<!-- 主键使用 id 标签配置映射关系 -->
<id column="id" property="id1" />
<!-- 其他列使用 result 标签配置映射关系 -->
<result column="name" property="name1"/>
</resultMap>
<select id="selAll" resultMap="mymap">
select * from teacher
</select>
3. 使用 resultMap 实现关联单个对象(N+1 方式)
N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息.在 service 里面写的代码,由 mybatis 完成装配。
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher teacher;
}
public class Teacher {
private int id;
private String name;
}
studentMapper.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.xxx.mapper.StudentMapper">
<resultMap type="student" id="stuMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="tid" property="tid"/> <association property="teacher" column="tid" select="com.xxx.mapper.TeacherMapper.selById" >
</association>
</resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
</select>
</mapper>
TeacherMappe.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.xxx.mapper.TeacherMapper">
<select id="selById" resultType="teacher" parameterType="int">
select * from teacher where id=#{0}
</select>
</mapper>
(1)<association> 装配一个对象时使用
(2) property: 对象在类中的属性名
(3)select:通过哪个查询查询出这个对象的信息
(4) column: 把当前表的哪个列的值做为参数传递给另一个查询
(5) 大前提使用 N+1 方式.时如果列名和属性名相同可以不配置,使用 Auto mapping 特性.但是 mybatis 默认只会给列专配一次, 即如果把一个列当做另外一个查询的查询条件,别的映射列可以不用配置,但是这个当做参数的列一定要配置。即<result column="tid" property="tid"/>一定要配置,因为
<association property="teacher" column="tid" select="com.xxx.mapper.TeacherMapper.selById" >
</association>中的查询用到了tid作为参数。
(6)javaType 属性:<association/>专配完后返回一个什么类型的对象.取值是一个类(或类的别名)
mybatis学习 十三 resultMap标签 一对一的更多相关文章
- MyBatis学习-映射文件标签篇(select、resultMap)
MyBatis 真正的核心在映射文件中.比直接使用 JDBC 节省95%的代码.而且将 SQL 语句独立在 Java 代码之外,可以进行更为细致的 SQL 优化. 一. 映射文件的顶级元素 selec ...
- mybatis学习 十四 resultMap标签 一对一(联合查询)
1.使用 resultMap 实现关联单个对象(联合查询方式) <resultMap type="Student" id="stuMap1"> &l ...
- Mybatis学习第四天——一对一&&一对多
两表关系: 1.Mybatis中一对一关系 <!-- 两表联查,通过相同属性user_id left join 表示以左边的表为主表 --> <select id="fin ...
- Mybatis学习笔记-ResultMap结果集映射
解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...
- Mybatis学习笔记导航
Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
- Mybatis学习(四)————— 高级映射,一对一,一对多,多对多映射
一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种关系来讲,比如有员工和部 ...
- mybatis学习笔记(10)-一对一查询
mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...
- Mybatis学习总结(六)——高级映射(一对一,一对多,多对多)
一.订单商品数据模型 1.数据库执行脚本 创建数据库表代码: /*Table structure for table `t_user` */ CREATE TABLE t_user ( id INT ...
随机推荐
- telnet ip/域名 端口 是否成功
有时候会ping ip 通,但是telnet不通,可能端口未开. telnet不成功,则显示不能打开到主机的链接,链接失败 . telnet成功,则进入telnet页面(全黑的),证明端口可用.
- 十 suprocess模块
1 import subprocess 2 3 ''' 4 sh-3.2# ls /Users/egon/Desktop |grep txt$ 5 mysql.txt 6 tt.txt 7 事物.tx ...
- NumPy 统计函数
NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下: numpy.amin() 和 numpy.amax() numpy.a ...
- Django项目之客户
关于客户的操作 主页(被继承) {% load static %} <!DOCTYPE html> <html lang="en"> <head> ...
- 前端、数据库、Django简单的练习
一.前端 1.前端页面由哪几层构成,分别是什么,作用是什么? 分为:结构层(html),表示层(css),行为层(js). 结构层 超文本标记语言.由HTML或XHTML之类的标记语言负责创建.标签, ...
- gridview空间使用
1.HTML代码 <asp:GridView ID="gv_Info" runat="server" AutoGenerateColumns=" ...
- 安恒7月赛wp
1.[order] 这道题,发现order参数处有注入点,于是就使用sqlmap盲注,emmmm,学到了sqlmap的一些小窍门. 首先,解题的语句是: sqlmap -u "htt ...
- 上海大都会赛 I Matrix Game(最大流)
At the start of the matrix game, we have an N x M matrix. Each grid has some balls.The grid in (i,j) ...
- TZOJ 1221 Tempter of the Bone(回溯+剪枝)
描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...
- UVa 10129 Play on Words(有向图欧拉路径)
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...