mybatis第四天

南笙酒味 提交于 2020-02-07 11:29:11

1.1.ResultMap和ResultType

1.1.1.ResultType

resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。 ,如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中 。

1234
<!-- resultType实现--><select id="queryOrderUserByOrderNumber" resultType="com.lxy.mybatis.pojo.OrderUser">    select * from tb_order o left join tb_user u on o.user_id=u.id where o.order_number = #{number}</select>

1.1.2.ResultMap

resultMap则是对外部ResultMap的引用 ,将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中

1234567891011121314151617
<!-- resultmap实现 -->       <resultMap id="OrderUserResultMap" type="com.lxy.mybatis.pojo.Order" autoMapping="true">           <id property="id" column="id" />           <!-- association :配置一对一属性 -->           <!-- property:order里面的User属性名 -->           <!-- javaType:属性类型 -->           <association property="user" javaType="com.lxy.mybatis.pojo.User">               <!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->               <id property="id" column="user_id" />               <result property="userName" column="user_name"/>           </association>       </resultMap>       <select id="queryOrderWithUserByOrderNumber" resultMap="OrderUserResultMap" >           select * from tb_order o left join tb_user u on o.user_id=u.id where o.order_number = #{number}       </select>

因为在:

12345
<association property="user" javaType="com.lxy.mybatis.pojo.User">                <!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->                <id property="id" column="user_id" />                <result property="userName" column="user_name"/>            </association>

中只为userName设定了关联,所以打印:

12345
@Testpublic void queryOrderWithUserByOrderNumber() throws Exception {    Order order = orderMapper.queryOrderWithUserByOrderNumber("123456");    System.out.println(order.getUser());}

的时候只会打印出id 和 userName两个属性,其他的为null。

resultMap包含的元素:

1234567891011121314
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--><resultMap id="唯一的标识" type="映射的pojo对象">  <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />  <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>  <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>  </association>  <!-- 集合中的property须为oftype定义的pojo对象的属性-->  <collection property="pojo的集合属性" ofType="集合中的pojo对象">    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />    </collection></resultMap>

1.2.一对多查询

新建OrderDetail类:

12345678
package com.lxy.mybatis.pojo;public class OrderDetail {    private int id;    private  int orderId;    private double totalPrice;    private int status;}

修改Order类:

1234567891011121314151617181920
package com.lxy.mybatis.pojo;import java.util.Date;import java.util.List;public class Order {    private int id;    private long userId;    private String orderNumber;    private Date created;    private Date updated;    private  User user;    private List<OrderDetail> detailList;    public User getUser(){        return user;   }   public  List<OrderDetail>  getDetailList(){        return detailList;   }}

在OrderMapper.xml中增加:

123456789101112131415
<resultMap id="OrderUserDetailResultMap" type="com.lxy.mybatis.pojo.Order" autoMapping="true">    <id property="id" column="id"/>    <association property="user" javaType = "com.lxy.mybatis.pojo.User">        <id property="id" column="user_id" />        <result property="userName" column="user_name"/>    </association>    <collection property="detailList"  javaType="List" ofType="com.lxy.mybatis.pojo.OrderDetail" autoMapping="true">        <id property="id" column="id"/>    </collection></resultMap><select id="queryOrderWithUserAndDetailByOrderNumber" resultMap="OrderUserDetailResultMap">    select * from  tb_order o left join tb_user u on o.user_id=u.id left join tb_orderdetail od on o.id=od.order_id    where  o.order_number = #{number}</select>

其中collection属性用于集合的情况。

123
<collection property="detailList"  javaType="List" ofType="com.lxy.mybatis.pojo.OrderDetail" autoMapping="true">        <id property="id" column="id"/>    </collection>

测试类:

123456
@Testpublic void queryOrderWithUserAndDetailByOrderNumber(){    Order order = orderMapper.queryOrderWithUserAndDetailByOrderNumber("123456");    System.out.println(order.getUser());    System.out.println(order.getDetailList());}

1.3.多对多查询

修改OrderDetail类 :

123456789
package com.lxy.mybatis.pojo;public class OrderDetail {    private int id;    private  int orderId;    private double totalPrice;    private int status;    private  Item item;}

新增item类:

12345678
package com.lxy.mybatis.pojo;public class Item {    private Integer id;    private String itemName;    private Float itemPrice;    private String itemDetail;}

测试类:

1234567
@Testpublic void queryOrderWithUserAndDetailItemByOrderNumber() throws Exception {    Order order = orderMapper.queryOrderWithUserAndDetailItemByOrderNumber("123456");    System.out.println(order);    System.out.println(order.getUser());    System.out.println(order.getDetailList());}

mapper:

1234567891011121314151617181920
<resultMap id="OrderUserDetailItemResultMap" type="com.lxy.mybatis.pojo.Order" autoMapping="true">    <id column="id" property="id"/>    <association property="user" javaType="com.lxy.mybatis.pojo.User" autoMapping="true">        <id column="user_id" property="id"/>    </association>    <collection property="detailList" javaType="List" ofType="com.lxy.mybatis.pojo.OrderDetail" autoMapping="true">        <id column="detail_id" property="id"/>        <association property="item" javaType="com.lxy.mybatis.pojo.Item" autoMapping="true">            <id column="item_id" property="id"/>        </association>    </collection></resultMap><select id="queryOrderWithUserAndDetailItemByOrderNumber" resultMap="OrderUserDetailItemResultMap">    select * ,od.id as detail_id from tb_order o    left join tb_user u on o.user_id=u.id    left join tb_orderdetail od on o.id=od.order_id    left join tb_item i on od.item_id=i.id    where o.order_number = #{number}</select>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!