一对一(查询一个订单,同时查询订单对应的用户信息)
实体关系
~~~
public class Order {
private Integer orderid;
private Timestamp ordertime;
private Userinfo userinfo;
private Double totalprice;
private String status;
}
~~~
~~~
public class Userinfo {
private Integer userid;
private String username;
private String sex;
private double sal;
private Date birthday;
private Timestamp stime;
}
~~~
XML映射配置
~~~
<select id="getOrderById" parameterType="int" resultMap="_order">
select o.*, u.username
from sorder o, userinfo u
where o.userid = u.userid
and o.orderid = #{orderid}
</select>
<!-- id是select result_map的值 -->
<!-- type是select查询的返回值类型 -->
<resultMap type="Order" id="_order">
<id property="orderid" column="orderid"></id>
<result property="ordertime" column="ordertime"/>
<result property="totalprice" column="totalprice"/>
<result property="status" column="status"/>
<association property="userinfo" javaType="Userinfo">
<id property="userid" column="userid"></id>
<result property="username" column="username"/>
</association>
</resultMap>
~~~
测试类
~~~
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
Order o = orderMapper.getOrderById(1);
System.out.println(o.getOrdertime()+"\t"+o.getTotalprice()+"\t"+o.getUserinfo().getUsername());
~~~