MyBatis3.X复杂Sql查询
MyBatis3.X的resultMap
1.Mybatis的sql语句返回的结果有两种
- resultType
- 查询出的字段在相应的pojo中必须有和它相同的字段对应,或者基本数据类型
- 适合简单查询
- resultMap
需要自定义字段,或者多表查询,一对多等关系,比resultType更强大
适合复杂查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<resultMap id= "VideoResultMap" type= "Video" >
<! --
id 指定查询列列的唯⼀一标示
column 数据库字段的名称
property pojo类的名称
-->
<id column = "id" property= "id" jdbcType= "INTEGER" />
<result column = "video_tile" property= "title" jdbcType= "VARCHAR" />
<result column = "summary" property= "summary" jdbcType= "VARCHAR" />
<result column = "cover_img" property= "coverImg" jdbcType= "VARCHAR" />
</resultMap>
< select id= "selectBaseFieldByIdWithResultMap" resultMap= "VideoResultMap" >
select id , title as video_tile, summary, cover_img from video where id = #{video_id}
</ select >
|
ResultMap复杂对象一对一查询结果映射之association
association:映射到POJO的某个复杂类型属性,比如订单order对象里面包含user对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<!-- 名称空间,需要保存全局唯一,最好是和dao层的Java接口一致
可以映射sql语句到对应的方法名参数和返回值
mybatis是使用接口动态代理
-->
<mapper namespace= "net.xiaotiancai.online_class.dao.VideoOrderMapper" >
<resultMap id= "VideoOrderResultMap" type= "VideoOrder" >
<id column= "id" property= "id" ></id>
<result column= "user_id" property= "userId" ></result>
<result column= "out_trade_no" property= "outTradeNo" ></result>
<result column= "state" property= "state" ></result>
<result column= "total_fee" property= "totalFee" ></result>
<result column= "video_id" property= "videoId" ></result>
<result column= "video_title" property= "videoTitle" ></result>
<!--
配置属性一对一
property对应videoOrder里面的User
javaType对应这个属性的类型
-->
<association property= "user" javaType= "User" >
<id column= "user_id" property= "id" ></id>
<result column= "name" property= "name" ></result>
<result column= "head_img" property= "headImg" ></result>
<result column= "phone" property= "phone" ></result>
</association>
</resultMap>
<!--一对一订单查询,订单内部包含用户属性-->
<select id= "queryVideoOrderList" resultMap= "VideoOrderResultMap" >
select
o.id id,
o.user_id,
o.out_trade_no,
o.state,
o.total_fee,
o.video_id,
o.video_title,
u.name,
u.head_img,
u.phone
from video_order o left join user u on o.user_id = u.id
</select>
</mapper>
|
代码
1
2
3
4
|
// resultmap association关联查询
VideoOrderMapper videoOrderMapper =sqlSession.getMapper(VideoOrderMapper. class );
List<VideoOrder> videoOrderList = videoOrderMapper.queryVideoOrderList();
System.out.println(videoOrderList.toString());
|
ResultMap复杂对象一对多查询结果映射之collection
collection: 一对多查询结果查询映射,比如user有多个订单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<resultMap id= "UserOrderResultMap" type= "User" >
<id property= "id" column= "id" />
<result property= "name" column= "name" />
<result property= "headImg" column= "head_img" />
<result property= "phone" column= "phone" />
<!--
property 填写pojo类中集合类属性的名称
ofType 集合⾥里里⾯面的pojo对象
-->
<collection property= "videoOrderList" ofType= "VideoOrder" >
<!--配置主键,管理理order的唯⼀一标识-->
<id column= "order_id" property= "id" />
<result column= "user_id" property= "userId" />
<result column= "out_trade_no" property= "outTradeNo" />
<result column= "state" property= "state" />
<result column= "total_fee" property= "totalFee" />
<result column= "video_id" property= "videoId" />
<result column= "video_title" property= "videoTitle" />
<result column= "video_img" property= "videoImg" />
</collection>
</resultMap>
<select id= "queryUserOrder" resultMap= "UserOrderResultMap" >
select
u.id,
u.name,
u.head_img,
u.phone,
o.id order_id,
o.out_trade_no,
o.user_id,
o.state,
o.total_fee,
o.video_id,
o.video_title
from user u left join video_order o on u.id = o.user_id
</select>
|
代码
1
2
3
4
5
|
// resultmap association关联查询
VideoOrderMapper videoOrderMapper =sqlSession.getMapper(VideoOrderMapper. class );
//resultmap collection测试
List<User> userList = videoOrderMapper.queryUserOrder();
System.out.println(userList.toString());
|
Mybatis3.X ResultMap复杂对象查询总结
总结ResultMap的复杂对象查询
- association映射的是一个pojo类,处理一对一的关联关系。
- collection映射的一个集合列表,处理的是一对多的关联关系。
- 模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--column不做限制,可以为任意表的字段,而property须为type定义的pojo属性-->
<resultMap id=“唯一的标识”type=“映射的pojo对象">
<id column= "表的主键字段,或查询语句中的别名字段”jdbcrype=" 字段类型”property=“映射pojo对象的主键属性"/>
<result column= "表的一个字段”jdbcrype=" 字段类型”property=“映射到pojo对象的一个属性"/>
<association property= "pojo的一个对象属性”javarype=" pojo关联的pojo对象">
<id column= "关联pojo对象对应表的主键字段”jdbcrype=" 字段类型”property= "关联pojo对象的属性" />
<result column= "表的字段”jdbcrype=" 字段类型”property= "关联pojo对象的属性" />
</association>
<!--集合中的property 需要为oftype定义的pojo对象的属性-->
<collection property= "pojo的集合属性名称”ofType=" 集合中单个的pojo对象类型">
<id column= "集合中pojo对象对应在表的主键字段”jdbcrype=" 字段类型”property=“集合中pojo对象的主键属性”/>
<result column= "任意表的字段”jdbcrype=" 字段类型”property="集合中的pojo对象的属性”/>
</collection>
</resultMap>
|
到此这篇关于MyBatis3.X复杂Sql查询的文章就介绍到这了,更多相关MyBatis复杂Sql查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/ruan_luqingnian/article/details/115366923