MyBatis 一对一查询方式二

☆樱花仙子☆ 提交于 2020-01-11 04:01:42

一、创建数据库 SQL

CREATE TABLE user (id INT, username VARCHAR(20), phone VARCHAR(20));
CREATE TABLE orderinfo (id INT, uid INT, goods VARCHAR(20), info VARCHAR(50));

二、创建与数据库表对应的实体类

public class User implements Serializable {

    private int id;
    private String username;
    private String phone;
	...
}
public class OrderInfo implements Serializable {

    private int id;
    private int uid;
    private String goods;
    private String info;
    private User user;
	...
}

三、持久层接口

List<OrderInfo> findAll();

四、持久层映射文件

<resultMap id="orderInfoMap" type="chu.yi.bo.domain.OrderInfo">
    <id column="id" property="id"/>
    <result column="uid" property="uid"/>
    <result column="goods" property="goods"/>
    <result column="info" property="info"/>
    <association property="user" javaType="chu.yi.bo.domain.User">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="phone" property="phone"/>
    </association>
</resultMap>

<select id="findAll" resultMap="orderInfoMap">
    select o.*,u.* from orderinfo o,user u where o.uid = u.id;
</select>

五、测试

List<OrderInfo> orderInfoList = orderInfoDao.findAll();
for(OrderInfo orderInfo : orderInfoList) {
    System.out.println(orderInfo);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!