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精选云产品限量热卖,助力低成本上云

复制代码
相关推荐
前端小巷子12 分钟前
跨域问题解决方案:JSONP
前端·javascript·面试
ladymorgana9 小时前
【Spring boot】tomcat Jetty Undertow对比,以及应用场景
spring boot·tomcat·jetty
Alfred king11 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
一只叫煤球的猫12 小时前
手撕@Transactional!别再问事务为什么失效了!Spring-tx源码全面解析!
后端·spring·面试
喜欢敲代码的程序员12 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
海的诗篇_15 小时前
前端开发面试题总结-原生小程序部分
前端·javascript·面试·小程序·vue·html
胡清波17 小时前
# vue 的 Diff 算法
前端·面试
Jackson_Mseven17 小时前
面试官:useEffect 为什么总背刺?我:闭包、ref 和依赖数组的三角恋
前端·react.js·面试
凌辰揽月17 小时前
8分钟讲完 Tomcat架构及工作原理
java·架构·tomcat
绝无仅有18 小时前
对接三方SDK开发过程中的问题排查与解决
后端·面试·架构