resultMap标签中里的association标签


association(一对一):映射到JavaBean的某个“复杂类型”属性,比如JavaBean类,即JavaBean,即JavaBean内部嵌套一个复杂数据类型(JavaBean)属性,这种情况就属于复杂类型的关联。但是需要注意:association仅处理一对一的关联关系。

下面通过一个示例来演示association具体的应用,示例需求:查询学生表的全部数据信息。

首先修改Student,增加Grade (Grade实体类只有gid和gname两个属性) 年级属性,并增加响应的getter和setter方法,示例代码如下:

public class Student {
	private int stuid;
	private String stuname;
	private String stupwd;
	private String stusex;
	private int stuage;
	private Grade grade;
	//省略getter和setter方法 toString和构造器等方法
}

我们的JavaBean:Student对象内部嵌套了一个复杂数据类型的属性:grade。然后在对应的Dao接口中增加获取全部信息的方法:

List<Student> findAll();

修改对应的Mapper映射文件。由于Student对象内嵌JavaBean对象(Grade),因此需要使用association来实现结果映射。代码如下:

<resultMap type="student" id="stumap">
	<id column="stuid" property="stuid"/>
	<result column="stuname" property="stuname"/>
	<result column="stupwd" property="stupwd"/>
	<result column="stuage" property="stuage"/>
	<result column="stusex" property="stusex"/>
	<!-- 外键列如何表示 -->
	<!-- association描述关系 -->
	<association property="grade" javaType="grade">
		<id column="gid" property="gid"/>
		<result column="gname" property="gname"/>
	</association>
</resultMap>

<select id="findAll" resultMap="stumap">
	select stuid,stuname,stupwd,stuage,grade.gid,gname
	from grade,student
	where student.gid=grade.gid
</select>

从上述代码中,简单分析association的属性。
javaType:完整Java类名或别名。
property:映射数据库列的实体对象的属性。
id。设置该项可以有效地提升MyBatis的性能。
result。property:映射数据库列的实体类对象的属性。 column:数据库列名或别名

最后进行测试即可:

@org.junit.Test
	public void selectInfo() {
		SqlSession session=null;
		try {
			session = MyBatisUtils.getSqlSession();
			StudentDao sd = session.getMapper(StudentDao.class);
			List<Student> list = sd.findAll();
			list.forEach(System.out::println);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			MyBatisUtils.closeSqlSession(session);
		}
	}

测试结果如下:
在这里插入图片描述

虽然我们成功查询到了信息,但是association是没有办法达到复用的,那么它是否可复用?
答案是肯定的,association提供了另一个属性:resultMap。通过这个属性可以扩展一个resultMap来进行联合映射。只需将刚刚的映射文件修改如下即可:

<resultMap type="student" id="stumap">
	<id column="stuid" property="stuid"/>
	<result column="stuname" property="stuname"/>
	<result column="stupwd" property="stupwd"/>
	<result column="stuage" property="stuage"/>
	<result column="stusex" property="stusex"/>
	<!-- 外键列如何表示 -->
	<!-- association描述关系 -->
	<association property="grade" javaType="grade" resultMap="grademap"></association>
</resultMap>

<resultMap type="grade" id="grademap">
	<id column="gid" property="gid"/>
	<result column="gname" property="gname"/>
</resultMap>
	
<select id="findAll" resultMap="stumap">
	select stuid,stuname,stupwd,stuage,grade.gid,gname
	from grade,student
	where student.gid=grade.gid
</select>

以上就可以使association达到复用的效果。

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值