MyBatis的多对多映射(十)上

简介: MyBatis的多对多映射(十)

一. Mybatis的多对多关联映射


多对多的关系在数据库中是很常见的,但是在业务代码中常常处理成 双向的一对多的关联。 可以与Hibernate的多对多对照来看:

Hibernte的多对多映射(十二)


创建User表:

20190708165348821.png

创建Role 角色表:


20190711085344184.png


创建User_role 表


20190711085353136.png


相应的实体类:

User.java


package com.yjl.pojo;
import java.util.List;
/**
 @author:yuejl
 @date: 2019年6月15日 上午11:11:02
 @Description Mybatis 使用的基本类 User
*/
public class User {
  /**
   * @param id id编号,自增
   * @param name 姓名
   * @param age 年龄
   * @param sex 性别
   * @param description 描述
   */
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  private String description;
  //引入角色的多个对象集合。。
  private List<Role> roles;
  public User(){
  }
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
  public List<Role> getRoles() {
    return roles;
  }
  public void setRoles(List<Role> roles) {
    this.roles = roles;
  }
  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", description=" + description
        + "]";
  }
}


Role.java


package com.yjl.pojo;
import java.util.List;
/**
 @author:两个蝴蝶飞
 @date: 2019年3月2日 下午6:18:51
 @Description 角色组
*/
public class Role {
  /**
   * @param id 角色编号
   * @param name 用户的名称
   */
  private Integer id;
  private String name;
  public Role() {
  }
  public Role(String name) {
    this.name = name;
  }
  /**
   * @param users 用户   是一对多的关系
   */
  private List<User> users;
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public List<User> getUsers() {
    return users;
  }
  public void setUsers(List<User> users) {
    this.users = users;
  }
  @Override
  public String toString() {
    return "Role [id=" + id + ", name=" + name + "]";
  }
}


二. 角色到员工的一对多查询(select 形式)


RoleMapper.java 中接口:


public Role findByIdWithSelect(int id);


UserMapper.java 中接口:


public List<User> findUserByRoleId(@Param(value="roleId") int roleId);


RoleMapper.xml sql语句:


<!--角色查询员工的一对多-->
<resultMap type="role" id="roleResultMapWithSelect">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="users" ofType="user" column="id"
    select="com.yjl.mapper.UserMapper.findUserByRoleId"></collection>
  </resultMap>
<select id="findByIdWithSelect" parameterType="int" resultMap="roleResultMapWithSelect">
    select * from role t where t.id=#{id}
</select>


UserMapper.xml 中sql语句


<resultMap type="user" id="userResultMap">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="description" column="description"/>
  </resultMap>
<select id="findUserByRoleId" parameterType="int" resultMap="userResultMap">
    select u.* from user_role t,user u where t.userId=u.id and t.roleId=#{roleId}
  </select>


测试方法:


@Test
  public void findByIdWithSelectTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
    //具有超级管理员角色的用户
    Role role=roleMapper.findByIdWithSelect(1);
    System.out.println(role);
    List<User> userList=role.getUsers();
    userList.forEach(n ->System.out.println(n));
  }


20190711085413883.png

相关文章
|
13小时前
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
15 1
|
13小时前
|
SQL Java 数据库连接
|
13小时前
|
Java 数据库连接 Maven
使用mybatis插件generator生成实体类,dao层和mapper映射
使用mybatis插件generator生成实体类,dao层和mapper映射
64 0
|
13小时前
|
XML Java 数据库连接
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
91 0
|
13小时前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
31 4
|
13小时前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
13小时前
|
XML Java 数据库连接
java对象有集合mybatis如何映射
java对象有集合mybatis如何映射
21 4
|
13小时前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
17 1
|
13小时前
|
Java 数据库连接 mybatis
mybatis简单案例源码详细【注释全面】——Dao层映射文件(UserMapper.xml)【重要】
mybatis简单案例源码详细【注释全面】——Dao层映射文件(UserMapper.xml)【重要】
11 0
|
13小时前
|
XML Java 数据库连接
【Mybatis】XML映射文件
【Mybatis】XML映射文件
29 0