mybatis中association和collection使用

一、概述

  • association:一个复杂的类型关联。许多结果将包成这种类型
  • collection:复杂类型的集合

这2个属性的使用,而一对多和多对一都是相互的,只是站的角度不同。

二、使用

目前准备了两张表,一张人员表一张门派表,一个人员属于一个门派属于(一对一); 一个门派有多个人员属于(一对多)的关系;

准备好实体

/**
* 人员表
* @TableName persons
*/
@TableName(value ="persons")
@Data
public class Persons implements Serializable {
/**
* id
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id; /**
* 姓名
*/
@TableField(value = "name")
private String name; /**
* 年龄
*/
@TableField(value = "age")
private Integer age; /**
* 性别
*/
@TableField(value = "sex")
private String sex; /**
* 住址
*/
@TableField(value = "address")
private String address; /**
* 门派Id
*/
@TableField(value = "sect_id")
private Long sect_id; /**
* 绝技
*/
@TableField(value = "skill")
private String skill; /**
* 战力值
*/
@TableField(value = "power")
private Integer power; /**
* 创建时间
*/
@TableField(value = "create_time")
private LocalDateTime create_time; /**
* 修改时间
*/
@TableField(value = "modify_time")
private LocalDateTime modify_time; @ExcelIgnore
@TableField(exist = false)
private static final long serialVersionUID = 1L; @ExcelIgnore
@TableField(exist = false)
private Sect sect; //门派对象
}
/**
* 门派表
* @TableName sect
*/
@TableName(value ="sect")
@Data
public class Sect implements Serializable {
/**
* 主键id
*/
@TableId(value = "id")
private Long id; /**
* 门派名称
*/
@TableField(value = "sect_name")
private String sect_name; /**
* 创建日期
*/
@TableField(value = "create_time")
private LocalDateTime create_time; @TableField(exist = false)
private static final long serialVersionUID = 1L; @TableField(exist = false)
private List<Persons> persons; //人员列表
}

1.assocition使用

<resultMap id="BaseResultMap" type="com.test.entity.Persons">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="sex" column="sex" jdbcType="VARCHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
<result property="sect_id" column="sect_id" jdbcType="BIGINT"/>
<result property="skill" column="skill" jdbcType="VARCHAR"/>
<result property="power" column="power" jdbcType="INTEGER"/>
<result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
<result property="modify_time" column="modify_time" jdbcType="TIMESTAMP"/>
<!-- Sect对象字段 -->
<association property="sect" javaType="com.test.entity.Sect">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="sect_name" column="sect_name" jdbcType="VARCHAR"/>
<result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
</association>
</resultMap> <select id="getPersonList" resultMap="BaseResultMap">
select p.*,s.* from persons p left join sect s on s.id = p.sect_id
</select>

association:一对一

  • property:在人员表中指定的对象名称。
  • javaType:该对象的返回类型。

2.collection的使用

<resultMap id="BaseResultMap" type="com.test.entity.Sect">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="sect_name" column="sect_name" jdbcType="VARCHAR"/>
<result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
<!-- Person对象字段 -->
<collection property="persons" ofType="com.test.entity.Persons">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="sex" column="sex" jdbcType="VARCHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
<result property="sect_id" column="sect_id" jdbcType="BIGINT"/>
<result property="skill" column="skill" jdbcType="VARCHAR"/>
<result property="power" column="power" jdbcType="INTEGER"/>
<result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
<result property="modify_time" column="modify_time" jdbcType="TIMESTAMP"/>
</collection>
</resultMap> <select id="getSectList" resultMap="BaseResultMap">
select s.*,p.* from sect s left join persons p on s.id = p.sect_id
</select>

collection:一对多

  • property:门派表中指定的集合名称
  • ofType:对象的返回类型

以上两种都是以连表的形式写的,如果不想连表可直接在标签中实现

<association property="sect" javaType="com.test.entity.Sect" select="方法名" column="给select中指定的方法传递的参数"></association>
<collection property="persons" ofType="com.test.entity.Persons" select="方法名" column="给select中指定的方法传递的参数"></collection>

