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
:字段必须跟数据库字段名称一致
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