mybatis中association和collection使用
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使用的更多相关文章
- mybatis中association和collection的column传入多个参数值
在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map
- mybatis中association的column传入多个参数值
顾名思义,association是联合查询. 在使用association中一定要注意几个问题.文笔不好,白话文描述一下. 1: <association property="fncg ...
- MyBatis 中 Result Maps collection already contains value for xxx 错误原因
出现此类错误的原因是同一个DAO操作多个Mapper导致的,将Mapper配置文件的 ResultMap的ID修改成不相同即可解决
- mybatis中foreach使用
mybatis中的<foreach collection="list" item="item" index="index" open= ...
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- mybatis中collection和association的作用以及用法
deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...
- MyBatis之ResultMap的association和collection标签(一)
1.先说resultMap比较容易混淆的点, 2. Map结尾是映射,Type是类型 resultType 和restltMap restulyType: 1.对应的是java对象中的属性,大小写不 ...
- MyBatis之ResultMap的association和collection标签详解
一.前言 MyBatis 创建时的一个思想是:数据库不可能永远是你所想或所需的那个样子. 我们希望每个数据库都具备良好的第三范式或 BCNF 范式,可惜它们并不都是那样. 如果能有一种数据库映射模式, ...
- 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明
<foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...
- Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)
Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...
随机推荐
- 几款优秀的点播、RTSP/RTMP直播播放器介绍
1.ijkplayer 项目地址: https://github.com/Bilibili/ijkplayer 介绍:Ijkplayer 是Bilibili发布的基于 FFplay 的轻量级 Andr ...
- 面试突击83:什么情况会导致@Transactional事务失效?
一个程序中不可能没有事务,而 Spring 中,事务的实现方式分为两种:编程式事务和声明式事务,又因为编程式事务实现相对麻烦,而声明式事务实现极其简单,所以在日常项目中,我们都会使用声明式事务 @Tr ...
- Linux下从零开始创建lvm虚拟磁盘阵列+脚本化解决方案
逻辑卷管理器(英语:Logical Volume Manager,缩写为LVM),又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Logical volume ...
- 使用traefik进行流量复制
文章转载自:https://mp.weixin.qq.com/s/nMMN7hAJK6SFn1V1YyxvHA Traefik 2.0 还引入了镜像服务,一种可以将流入流量复制并同时将其发送给其他服务 ...
- PostgreSQL 创建数据库
PostgreSQL 创建数据库可以用以下三种方式: 1.使用 CREATE DATABASE SQL 语句来创建. 2.使用 createdb 命令来创建. 3.使用 pgAdmin 工具. CRE ...
- 一文搞定 Spring事务
Spring 事务 上文 使用SpringJDBC 1.JDBC事务控制 不管你现在使用的是那一种ORM开发框架,只要你的核心是JDBC,那么所有的事务处理都是围绕着JDBC开展的,而JDBC之中 ...
- 基于.NetCore开发博客项目 StarBlog - (18) 实现本地Typora文章打包上传
前言 九月太忙,只更新了三篇文章,本来这个功能是从九月初就开始做的,结果一直拖到现在国庆假期才有时间完善并且写文章~ 之前我更新了几篇关于 Python 的文章,有朋友留言问是不是不更新 .Net 了 ...
- CF600E Lomsat gelral (线段树合并)
相当于是线段树合并的模板题,比(雨天的尾巴)还要板. 唯一注意的是线段树的更新,因为同一子树中可能有多种颜色占主导地位,要输出编号和,比如一颗子树中,1出现3次(最多),3出现3次,那么应该输出4. ...
- 在vue中_this和this的区别
_this只是一个变量名,this代表父函数,如果在子函数还用this,this的指 向就变成子函数了,_this就是用来存储指向的 普通函数中的this表示调用此函数时的对象,箭头函数里面的this ...
- 一天一道Java面试题----第十二天(如何实现接口幂等性)
这里是参考B站上的大佬做的面试题笔记.大家也可以去看视频讲解!!! 文章目录 1.如何实现接口幂等性 1.如何实现接口幂等性 唯一id.每次操作,都根据操作和内容生成唯一的id,在执行之前先判断id是 ...