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

复制代码
相关推荐
AI人工智能+电脑小能手9 小时前
【大白话说Java面试题】【Java基础篇】第30题:JDK动态代理和CGLIB动态代理有什么区别
java·开发语言·后端·面试·代理模式
头发够用的程序员10 小时前
C++和Python面试经典算法汇总(一)
开发语言·c++·python·算法·容器·面试
云泽80812 小时前
二叉树高阶笔试算法题精讲(二):非递归遍历与序列构造全解析
c++·算法·面试
Cosolar12 小时前
大型语言模型(LLM)微调与量化技术全指南——从预训练到高效部署
人工智能·后端·面试
沪漂阿龙13 小时前
深度硬核!2026年NLP面试最全指南:从Word2Vec到Transformer,大模型时代算法工程师通关秘籍
自然语言处理·面试·word2vec
xsgbbx18 小时前
Agent Skills 实战:用 SKILL.md 把 Claude Code 从助手变成队友
面试
knight_9___18 小时前
LLM工具调用面试篇6
人工智能·python·面试·职场和发展·llm·agent
生物信息与育种19 小时前
黄三文院士领衔植物星球计划(PLANeT)发表Cell
人工智能·深度学习·算法·面试·transformer
闵孚龙20 小时前
一篇文章彻底吃透NumPy与Pandas——从零基础到面试通关的完整指南
面试·numpy·pandas
Heo20 小时前
14_React 中的更新队列 updateQueue
前端·javascript·面试