• Mybatis——关联映射查询


    一、一对一映射:

    主Model:

     1 package com.neuedu.model;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * orders订单表
     7  */
     8 public class Order {
     9     
    10     private int id;
    11     
    12     private int userid;
    13     
    14     private String orderId;
    15     
    16     private Date createTime;
    17     
    18     private String note;
    19     
    20     //一对一关系
    21     private User user;
    22 
    23     public int getId() {
    24         return id;
    25     }
    26 
    27     public void setId(int id) {
    28         this.id = id;
    29     }
    30 
    31     public int getUserid() {
    32         return userid;
    33     }
    34 
    35     public void setUserid(int userid) {
    36         this.userid = userid;
    37     }
    38 
    39     public String getOrderId() {
    40         return orderId;
    41     }
    42 
    43     public void setOrderId(String orderId) {
    44         this.orderId = orderId;
    45     }
    46 
    47     public Date getCreateTime() {
    48         return createTime;
    49     }
    50 
    51     public void setCreateTime(Date createTime) {
    52         this.createTime = createTime;
    53     }
    54 
    55     public String getNote() {
    56         return note;
    57     }
    58 
    59     public void setNote(String note) {
    60         this.note = note;
    61     }
    62     
    63     public User getUser() {
    64         return user;
    65     }
    66 
    67     public void setUser(User user) {
    68         this.user = user;
    69     }
    70 
    71     @Override
    72     public String toString() {
    73         return "Order [id=" + id + ", userid=" + userid + ", orderId=" + orderId + ", createTime=" + createTime
    74                 + ", note=" + note + ", user=" + user + "]";
    75     }
    76     
    77 }

    方式一:通过自定义类实现

    (1)Model包中,新建一个类使之继承主查询表Model,添加新对应表Model中的属性

     1 package com.neuedu.model;
     2 /**
     3  * 自定义
     4  */
     5 public class OrderCustomer extends Order{
     6 
     7     private int userid;
     8     
     9     private String username;
    10     
    11     private String address;
    12 
    13     public int getUserid() {
    14         return userid;
    15     }
    16 
    17     public void setUserid(int userid) {
    18         this.userid = userid;
    19     }
    20 
    21     public String getUsername() {
    22         return username;
    23     }
    24 
    25     public void setUsername(String username) {
    26         this.username = username;
    27     }
    28 
    29     public String getAddress() {
    30         return address;
    31     }
    32 
    33     public void setAddress(String address) {
    34         this.address = address;
    35     }
    36 
    37     @Override
    38     public String toString() {
    39         return "OrderCustomer [userid=" + userid + ", username=" + username + ", address=" + address + ", getId()="
    40                 + getId() + ", getOrderId()=" + getOrderId() + ", getCreateTime()=" + getCreateTime() + ", getNote()="
    41                 + getNote() + "]";
    42     }
    43     
    44 }

    (2)mapper.xml中,通过ResultType指向新建的类,进行注入

    1 <!-- 一对一查询,需多自定义一个类OrderCustomer -->
    2     <select id="selectOrder" resultType="com.neuedu.model.OrderCustomer">
    3         select 
    4             o.id, o.userid, o.orderid, o.createtime, o.note,
    5             t.id userid,t.username,t.birthday,t.address 
    6         from orders o,t_user t 
    7         where o.userid = t.id
    8     </select>

    方式二:通过ResultMap方式注入

    (1)在mapper.xml中建立ResultMap映射,运用association标签添加一对一关系映射

     1 <!-- 一对一查询,返回resultMap -->
     2     <resultMap id="selectOrderResultMap" type="com.neuedu.model.Order" >
     3         <id property="id" column="id"/>    <!-- property pojo对象的属性名称    column查询语句的列名称-->
     4         <result property="userid" column="userid"/>
     5         <result property="orderId" column="orderid"/>
     6         <result property="createTime" column="createtime"/>
     7         <result property="note" column="note"/>
     8         <!-- 一对一 关系-->
     9         <association property="user" javaType="com.neuedu.model.User">
    10             <id property="id" column="userid"/>
    11             <result property="username" column="username"/>
    12             <result property="birthday" column="birthday"/>
    13             <result property="address" column="address"/>
    14         </association>
    15     </resultMap>

    (2)在mapper.xml中编写查询语句,使ResultMap属性指向前面定义的ResultMap的id

    1 <select id="selectOrderByResultMap" resultMap="selectOrderResultMap">
    2         select 
    3             o.id, o.userid, o.orderid, o.createtime, o.note,
    4             t.id userid,t.username,t.birthday,t.address 
    5         from orders o,t_user t 
    6         where o.userid = t.id
    7     </select>

    二、一对多查询:

    主Model中添加属性

    1 //一对多关系
    2     private List<OrderDetail> orderDetailList;

    (一个订单对应多个商品明细)

    (1)通过属性collection添加一对多映射

    1 <!-- 一对多关系:collection -->
    2         <collection property="orderDetailList" ofType="com.neuedu.model.OrderDetail">
    3             <id property="id" column="odid"/>
    4             <result property="ordersid" column="ordersid"/>
    5             <result property="itemsid" column="itemsid"/>
    6             <result property="itemsnum" column="itemsnum"/>
    7         </collection>

    注意:resultMap标签中的extends可指定继承resultMap的标签,可使用其中的所有映射关系,并添加自己独自的

  • 相关阅读:
    Github for Windows使用介绍
    Activity生命周期
    Java日期LocalDate使用
    一、安装Windows 2012域控(For SQLServer 2014 AlwaysOn)
    .NET(C#):分析IL中的if-else,while和for语句并用Emit实现
    sqlserver中几种典型的等待
    ServiceStack.Redis常用操作
    ServiceStack.Redis 之 IRedisTypedClient<第四篇>
    ServiceStack.Redis之IRedisClient<第三篇>
    Redis常用命令速查 <第二篇>
  • 原文地址:https://www.cnblogs.com/ccw95/p/6183200.html
Copyright © 2020-2023  润新知