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>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值