Mybatis面试题总结

1.#与$有什么区别?

2.如何解决实体类属性名称与表字段名称不一致的情况?

  1. 在sql语句中使用别名
  2. 使用resultMap标签实现手动映射

3.如何实现多表关联查询的一对一、一对多及多对多的关系?

  1. 使用resultMap+association标签实现一对一
  2. 使用resultMap+collection标签实现一对多
  3. 使用resultMap+collection+collection标签实现多对多

4.如何实现模糊查询?

  1. 使用 like ‘%${value}%’
    <select id="queryLikeByName" resultType="user">
    	select * from user where name like '%${value}%'
    </select>
    
  2. 使用bind标签
    <select id="queryLikeByName" resultType="user">
    	<bind name="name" value="'%'+name+'%'">
    	select * from user where name like #{name}
    </select>
    

    注意:使用bind标签时,里面的参数名称只能使用@Param("")的别名

5.DAO接口/MAPPER接口的原理是什么?接口中的方法可以重载吗?

Mybatis基于接口开发的实现原理是使用了JDK动态代理,在调用接口中方法时,Mybatis会为接口生成代理对象,代理对象会拦截接口中的方法,转而执行MappedStatement对象所代表的sql,然后将执行结果返回。其中MappedStatement对象产生于xml文件中的标签(每个select、insert等标签都会被解析成一个MappedStatement对象),Mybatis会将接口全限定名+方法名拼接为一个字符串作为一个key值,通过key值,可唯一定位到一个MappedStatement对象。

由于Mybatis使用的是接口全限定名+方法名作为key寻找指定的MappedStatement对象策略,所以接口中方法不能重载

6.如何实现动态SQL?

动态SQL是指根据表达式的值进行逻辑判断,然后动态拼接SQL。Mybatis提供了9中标签来实现
trim | where | set | if | choose | when | otherwise | foreach | bind

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
	      PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
	      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.khue.mapper.UserMapper">
	<!--
		name有值执行:select * from user where name=#{name}
		name无值执行:select * from user
	-->
	<select id="queryByConditon" resultType="user">
		select * from user
		<where>
			<if test="name!=null and name!=''">and name=#{name}</if>
		</where>
	</select>
	
	<select id="queryByConditon" resultType="user">
		select * from user
		<where>
			<choose>
				<when test="name!=null and name!=''">and name=#{name}</when>
				<otherwise>1=1</otherwise>
			</choose>
		</where>
	</select>
	
	<select id="queryByConditon" resultType="user">
		select * from user
		<trim prefix="where" prefixOverrides="and">
			<if test="name!=null and name!=''">and name=#{name}</if>
		</trim>
	</select>
	
	<!--
		name有值执行:update user set name=#{name} where id=#{id}
		name无值执行:update user where id=#{id}
	-->
	<update id="updateByCondition">
		update user
		<set>
			<if test="name!=null and name!=''">name=#{name},</if>
		</set>
		where id=#{id}
	</update>
	
	<update id="updateByCondition">
		update user
		<trim prefix="set" suffixOverrides=",">
			<if test="name!=null and name!=''">name=#{name},</if>
		</trim>
		where id=#{id}
	</update>
	
	<!--
		select * from user where id in(1,2,...)
		参数为数组或可变参数时,collection为array
		参数为List时,collection为list
	-->
	<select id="queryByCondition" resultType="user">
		select * from user where id in
		<foreach collection="array" open="(" separator="," close=")" item="id">#{id}</foreach>
	</select>
	
	<!--模糊查询-->
	<select id="queryLikeByName" resultType="user">
		<bind name="name" value="'%'+name+'%'">
		select * from user where name like #{name}
	</select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个开源的持久层框架,它可以数据库操作与Java对象之间的映射关系进行配置,简化了数据库操作的编写过程。下面是一些常见的MyBatis面试题及其答案: 1. 什么是MyBatisMyBatis是一个持久层框架,它可以将数据库操作与Java对象之间的映射关系进行配置,简化了数据库操作的编写过程。 2. MyBatis的优点有哪些? - 简化了数据库操作的编写过程,提高了开发效率。 - 提供了灵活的SQL映射配置,可以满足各种复杂的查询需求。 - 支持动态SQL,可以根据不同的条件生成不同的SQL语句。 - 提供了缓存机制,可以提高查询性能。 - 与Spring等框架集成较为方便。 3. MyBatis的核心组件有哪些? MyBatis的核心组件包括: - SqlSessionFactory:用于创建SqlSession对象的工厂。 - SqlSession:用于执行SQL语句和管理事务。 - Mapper接口:定义了数据库操作的方法。 - Mapper XML文件:配置了SQL语句和结果映射关系。 4. MyBatis中的动态SQL是什么? 动态SQL是指根据不同的条件生成不同的SQL语句。MyBatis提供了一些标签(如if、choose、foreach等)来实现动态SQL的编写,可以根据条件判断、循环等来动态生成SQL语句。 5. MyBatis的一级缓存和二级缓存有什么区别? - 一级缓存是SqlSession级别的缓存,它默认开启且不可关闭。在同一个SqlSession中,如果执行了相同的查询语句,那么第二次以后的查询会直接从缓存中获取结果,而不会再去数据库查询。 - 二级缓存是Mapper级别的缓存,它可以跨SqlSession共享。当多个SqlSession执行相同的查询语句时,如果开启了二级缓存,那么第二次以后的查询会直接从缓存中获取结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值