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 ...
随机推荐
- elasticsearch设置密码及Java密码连接
目录 1. 安装 2. 修改elasticsearch-8.2.2\config\elasticsearch.yml文件里面xpack.security.enabled: false为 3. 重新启动 ...
- python压缩pdf(指定缩放比例)
python压缩pdf(指定缩放比例) 原理 pdf文件处理使用https://pymupdf.readthedocs.io/en/latest/index.html库可以轻松实现,该库的官方说明文档 ...
- 使用 MAUI 在 Windows 和 Linux 上绘制 PPT 的图表
我在做一个图表工具软件,这个软件使用 MAUI 开发.我的需求是图表的内容需要和 PPT 的图表对接,需要用到 OpenXML 解析 PPT 内容,读取到 PPT 图表元素的内容,接着使用 MAUI ...
- Rust基本数据类型
基本类型 Rust 每个值都有其确切的数据类型,总的来说可以分为两类:基本类型和复合类型. 基本类型意味着它们往往是一个最小化原子类型,无法解构为其它类型(一般意义上来说),由以下组成: 数值类型: ...
- 常用的清理 Kubernetes 集群资源命令
1. Kubernetes 基础对象清理 清理 Evicted 状态的 Pod kubectl get pods --all-namespaces -o wide | grep Evicted | a ...
- 第三章:模版层 - 1:Django模板语言详解
本节将介绍Django模版系统的语法.Django模版语言致力于在性能和简单性上取得平衡. 如果你有过其它编程背景,或者使用过一些在HTML中直接混入程序代码的语言,那么你需要记住,Django的模版 ...
- Dockerfile文件中的ENTRYPOINT,CMD命令跟k8s中command,args之间的关系
- SECS半导体设备通讯-4 GEM通信标准
一 概述 GEM标准定义了通信链路上的半导体设备的行为. SECS-II标准定义了在主机和设备之间交换的消息和相关数据项.GEM标准则定义了在哪种情况下应该使用哪些SECS-II消息以及由此产生的结果 ...
- js动态获取当前时间(年、月、日、上午/下午、时、分、秒)
//获取动态时间function mytime() { var mydate = new Date(); var year = mydate.getFullYear(); var month = my ...
- 【Java8新特性】- Stream流
Java8新特性 - Stream流的应用 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! ...