【mybatis】一对一、一对多和多对多实例 笔记

数据库表结构、数据

  • account表
    在这里插入图片描述
    在这里插入图片描述

  • user表
    在这里插入图片描述
    在这里插入图片描述

一对一 association、javaType

  • 第一种方式
<select id="findAllAccountUser" resultType="com.wei.pojo.AccountUser">
      select a.*,u.username,u.address from account a ,user u where a.uid = u.id
</select>
//继承Account,并且返回父类的tostring+自己需要的2个属性
public class AccountUser extends Account {
    private String username;
    private String address;
    @Override
    public String toString() {
        return super.toString()+"-"+"AccountUser{" +
                "username='" + username + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
    @Test
    public void fun9(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        AccountMapper mapper = sqlsession.getMapper(AccountMapper.class);
        List<AccountUser> accountUsers = mapper.findAllAccountUser();
        System.out.println(accountUsers);

    }
  • 第二种方式

<resultMap id="accountUserMap" type="account">
    <id property="id" column="aid" />
    <result property="uid" column="uid" />
    <result property="money" column="money" />
    <!-- association 一对一,使用javaType指定类型-->
    <association property="user" column="uid" javaType="user" >
        <result property="id" column="uid" />
        <result property="username" column="username" />
        <result property="address" column="address" />
        <result property="sex" column="sex" />
        <result property="birthday" column="birthday" />
    </association>
</resultMap>

<select id="findAll" resultMap="accountUserMap">
      select a.id aid,u.id uid,a.money,u.*  from account a,user u where a.uid = u.id
</select>
public class Account {
    private Integer id;
    private Integer uid;
    private Double money;
    private User user;
    
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                ", user=" + user +
                '}';
    }
}
//测试
    @Test
    public void fun10(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        AccountMapper mapper = sqlsession.getMapper(AccountMapper.class);
        List<Account> account = mapper.findAll();
        System.out.println(account);
    }
    
  • 运行结果如下
    在这里插入图片描述

一对多 collection 、ofType

<resultMap id="userAccountMap" type="user">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <result property="birthday" column="birthday" />
    <result property="sex" column="sex" />
    <result property="address" column="address" />
    <!-- collection 多对多,使用ofType指定集合中的类型-->
    <collection property="accounts" ofType="Account" >
        <id property="id" column="aid" />
        <result property="uid" column="uid" />
        <result property="money" column="money" />
    </collection>
</resultMap>

<select id="findAllUserAccount" resultMap="userAccountMap">
      select *,a.id aid from user u left outer join  account a on a.uid = u.id
</select>
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    private List<Account> accounts;
    
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\''+this.getAccounts();
    }
}
//测试
    @Test
    public void fun11(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        List<User> all = mapper.findAllUserAccount();
        for (User user : all) {
            System.out.println(user);
        }
    }
    
  • 运行结果如下
    在这里插入图片描述
    能够识别出同一用户的多个账户,没有缺少账户、分开输出。
    在这里插入图片描述

多对多

Role 查询出多个 User

  • RoleMapper.xml
<?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.wei.mapper.RoleMapper">

    <resultMap id="roleUserMap" type="role">
        <id property="id" column="id" />
        <result property="roleName" column="role_name" />
        <result property="roleDesc" column="role_desc" />
        <collection property="users" ofType="user2" >
            <id property="id" column="uid" />
            <result property="username" column="username" />
            <result property="birthday" column="birthday" />
            <result property="sex" column="sex" />
            <result property="address" column="address" />
        </collection>
    </resultMap>
    <select id="findAll" resultMap="roleUserMap">
      select * from role r
       left outer join user_role ur on r.id = ur.rid
        left outer join user u on u.id = ur.uid
     </select>
</mapper>
	//实体类
	@Data
	public class Role implements Serializable {
	    private Integer id;
	    private String roleName;
	    private String roleDesc;
	    private List<User> users;
	}
	//测试
    @Test
    public void fun2(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        RoleMapper mapper = sqlsession.getMapper(RoleMapper.class);
        List<Role> all = mapper.findAll();
        System.out.println(all);
        sqlsession.close();
    }
  • 运行结果
    在这里插入图片描述
    在这里插入图片描述

User 查询出多个 Role

  • UserMapper.xml
<?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.wei.mapper.UserMapper">

    <resultMap id="userRoleMap" type="user2">
        <id property="id" column="id" />
        <result property="username" column="username" />
        <result property="birthday" column="birthday" />
        <result property="sex" column="sex" />
        <result property="address" column="address" />
        <collection property="roles" ofType="role"  >
            <id property="id" column="rid" />
            <result property="roleName" column="role_name" />
            <result property="roleDesc" column="role_desc" />
        </collection>
    </resultMap>

    <select id="findAll" resultMap="userRoleMap">
      select * from user u
       left outer join user_role ur on u.id = ur.uid
        left outer join role r on r.id = ur.rid
     </select>

</mapper>
	//实体类
	@Data
	@Alias("user2") //此处取别名是为了避免我这里的别名冲突
	public class User implements Serializable {
	    private Integer id;
	    private String username;
	    private Date birthday;
	    private String sex;
	    private String address;
	    private List<Role> roles;
	}
	//测试
    @Test
    public void fun1(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        System.out.println(all);
        sqlsession.close();
    }
  • 运行结果
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nickkkkkkkkk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值