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
:字段必须跟数据库字段名称一致