1、简介
resultType能够把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。 若是sql查询到的字段与pojo的属性名不一致,则须要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中。resultMap能够实现将查询结果映射为复杂类型的pojo,好比在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。java
2、简单使用
order订单表:id,user_id,number,createtime,notesql
<!-- 查询全部的订单数据 --> <!-- resultMap:填入配置的resultMap标签的id值 --> <select id="queryOrderAll" resultMap="orderResultMap"> SELECT id, user_id, number, createtime, note FROM `order` </select> <!-- resultMap最终仍是要将结果映射到pojo上,type就是指定映射到哪个pojo --> <!-- id:设置ResultMap的id --> <resultMap id="orderResultMap" type="order"> <!-- 定义主键 ,很是重要。若是是多个字段,则定义多个id --> <!-- property:主键在pojo中的属性名 --> <!-- column:主键在数据库中的列名 --> <id property="id" column="id" /> <!-- 定义普通属性 --> <result property="userId" column="user_id" /> <result property="number" column="number" /> <result property="createtime" column="createtime" /> <result property="note" column="note" /> </resultMap>
3、关联查询
一对一数据模型:订单用户
一个订单信息只会是一我的下的订单,因此从查询订单信息出发关联查询用户信息为一对一查询
首先在Order类中增长属性User,而后编写mapper:数据库
<resultMap type="order" id="orderUserResultMap"> <id property="id" column="id" /> <result property="userId" column="user_id" /> <result property="number" column="number" /> <result property="createtime" column="createtime" /> <result property="note" column="note" /> <!-- association :配置一对一属性 --> <!-- property:order里面的User属性名 --> <!-- javaType:属性类型 --> <association property="user" javaType="user"> <!-- id:声明主键,表示user_id是关联查询对象的惟一标识--> <id property="id" column="user_id" /> <result property="username" column="username" /> <result property="address" column="address" /> </association> </resultMap> <!-- 一对一关联,查询订单,订单内部包含用户属性 --> <select id="queryOrderUserResultMap" resultMap="orderUserResultMap"> SELECT o.id, o.user_id, o.number, o.createtime, o.note, u.username, u.address FROM `order` o LEFT JOIN `user` u ON o.user_id = u.id </select>
一对多数据模型:用户订单
从用户信息出发查询用户下的订单信息则为一对多查询,由于一个用户能够下多个订单
首先在User类中增长属性List<Order> orders,而后编写mapper:mybatis
<resultMap type="user" id="userOrderResultMap"> <id property="id" column="id" /> <result property="username" column="username" /> <result property="birthday" column="birthday" /> <result property="sex" column="sex" /> <result property="address" column="address" /> <!-- 配置一对多的关系 property:填写pojo类中集合类类属性的名称 javaType:填写集合类型的名称 --> <collection property="orders" javaType="list" ofType="order"> <!-- 配置主键,是关联Order的惟一标识 --> <id property="id" column="oid" /> <result property="number" column="number" /> <result property="createtime" column="createtime" /> <result property="note" column="note" /> </collection> </resultMap> <!-- 一对多关联,查询订单同时查询该用户下的订单 --> <select id="queryUserOrder" resultMap="userOrderResultMap"> SELECT u.id, u.username, u.birthday, u.sex, u.address, o.id oid, o.number, o.createtime, o.note FROM `user` u LEFT JOIN `order` o ON u.id = o.user_id </select>
4、对返回数据进行分组
假设有以下sql查询语句:
select id, otherId from mytalbe
返回数据:
id otherId
01 00001
01 00002
01 00003
02 00004app
有java实体类以下:
class Id{
private String id;
private List<String> otherId;
}
怎样经过执行一次查询,返回以下形式的数据呢?
List<Id> aa = new ArrayList<Id>();
aa = ...//经过mybatis执行slq
aa:[
{01,[00001,00002,00003,00004]},
{02,[00004]}
]
实现方法以下:.net
<resultMap id="myid" type="Id"> <id Property="id" column="id"> <collecton property="otherId" ofType="String" javaType="ArrayList"> <result colume="otherId"> </collection> </resultMap> <select id="list" resultMap="myid"> select id, otherId from mytalbe </select>
须要注意的是<id> 标签,这是分组的标识。必定要注意。
文章参考:https://blog.csdn.net/qq_42780864/article/details/81429114code
文章参考:https://blog.csdn.net/shijunjoy/article/details/75119671对象