mybatis 配置一对多 一对一 多对多

(一对多:一个用户对应多个账户   一对一: 一个账户对应一个用户  多对多: 多个用户可以对应多个角色)

1. 实体类

   1.1  多的一方

package com.zsw.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    //建立一对多关系映射
    private List<Account> accounts;
//    //建立多对多
    private List<Role> roles;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", accounts=" + accounts +
                ", roles=" + roles +
                '}';
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

 1.2  多的一方

package com.zsw.domain;

import java.io.Serializable;

/**
 * @author Devin
 * @version 1.0
 * @date 2019/7/5 20:26
 */
public class Account implements Serializable {
    private int id;
    private int uid;
    private double money;
    private User user;

    public User getUser() {
        return user;
    }

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

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                ", user=" + user +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
package com.zsw.domain;

import java.io.Serializable;
import java.util.List;

/**
 * @author Devin
 * @version 1.0
 * @date 2019/7/6 15:25
 */
public class Role implements Serializable {
        private int roleId;
        private String roleName;
        private String roleDesc;
        private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return "Role{" +
                "roleId=" + roleId +
                ", roleName='" + roleName + '\'' +
                ", roleDesc='" + roleDesc + '\'' +
                ", users=" + users +
                '}';
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleDesc() {
        return roleDesc;
    }

    public void setRoleDesc(String roleDesc) {
        this.roleDesc = roleDesc;
    }
}

 

2.映射文件配置

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zsw.dao.UserDao">
    <sql id="seleUserall">
         select * from user
    </sql>

    <resultMap id="UserMap" type="user">
        <id column="id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="address" property="address"></result>
        <result column="sex" property="sex"></result>
        <result property="birthday" column="birthday"></result>
        <collection property="accounts" ofType="account">
            <id property="id" column="aid"></id>
            <result  property="uid" column="uid"></result>
            <result  property="money" column="money"></result>
        </collection>
        <collection property="roles" ofType="role">
            <id property="roleId" column="rid"></id>
            <result property="roleName" column="ROLE_NAME"></result>
            <result property="roleDesc" column="ROLE_DESC"></result>
        </collection>
    </resultMap>
    <select id="findAll" resultMap="UserMap">
    select u.*,a.id as aid ,a.uid as uid ,a.money from user  u LEFT outer JOIN account a on u.id= a.uid
    </select>

    <select id="findAllAndRole" resultMap="UserMap">
         select u.* ,r.id rid,r.role_name, r.ROLE_DESC from user as u
            left OUTER join user_role ur on u.id = ur.uid
             left OUTER join role r on ur.rid = r.id
    </select>


</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsw.dao.AccountDao">
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="aid"></id>
        <result  property="uid" column="uid"></result>
        <result  property="money" column="money"></result>
        <!--配置一对一关系映射-->
        <association property="user" column="uid" javaType="user">
            <id property="id" column="id"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result property="birthday" column="birthday"></result>
        </association>
    </resultMap>
    
    <select id="findAll" resultType="account">
        select * from  account as a user as u where a.uid = u.id
    </select>
    <select id="findAccount" resultType="accountUser">
       select u.*,a.id as aid,a.uid as uid,a.money from user as u, account as a where a.uid=u.id
    </select>

    <select id="findAllAccountAndUser" resultMap="accountUserMap">
      select u.*,a.id as aid,a.uid as uid,a.money from user as u, account as a where a.uid=u.id
    </select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsw.dao.RoleDao">
    <resultMap id="roleMap" type="role">
        <id property="roleId" column="id"></id>
        <result property="roleName" column="ROLE_NAME"></result>
        <result property="roleDesc" column="ROLE_DESC"></result>
        <collection property="users" column="uid" ofType="user">
            <id property="id" column="uid"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result property="birthday" column="birthday"></result>
        </collection>
    </resultMap>
    <select id="findAll" resultMap="roleMap">
      select r.* , u.id uid,u.username, u.address,u.sex,u.birthday from role r
        left join user_role ur on r.id = ur.rid
           left OUTER join user u on ur.uid = u.id
</select>
</mapper>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值