查询某个订单的订单内容和用户信息
实现一个订单类的子类 添加订单表中没有的User属性
实现子类 继承订单类
package edu.ccit.cn.domain;
/**
* @program: Mybatis03_tables
* @description
* @author: LIANG
* @create: 2021-06-01 15:34
**/
public class OrdersExpand extends Orders {
private String username;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "OrdersExpand{" +
"username='" + username + '\'' +
", address='" + address + '\'' +
'}'+super.toString();
}
}
<select id="findOrdersById" parameterType="int" resultType="edu.ccit.cn.domain.OrdersExpand">
SELECT
o.*,u.username,u.address
FROM
orders o,user u
where
o.user_id=u.id
and o.id=#{id}
</select>
直接在订单类中添加User属性
sql片段
<sql id="select_orders">
orders .*
</sql>
<sql id="select_user">
user.username,user.address
</sql>
定义resultMap
assocation用于嵌套对象 column为列明 property为属性名
<resultMap id="Orders_user" type="Orders">
<result column="id" property="id"></result>
<result column="number" property="number"></result>
<result column="createtime" property="createtime"></result>
<!--关联内部对象-->
<association property="user" javaType="User">
<result column="username" property="username"></result>
<result column="address" property="address"></result>
</association>
</resultMap>
<select id="findOrdersById02" parameterType="int" resultMap="Orders_user">
select
<include refid="select_orders"></include>,
<include refid="select_user"></include>
from
orders,user
where
orders.user_id=user.id and orders.id=3
</select>