Mybatis的多表查询

一对多:

这里一个用户有多个订单

class User {
    private int id;
    private String username;
    private String password;

    private List<Order> orderList;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", orderList=" + orderList +
                '}';
    }
}






class Order {
    private int oid;
    private int total;
    private User user;

    public int getOid() {
        return oid;
    }

    public void setOid(int oid) {
        this.oid = oid;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Order{" +
                "oid=" + oid +
                ", total=" + total +
                ", user=" + user +
                '}';
    }
}
  <resultMap id="orderMap" type="user"><!--配置映射-->
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>

        <collection property="orderList" ofType="order">
            <result column="oid" property="oid"/>
            <result column="total" property="total"/>
        </collection>
    </resultMap>



    <select id="findAll"  resultMap="orderMap">
        select * from  user,orders where user.id = orders.uid
    </select>

多对多查询(与一对多类似):

一个用户有多个角色,一个角色对应对各用户

 <resultMap id="userRoleMap" type="user">
        <id column="userId" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>

        <collection property="roleList" ofType="role">
            <result column="roleId" property="id"/>
            <result column="roleName" property="roleName"/>
            <result column="roleDesc" property="roleDesc"/>
        </collection>
    </resultMap>
    <select id="findUserAndRole" resultMap="userRoleMap">
        select * from sys_role,sys_user_role,sys_user where sys_role.id = roleId and userId = sys_user.id
    </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值