8、mybatis中的sql映射文件详解(3)

对于初学者,如何进行mybatis的学习呢?我总结了几点,会慢慢的更新出来。首先大家需要了解mybatis是什么、用mybatis来做什么、为什么要用mybatis、有什么优缺点;当知道了为什么的时候就开始了解如何用的问题,如何使用mybatis、有几种使用方式、各种方式的优缺点,在这个阶段也会学习mybatis涉及到的一些标签的用法;当知道了基础用法之后,就开始接触一些高级的用法,例如动态sql的使用、mybatis的缓存使用等;至此,在实战项目中使用mybatis进行开发已经没有问题了。

接下来就开始深入的研究一下mybatis这个持久层的框架,在纯技术的方面进行研究,提高自己的能力。首先,大家需要了解一下mybatis的整体技术架构和工作原理;接下来,就开始了解一下mybatis各大核心组件的具体功能及其工作原理。至此,算是对mybatis的原理简单的了解一下了,由于博主的能力有限,因此对于mybatis的框架技术研究也就到这里算结束了。

最后会了解一些其他的东西,例如:mybatis的逆向工程使用、如何开发一个mybatis插件,在这里会介绍一下mybatis的分页实现等。

至此,mybatis也算是入门了,出去就可以和别人说,你稍微了解mybatis框架,对其也多少有一点自己的理解和看法了。

目录

1、级联属性的映射

2、association指定关联对象映射

3、association 分步查询(嵌套查询)

4、collection 指定关联集合对象映射

5、collection 分步查询(嵌套查询)

6、discriminator 鉴别器的使用


上一篇 介绍了输入映射和输出映射,结束于自定义映射resultMap的更多用法,这一篇主要针对resultMap 的一些高级用法进行叙述。

1、级联属性的映射

这里直接通过例子来看用法,例如:每一个用户对应一个部门,查询用户信息时需要级联查询出部门的信息,此时就可以使用resultMap 自定义映射关系,具体如下:

<resultMap id="user2" type="com.app.mapper.User">
	<id column="id" property="id"/>
	<result column="name" property="name"/>
	<result column="dname" property="department.name"/>
</resultMap>

<select id="selectUser2" resultMap="user2">
	select a.id, a.name, b.tname from oa_user a, oa_department b 
    where a.id = #{id} and a.department = b.id
</select>

此时,User 实体类的属性定义如下:

private Long id; // id
private String name; // 用户名
private Department department; // 关联的部门

2、association指定关联对象映射

对于1中的sql 映射,可以使用 association 标签进行指定关联对象的映射,其具体使用如下:

<select id="selectUser3" resultMap="user3">
	select a.id, a.name, b.did, b.dname from oa_user a, oa_department b 
    where a.id = #{id} and a.department = b.did
</select>

<resultMap id="user3" type="com.app.mapper.User">
	<id column="id" property="id"/>
	<result column="name" property="name"/>
	<association property="department" javaType="com.app.mapper.Department">
		<id column="did" property="did"/>
		<result column="dname" property="name"/>
	</association>
</resultMap>

3、association 分步查询(嵌套查询)

有的时候sql 语句过于复杂,无法一条语句获取到最终的结果,需要多条语句,或者为了实现延迟加载,针对于 2 中例子,可以通过嵌套查询的方式实现,具体如下:

<select id="getUserById" resultMap="user"></select>

<resultMap type="com.app.mapper.User" id="user">
	<id column="id" property="id"/>
	<result column="name" property="name"/>

	<!--   嵌套一个查询,N+1查询     -->
	<association property="department"
				 select="com.app.mapper.UserMapper.getDepartmentByDid"
				 column="did"/>

</resultMap>

<select id="getDepartmentByDid" resultType="com.app.mapper.Department">
	select did, name from department where did = #{did}
</select>

如果想要实现延时加载,需要在mybatis全局配置文件中设置,这个在 全局配置文件 介绍一篇中有介绍:

<setting name="lazyLoadingEnabled" value="true"></setting>

4、collection 指定关联集合对象映射

有的时候关联的数据是一个集合,例如,一个学生有多个老师,此时就需要使用collection 标签进行结果对象映射了,具体如下:

<select id="getStudentBySid" resultMap="student1">
	select a.sid, a.sname, b.tid, b.tname from student a, teacher b 
    where b.sid = a.sid
</select>

<resultMap id="student1" type="com.app.mapper.Student">
	<id column="sid" property="sid"/>
	<result column="sname" property="sname"/>
	<collection property="teachers" ofType="com.app.mapper.Teacher">
		<id column="tid" property="tid"/>
		<result column="tname" property="tname"/>
	</collection>
</resultMap>

 对应的java实体如下:

private Long sid;
private String sname;
private List<Teacher>  teachers;

5、collection 分步查询(嵌套查询)

类似于 3 ,如果想要实现延时加载的话,可以通过使用 嵌套查询(N+1查询)的方式实现,具体如下:

<select id="getStudentBySid" resultMap="student1">
	select a.sid, a.sname, b.tid, b.tname from student a, teacher b 
    where a.tid = b.tid
</select>

<resultMap id="student1" type="com.app.mapper.Student">
	<id column="sid" property="sid"/>
	<result column="sname" property="sname"/>
	<collection property="teachers" 
                select="com.app.mapper.UserMapper.selectTeacherBySid" 
                column="sid"                         
                fetchType="lazy"/>
</resultMap>
<select id="getStudentBySid" resultType="com.app.mapper.UserMapper.Teacher">
	select tid, tname from teacher  where sid = #{sid}
</select>

6、discriminator 鉴别器的使用

鉴别器的作用类似于if ... else ... 选择的作用,在进行分步查询时,可以对前一步查询出来的自定字段值进行判断来确定下一步的执行行为,例如查询用户字段,根据用户的性别来进行不同联系方式的映射,具体使用如下:

<select id="getUser" resultMap="user3">
	select uid, uname, sex, phone, email from user where uid = #{uid}
</select>

<resultMap id="user3" type="com.app.mapper.User">
	<id column="uid" property="uid"/>
	<result column="uname" property="uname"/>
	<discriminator javaType="int">
		<!-- 
			1 为男,若为1, 则将用户的手机映射到联系方式; 
			2 为女,若为2, 则将用户的邮箱映射到联系方式 
		-->
		<case value="1">
			<result column="phone" property="link_info"/>
		</case>
		<case value="2">
			<result column="email" property="link_info"/>
		</case>
	</discriminator>
</resultMap>

这一篇这介绍到这里,下一篇开始进行动态sql开发的介绍。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值