Mybatis面试系列六

1、Mybatis 动态sql有什么用?执行原理?有哪些动态sql?

Mybatis 动态 sql 可以在 Xml 映射文件内,以标签的形式编写动态 sql,执行原理

是根据表达式的值完成逻辑判断并动态拼接sql的功能。

Mybatis 提供了 9种动态 sql标签:trim | where | set | foreach | if | choose

| when | otherwise | bind。

2、Xml映射文件中,除了常见的select|insert|updae|delete

标签之外,还有哪些标签?

答:

复制代码
<resultMap>、<parameterMap>、<sql>、<include>、
<selectKey>

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

签。

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

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

置 namespace,那么 id 不能重复;

原因就是namespace+id 是作为 Map<String, MapperStatement>的 key

使用的,如果没有namespace,就剩下 id,那么,id重复会导致数据互相覆盖。

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

也就不同。

4、为什么说Mybatis是半自动ORM映射工具?它与全自动

的区别在哪里?

Hibernate 属于全自动 ORM 映射工具,使用 Hibernate 查询关联对象或者关联

集合对象时,可以根据对象关系模型直接获取,所以它是全自动的。而Mybatis

在查询关联对象或关联集合对象时,需要手动编写sql来完成,所以,称之为半自

动 ORM映射工具。

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

复制代码
<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>

阿里云618精选云产品限量热卖,助力低成本上云

复制代码
相关推荐
疏桐1 小时前
并发
面试
雷渊1 小时前
redis可以保证数据不丢失吗?
java·后端·面试
·云扬·2 小时前
Java并发编程高频面试题
java·jvm·面试
Tadecanlan2 小时前
[C++面试] C++中各类括号的差异:[]、{}、<>、()
开发语言·c++·面试
雷渊3 小时前
redis如何实现发布/订阅功能?
java·后端·面试
雷渊3 小时前
redis中的zset怎么实现的?
java·后端·面试
远方小镇5 小时前
抖音开放平台-业务架构招前端
前端·javascript·面试
uhakadotcom5 小时前
Kubernetes Ingress 入门指南:简化集群服务访问
后端·面试·github
嘵奇5 小时前
Spring Boot内嵌服务器全解析:Tomcat vs Jetty vs Undertow 选型指南
服务器·spring boot·tomcat
uhakadotcom5 小时前
Apache SkyWalking:分布式系统的性能监控利器
后端·面试·github