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 ...
随机推荐
- SpringBoot 整合 MongoDB 实战介绍
一.介绍 在前面的文章中,我们详细的介绍了 MongoDB 的配置和使用,如果你对 MongoDB 还不是很了解,也没关系,在 MongoDB 中有三个比较重要的名词:数据库.集合.文档! 数据库(D ...
- 第六十篇:Vue的基本使用
好家伙,要来了,经典"hello world" 试用一下vue ① 导入 vue.js的 script 脚本文件 ② 在页面中声明一个将要被vue所控制的DOM区域 ③ 创建vm实 ...
- PHP生成唯一不重复的编号
当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 下载:https://url72 ...
- ar9485 win10 笔记本电脑 无线网间歇性掉线
问题 新安装了系统,刚开始上网正常,下载东西或者待机一段时间后掉线了. wifi都能搜到,就是连不上,重启电脑可以解决,但是每次重启很麻烦,必须找到治本的方法. 排除问题 1.手机连接没有问题,排除 ...
- 传输层协议(tcp ip和udp 三次握手 四次握手)
1 TCP/IP协议介绍 TCP/IP协议:Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议. TCP/IP是一个Proto ...
- 第一个Django应用 - 第六部分:静态文件
前面我们编写了一个经过测试的投票应用,现在让我们给它添加一张样式表和一张背景图片. 除了由服务器生成的HTML文件外,WEB应用一般需要提供一些其它的必要文件,比如图片文件.JavaScript脚本和 ...
- Ingress
一.需求背景 固定对外提供服务采用了NodePort方式映射并固定了30001端口,但是,该端口默认范围是30000~32767,并且我们的web服务一般都是80.443端口对外,因此我们产生了如下几 ...
- mongodb集群搭建(分片+副本)开启安全认证
关于安全认证得总结: 这个讲述的步骤也是先创建超管用户,关闭服务,然后生成密钥文件,开启安全认证,启动服务 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...
- Gitlab添加K8S集群
介绍如何在Gitlab项目中添加K8S集群,以便使用K8S集群部署gitlab-runner帮我们运行gitlab的CI/CD. 参考官方文档:https://docs.gitlab.com/ee/u ...
- Node.js躬行记(23)——Worker threads
Node.js 官方提供了 Cluster 和 Child process 创建子进程,通过 Worker threads 模块创建子线程.但前者无法共享内存,通信必须使用 JSON 格式,有一定的局 ...