mybatis 一对一association和一对多colleciton 一对多的使用

简介: mybatis 一对一association和一对多colleciton 一对多的使用

Mybatis标签association一对一的使用

  • 一、association
  • 二、使用方法
  • 1. 方法一: 嵌套结果映射
  • 2. 方法二: 嵌套select 查询
  • 三、colleciton 一对多

一、association

Mybatis的 association是一对一的使用的, 在 resultMap 标签内使用

当一个Bean中有 一个Object属性需要关联查询出来的使用就用association标签

如下

查询用户结果 需要关联出 角色


用户

@Data
public class User {
    private Integer id;
    private String name;
    private Role role;
}


角色

@Data
public class Role {
    private Integer id;
    private Integer userId;
    private String name;
    private String type;
}


sql

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户id',
  `name` varchar(255) DEFAULT NULL COMMENT '角色名称',
  `type` varchar(255) DEFAULT NULL COMMENT '角色类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;


二、使用方法

1. 方法一: 嵌套结果映射

    <!-- 定义resultMap -->
    <resultMap id="UserResultMap" type="User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <association ofType="role" property="role">
            <result column="role_id" property="id"/>
            <result column="role_name" property="name"/>
            <result column="role_type" property="type"/>
        </association>
    </resultMap>
    <!--查询语句-->
    <select id="selectUserById" resultMap="UserResultMap">
        select u.id ,u.name,r.id AS role_id ,r.name AS role_name ,r.type AS role_type FROM user AS u INNER JOIN role AS r ON u.id = r.user_id where u.id = #{id}
    </select>


2. 方法二: 嵌套select 查询

    <!-- 定义resultMap -->
    <resultMap id="UserResultMap" type="User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <association ofType="role" property="role" column="id" select="selectRoleByUserId"/>
    </resultMap>
    <!--查询语句-->
    <select id="selectUserById" resultMap="UserResultMap">
        select  id , name FROM user where id = #{id}
    </select>
    <!--查询语句-->
    <select id="selectRoleByUserId" resultMap="Role">
        select  id , name,type FROM role where user_id = #{userId}
    </select>


  • select=“selectRoleByUserId” 找的是第二个sql语句,如果调用别的xml文件中方法写全路径就可以找到.
  • column=“id” 参数id 传多个参数的话就是 {“属性名”=“参数”,“属性名”=“参数”} 这样的.

三、colleciton 一对多

Mybatis标签collection一对多的使用

  • 一、colleciton 标签
  • 二、collection使用方法
  • 1. 方法一: 嵌套结果映射
  • 2. 方法二: 嵌套select 查询
  • 三、 association 一对一

一、colleciton 标签

Mybatis的 collection 是一对多的使用的, 在 resultMap 标签内使用

当一个Bean中有 一个list属性需要关联查询出来的使用就用collection 标签

如下

查询用户结果 需要关联出 角色集合


用户

@Data
public class User {
    private Integer id;
    private String name;
    private List<Role> roles;
}

角色

@Data
public class Role {
    private Integer id;
    private Integer userId;
    private String name;
    private String type;
}


sql

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT '用户id',
  `name` varchar(255) DEFAULT NULL COMMENT '角色名称',
  `type` varchar(255) DEFAULT NULL COMMENT '角色类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;


二、collection使用方法

1. 方法一: 嵌套结果映射

    <!-- 定义resultMap -->
    <resultMap id="UserResultMap" type="User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <collection ofType="role" property="roles">
            <result column="role_id" property="id"/>
            <result column="role_name" property="name"/>
            <result column="role_type" property="type"/>
        </collection>
    </resultMap>
    <!--查询语句-->
    <select id="selectUserById" resultMap="UserResultMap">
        select u.id ,u.name,r.id AS role_id ,r.name AS role_name ,r.type AS role_type FROM user AS u INNER JOIN role AS r ON u.id = r.user_id where u.id = #{id}
    </select>

2. 方法二: 嵌套select 查询

    <!-- 定义resultMap -->
    <resultMap id="UserResultMap" type="User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <collection ofType="role" property="roles" column="id" select="selectRoleByUserId"/>
    </resultMap>
    <!--查询语句-->
    <select id="selectUserById" resultMap="UserResultMap">
        select  id , name FROM user where id = #{id}
    </select>
    <!--查询语句-->
    <select id="selectRoleByUserId" resultMap="role">
        select  id , name,type FROM role where user_id = #{userId}
    </select>


相关文章
|
30天前
|
XML Java 数据库连接
Mybatis一对一,一对多关联查询
## MyBatis一对一、一对多关联查询详解 MyBatis是一款优秀的持久层框架,提供了灵活的SQL映射功能,支持复杂的数据库操作。本文将详细介绍MyBatis中一对一和一对多关联查询的实现。 ### 一对一关联查询 一对一关联关系指的是一个表中的一条记录与另一个表中的一条记录相关联。例如,一个用户有一个地址信息。 #### 数据库表设计 假设有两个表:`user`和 `address`。 ``` CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE address
36 18
|
5月前
|
SQL XML Java
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
4月前
|
SQL XML Java
Mybatis中一对一和一对多的处理
这篇文章讲解了在Mybatis中如何处理一对一和一对多的关系映射,包括使用association和collection标签的具体方法。
117 1
|
9月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
162 0
|
6月前
|
Java 数据库连接 mybatis
后端框架的学习----mybatis框架(9、多对一处理和一对多处理)
这篇文章介绍了在MyBatis框架中如何处理多对一和一对多的关联查询,通过定义`<resultMap>`和使用`<association>`与`<collection>`元素来实现对象间的关联映射。
|
8月前
|
Java 数据库连接 mybatis
Mybatis基于注解的一对一和一对多查询
Mybatis基于注解的一对一和一对多查询
|
8月前
|
SQL Java 数据库连接
Mybatis中一对多mapper配置
Mybatis中一对多mapper配置
117 0
|
9月前
|
Java 数据库连接 mybatis
mybatis的一对多
mybatis的一对多
|
9月前
|
存储 XML Java
mybatis使用内部类处理一对多类型数据2
mybatis使用内部类处理一对多类型数据2
86 0
|
28天前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
52 2