select:指定方法名,如果在同一个mapper中直接使用即可,如果不在需要将包名也写上例如:com.test.方法名

column:字段必须跟数据库字段名称一致

mybatis中association和collection使用的更多相关文章

  1. mybatis中association和collection的column传入多个参数值

    在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map

  2. mybatis中association的column传入多个参数值

    顾名思义,association是联合查询. 在使用association中一定要注意几个问题.文笔不好,白话文描述一下. 1: <association property="fncg ...

  3. MyBatis 中 Result Maps collection already contains value for xxx 错误原因

    出现此类错误的原因是同一个DAO操作多个Mapper导致的,将Mapper配置文件的  ResultMap的ID修改成不相同即可解决

  4. mybatis中foreach使用

    mybatis中的<foreach collection="list" item="item" index="index" open= ...

  5. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:)   标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...

  6. mybatis中collection和association的作用以及用法

    deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...

  7. MyBatis之ResultMap的association和collection标签(一)

    1.先说resultMap比较容易混淆的点, 2. Map结尾是映射,Type是类型  resultType 和restltMap restulyType: 1.对应的是java对象中的属性,大小写不 ...

  8. MyBatis之ResultMap的association和collection标签详解

    一.前言 MyBatis 创建时的一个思想是:数据库不可能永远是你所想或所需的那个样子. 我们希望每个数据库都具备良好的第三范式或 BCNF 范式,可惜它们并不都是那样. 如果能有一种数据库映射模式, ...

  9. 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明

    <foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...

  10. Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)

    Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...

随机推荐

  1. 状态 :睡眠中,进程ID:13431,yum提示Another app is currently holding the yum lock; waiting for it to exit...

    问题描述: 今天想在虚拟机上重新安装docker然后使用到yum命令报错: 解决办法: [root@localhost ~]# rm -f /var/run/yum.pid 然后重新运行刚才的yum命 ...

  2. R数据分析:用R建立预测模型

    预测模型在各个领域都越来越火,今天的分享和之前的临床预测模型背景上有些不同,但方法思路上都是一样的,多了解各个领域的方法应用,视野才不会被局限. 今天试图再用一个实例给到大家一个统一的预测模型的做法框 ...

  3. Linux之LVM逻辑卷管理

    LVM逻辑卷管理 LVM机制:PV物理卷,VG卷组,LV逻辑卷. --功能-- --物理卷管理-- --卷组管理-- --逻辑卷管理-- create(建立) pvcreate vgcreate lv ...

  4. docker访问外部https数字证书问题

    一般我们构建的 docker 镜像使用的都是 alpine linux 系统,默认是不带 ca-certificates 根证书的,导致无法识别外部 https 携带的数字证书. 在访问的时候,会抛出 ...

  5. 聊聊单点登录(SSO)中的CAS认证

    SSO介绍 背景 随着企业的发展,一个大型系统里可能包含 n 多子系统, 用户在操作不同的系统时,需要多次登录,很麻烦,我们需要一种全新的登录方式来实现多系统应用群的登录,这就是单点登录. web 系 ...

  6. flutter系列之:flutter中常用的Stack layout详解

    [toc] 简介 对于现代APP的应用来说,为了更加美观,通常会需要用到不同图像的堆叠效果,比如在一个APP用户背景头像上面添加一个按钮,表示可以修改用户信息等. 要实现这样的效果,我们需要在一个Im ...

  7. 发布日志- kratos v2.1.4 发布!

    v2.1.4 release https://github.com/go-kratos/kratos/releases/tag/v2.1.4 New Features feat(registry/co ...

  8. 9. Ceph 基础篇 - Crush Maps

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485302&idx=1&sn=00a3a204 ...

  9. nginx干货文档

    文档地址:https://files.cnblogs.com/files/sanduzxcvbnm/跟冰河学习Nginx技术.pdf

  10. 用AR Engine手部骨骼跟踪能力实现虚拟手表试戴

    AR技术的落地应用,推动着电商领域的不断升级,通过增强现实为用户带来了虚拟与现实结合的AR购物体验.如AR试衣.AR试鞋.AR试妆等功能的出现让用户在手机上就能体验产品的佩戴效果,可以让用户更直观.更 ...