这篇文章将为大家详细讲解有关MyBatis3.X复杂Sql查询相关知识有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

MyBatis3.X复杂Sql查询MyBatis3.X的resultMap

1.Mybatis的sql语句返回的结果有两种

resultType

查询出的字段在相应的pojo中必须有和它相同的字段对应,或者基本数据类型

适合简单查询

resultMap

需要自定义字段,或者多表查询,一对多等关系,比resultType更强大
适合复杂查询

<resultMapid="VideoResultMap"type="Video"><!--id指定查询列列的唯⼀一标示column数据库字段的名称propertypojo类的名称--><idcolumn="id"property="id"jdbcType="INTEGER"/><resultcolumn="video_tile"property="title"jdbcType="VARCHAR"/><resultcolumn="summary"property="summary"jdbcType="VARCHAR"/><resultcolumn="cover_img"property="coverImg"jdbcType="VARCHAR"/></resultMap><selectid="selectBaseFieldByIdWithResultMap"resultMap="VideoResultMap">selectid,titleasvideo_tile,summary,cover_imgfromvideowhereid=#{video_id}</select>ResultMap复杂对象一对一查询结果映射之association

association:映射到POJO的某个复杂类型属性,比如订单order对象里面包含user对象

<!--名称空间,需要保存全局唯一,最好是和dao层的Java接口一致可以映射sql语句到对应的方法名参数和返回值mybatis是使用接口动态代理--><mappernamespace="net.xiaotiancai.online_class.dao.VideoOrderMapper"><resultMapid="VideoOrderResultMap"type="VideoOrder"><idcolumn="id"property="id"></id><resultcolumn="user_id"property="userId"></result><resultcolumn="out_trade_no"property="outTradeNo"></result><resultcolumn="state"property="state"></result><resultcolumn="total_fee"property="totalFee"></result><resultcolumn="video_id"property="videoId"></result><resultcolumn="video_title"property="videoTitle"></result><!--配置属性一对一property对应videoOrder里面的UserjavaType对应这个属性的类型--><associationproperty="user"javaType="User"><idcolumn="user_id"property="id"></id><resultcolumn="name"property="name"></result><resultcolumn="head_img"property="headImg"></result><resultcolumn="phone"property="phone"></result></association></resultMap><!--一对一订单查询,订单内部包含用户属性--><selectid="queryVideoOrderList"resultMap="VideoOrderResultMap">selecto.idid,o.user_id,o.out_trade_no,o.state,o.total_fee,o.video_id,o.video_title,u.name,u.head_img,u.phonefromvideo_orderoleftjoinuseruono.user_id=u.id</select></mapper>

代码

//resultmapassociation关联查询VideoOrderMappervideoOrderMapper=sqlSession.getMapper(VideoOrderMapper.class);List<VideoOrder>videoOrderList=videoOrderMapper.queryVideoOrderList();System.out.println(videoOrderList.toString());ResultMap复杂对象一对多查询结果映射之collection

collection: 一对多查询结果查询映射,比如user有多个订单

<resultMapid="UserOrderResultMap"type="User"><idproperty="id"column="id"/><resultproperty="name"column="name"/><resultproperty="headImg"column="head_img"/><resultproperty="phone"column="phone"/><!--property填写pojo类中集合类属性的名称ofType集合⾥里里⾯面的pojo对象--><collectionproperty="videoOrderList"ofType="VideoOrder"><!--配置主键,管理理order的唯⼀一标识--><idcolumn="order_id"property="id"/><resultcolumn="user_id"property="userId"/><resultcolumn="out_trade_no"property="outTradeNo"/><resultcolumn="state"property="state"/><resultcolumn="total_fee"property="totalFee"/><resultcolumn="video_id"property="videoId"/><resultcolumn="video_title"property="videoTitle"/><resultcolumn="video_img"property="videoImg"/></collection></resultMap><selectid="queryUserOrder"resultMap="UserOrderResultMap">selectu.id,u.name,u.head_img,u.phone,o.idorder_id,o.out_trade_no,o.user_id,o.state,o.total_fee,o.video_id,o.video_titlefromuseruleftjoinvideo_orderoonu.id=o.user_id</select>

代码

//resultmapassociation关联查询VideoOrderMappervideoOrderMapper=sqlSession.getMapper(VideoOrderMapper.class);//resultmapcollection测试List<User>userList=videoOrderMapper.queryUserOrder();System.out.println(userList.toString());Mybatis3.X ResultMap复杂对象查询总结总结ResultMap的复杂对象查询

association映射的是一个pojo类,处理一对一的关联关系。

collection映射的一个集合列表,处理的是一对多的关联关系。

模板

<!--column不做限制,可以为任意表的字段,而property须为type定义的pojo属性--><resultMapid=“唯一的标识”type=“映射的pojo对象"><idcolumn="表的主键字段,或查询语句中的别名字段”jdbcrype="字段类型”property=“映射pojo对象的主键属性"/><resultcolumn="表的一个字段”jdbcrype="字段类型”property=“映射到pojo对象的一个属性"/><associationproperty="pojo的一个对象属性”javarype="pojo关联的pojo对象"><idcolumn="关联pojo对象对应表的主键字段”jdbcrype="字段类型”property="关联pojo对象的属性"/><resultcolumn="表的字段”jdbcrype="字段类型”property="关联pojo对象的属性"/></association><!--集合中的property需要为oftype定义的pojo对象的属性--><collectionproperty="pojo的集合属性名称”ofType="集合中单个的pojo对象类型"><idcolumn="集合中pojo对象对应在表的主键字段”jdbcrype="字段类型”property=“集合中pojo对象的主键属性”/><resultcolumn="任意表的字段”jdbcrype="字段类型”property="集合中的pojo对象的属性”/></collection></resultMap>

关于“MyBatis3.X复杂Sql查询相关知识有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。