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>


相关文章
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
SQL XML Java
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
mybatis元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id *,result*,association报错解决
902 0
|
Java 数据库连接 mybatis
一文彻底搞懂Mybatis系列(十二)之MyBatis多对一映射延迟加载(association和lazyLoadingEnabled)
一文彻底搞懂Mybatis系列(十二)之MyBatis多对一映射延迟加载(association和lazyLoadingEnabled)
484 0
|
Java 数据库连接 mybatis
mybatis中的association和collection
mybatis中的association和collection
245 0
|
Java 数据库连接 mybatis
MyBatis中对象映射关联之association使用实践
MyBatis中对象映射关联之association使用实践
1082 1
|
Java 数据库连接 mybatis
【Mybatis用法】Mybatis框架中一对一,一对多association和collection的使用举例方法
【Mybatis用法】Mybatis框架中一对一,一对多association和collection的使用举例方法
690 0
|
SQL Java 数据库连接
【Mybatis用法】Mybatis 高级结果映射,ResultMap Association,mybatis的一对多,多对一,以及多对多的配置和使用
【Mybatis用法】Mybatis 高级结果映射,ResultMap Association,mybatis的一对多,多对一,以及多对多的配置和使用
642 0
|
SQL Java 数据库连接
mybatis <association> 标签的使用
mybatis <association> 标签的使用
1322 0
|
SQL Java 数据库连接
MyBatis中association的使用(MyBatis高级结果映射)
MyBatis中association的使用(MyBatis高级结果映射)
491 0
|
XML Java 数据库连接
MyBatis之ResultMap的association和collection标签详解(图文例子)
MyBatis之ResultMap的association和collection标签详解(图文例子)
1169 0
MyBatis之ResultMap的association和collection标签详解(图文例子)