Mybatis3.X的resultMap和一对多,多对多关系实例

目前正在学习Java,想把一些学习过程中的点点滴滴记录下来,一个原因是方便自己以后使用,另一个原因是巩固一下自己的学习成果和大家分享!!

下面我把Mybatis3.X中的复杂Sql查询给大家进行分享!

1 MyBatis3.X resultMap 你知道多少
          简介:讲解 Mybatis resultMap 讲解
                     Mybatis的SQL语句返回结果有两种:

                     resultType: 
查询出的字段在相应的 pojo 中必须有和它相同的字段对应,或者基本数据类型, 适合简单查询
                   
                     resultMap:需要⾃定义字段,或者多表查询,⼀对多等关系,比resultType更强大,适合复杂查询
 
<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>
2 ResultMap 复杂对象⼀对⼀查询结果映射之 association
          简介:讲解Mybatis 复杂对象映射配置 ResultMap association
                     association: 映射到POJO 的某个复杂类型属性,⽐如订单 order 对象⾥⾯包含 user 对象
 
<resultMap id="VideoOrderResultMap" type="VideoOrder">
 <id column="id" property="id"/>
 <result column="user_id" property="userId"/>
 <result column="out_trade_no" property="outTradeNo"/>
 <result column="create_time" property="createTime"/>
 <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"/>
 <!--
 association 配置属性⼀对⼀
 property 对应videoOrder⾥⾯的user属性名
 javaType 这个属性的类型
 -->
 <association property="user" javaType="User">
 <id property="id" column="user_id"/>
 <result property="name" column="name"/>
 <result property="headImg" column="head_img"/>
 <result property="createTime" column="create_time"/>
 <result property="phone" column="phone"/>
 </association>
 </resultMap>
 <!--⼀对⼀管理查询订单, 订单内部包含⽤户属性-->
 <select id="queryVideoOrderList" resultMap="VideoOrderResultMap">
 select
 o.id id,
 o.user_id ,
 o.out_trade_no,
 o.create_time,
 o.state,
 o.total_fee,
 o.video_id,
 o.video_title,
 o.video_img,
 u.name,
 u.head_img,
 u.create_time,
 u.phone
 from video_order o left join user u on o.user_id = u.id
 </select>

后台代码:

// resultmap association关联查询
VideoOrderMapper videoOrderMapper = sqlSession.getMapper(VideoOrderMapper.class);
List<VideoOrder> videoOrderList = videoOrderMapper.queryVideoOrderList();
System.out.println(videoOrderList.toString());
3 ResultMap 复杂对象⼀对多查询结果映射之 collection
          简介:讲解Mybatis 复杂对象⼀对多映射配置ResultMap collection
                     collection: ⼀对多查询结果查询映射,⽐如user 有多个订单
<resultMap id="UserOrderResultMap" type="User">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="headImg" column="head_img"/>
 <result property="createTime" column="create_time"/>
 <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="create_time" property="createTime"/>
 <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.create_time,
 u.phone,
 o.id order_id,
 o.out_trade_no,
 o.user_id,
 o.create_time,
 o.state,
 o.total_fee,
 o.video_id,
 o.video_title,
 o.video_img
 from user u left join video_order o on u.id = o.user_id
 </select>

后台代码:

// resultmap association关联查询
VideoOrderMapper videoOrderMapper = sqlSession.getMapper(VideoOrderMapper.class);
//resultmap collection测试
List<User> userList = videoOrderMapper.queryUserOrder();
System.out.println(userList.toString());

总结:

4 Mybatis3.X ResultMap 复杂对象查询总结
          简介:总结ResultMap 的复杂对象查询
                     association 映射的是⼀个pojo 类,处理⼀对⼀的关联关系。
                     collection 映射的⼀个集合列表,处理的是⼀对多的关联关系。
<!-- column不做限制,可以为任意表的字段,⽽property须为type 定义的pojo属性-->

<resultMap id="唯⼀的标识" type="映射的pojo对象">

 <id column="表的主键字段,或查询语句中的别名字段" jdbcType="字段类型" property="映射
pojo对象的主键属性" />

    <result column="表的⼀个字段" jdbcType="字段类型" property="映射到pojo对象的⼀个属
性"/>

<!-- association 集合中的property 需要为oftype定义的pojo对象的属性-->

 <association property="pojo的⼀个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo
对象的属性"/>
    <result column="表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
 </association>

 <!-- collection 集合中的property 需要为oftype定义的pojo对象的属性-->

 <collection property="pojo的集合属性名称" ofType="集合中单个的pojo对象类型">
   <id column="集合中pojo对象对应在表的主键字段" jdbcType="字段类型" property="集合
中pojo对象的主键属性" />
   <result column="任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属
性" /> 
 </collection>

</resultMap>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值