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

复制代码
相关推荐
Darling噜啦啦1 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
swipe1 天前
正则表达式入门到进阶:从表单校验到手写模板引擎
前端·javascript·面试
神奇小汤圆1 天前
RAG大厂面试题汇总:向量检索、混合检索、Rerank、幻觉处理高频问题
面试
假如让我当三天老蒯1 天前
回归基本功:Map/Set 与 WeakMap/WeakSet 的区别
前端·面试
假如让我当三天老蒯1 天前
回归基本功!前端的解构赋值、扩展运算符、剩余参数
前端·面试
Lee川1 天前
Memory 模块深度解析(面试向)
人工智能·面试
JAVA9652 天前
JAVA面试-JVM篇 03-JVM运行时数据区哪些是线程私有的哪些是共享的
java·jvm·面试
枫子有风2 天前
LLM-Agent智能体(大厂面试常问)
面试·职场和发展·llm·agent
敲个大西瓜2 天前
mybatis拦截器插件实现数据库字段加解密
mybatis