Mybatis最全的高质量面试题和答案—3

16、Xml 映射文件中,除了常见的select|insert|updae|delete标签之外,还有哪些标签?

答: resultMap、parameterMap、sql、include、selectKey,加上动态sql 的9 个标签, 其中sql为sql 片段标签,通过include标签引入sql 片段,selectKey为不支持自增的主键生成策略标签。

17、Mybatis 的Xml 映射文件中, 不同的Xml 映射文件, id 是否可以重复?

不同的Xml 映射文件,如果配置了namespace,那么id 可以重复;如果没有配置namespace,那么id 不能重复;

原因就是namespace+id 是作为Map<String, MapperStatement>的key使用的, 如果没有namespace,就剩下id,那么, id 重复会导致数据互相覆盖。

有了namespace,自然id 就可以重复,namespace 不同,namespace+id 自然也就不同。

18、为什么说Mybatis 是半自动ORM 映射工具?它与全自动的区别在哪里?

Hibernate 属于全自动ORM 映射工具,使用Hibernate 查询关联对象或者关联集合对象时,可以根据对象关系模型直接获取,所以它是全自动的。而Mybatis在查询关联对象或关联集合对象时,需要手动编写sql 来完成,所以,称之为半自动ORM 映射工具。

19、一对一、一对多的关联查询?

<mapper namespace="com.lcb.mapping.userMapper">

<!--association 一对一关联查询-->
<select id="getClass" parameterType="int"
resultMap="ClassesResultMap">
select * from class c,teacher t where c.teacher_id=t.t_id and
c.c_id=#{id}
</select>

<resultMap type="com.lcb.user.Classes" id="ClassesResultMap">
<!-- 实体类的字段名和数据表的字段名映射-->
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher"
javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>

<!--collection 一对多关联查询-->
<select id="getClass2" parameterType="int"
resultMap="ClassesResultMap2">
select * from class c,teacher t,student s where c.teacher_id=t.t_id
and c.c_id=s.class_id and c.c_id=#{id}
</select>

<resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher"
javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<collection property="student"
ofType="com.lcb.user.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>

</mapper>

20、MyBatis 实现一对一有几种方式?具体怎么操作的?

有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次, 通过在resultMap 里面配置association 节点配置一对一的类就可以完成;

嵌套查询是先查一个表,根据这个表里面的结果的外键id,去再另外一个表里面查询数据,也是通过association 配置,但另外一个表的查询通过select 属性配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值