1、什么是 Mybatis?
2、Mybaits 的优点:
2、与 JDBC 相比,减少了 50%以上的代码量,消除了 JDBC 大量冗余的代码,不需要手动开关连接;
3、很好的与各种数据库兼容(因为 MyBatis 使用 JDBC 来连接数据库,所以只要JDBC 支持的数据库 MyBatis 都支持)。
4、能够与 Spring 很好的集成;
5、提供映射标签,支持对象与数据库的 ORM 字段关系映射;提供对象关系映射标签,支持对象关系组件维护。
3、MyBatis 框架的缺点:
2、SQL 语句依赖于数据库,导致数据库移植性差,不能随意更换数据库。
4、MyBatis 框架适用场合:
2、对性能的要求很高,或者需求变化较多的项目,如互联网项目,MyBatis 将是不错的选择
5、MyBatis 与 Hibernate 有哪些不同?
2、Mybatis 直接编写原生态 sql,可以严格控制 sql 执行性能,灵活度高,非常适合对关系数据模型要求不高的软件开发,因为这类软件需求变化频繁,一但需求变化要求迅速输出成果。但是灵活的前提是 mybatis 无法做到数据库无关性,如果需要实现支持多种数据库的软件,则需要自定义多套 sql 映射文件,工作量大。
3、Hibernate 对象/关系映射能力强,数据库无关性好,对于关系模型要求高的软件,如果用 hibernate 开发可以节省很多代码,提高效率。
6、#{}和${}的区别是什么?
Mybatis 在处理#{}时,会将 sql 中的#{}替换为?号,调用 PreparedStatement 的set 方法来赋值;
使用#{}可以有效的防止 SQL 注入,提高系统安全性。
7、当实体类中的属性名和表中的字段名不一样 ,怎么办 ?
<select id=”selectorder” parametertype=”int” resultetype=”
me.gacl.domain.order”>
select order_id id, order_no orderno ,order_price price form
orders where order_id=#{id};
</select>
<select id="getOrder" parameterType="int"
resultMap="orderresultmap">
select * from orders where order_id=#{id}
</select>
<resultMap type=”me.gacl.domain.order” id=”orderresultmap”>
<!–用 id 属性来映射主键字段–>
<id property=”id” column=”order_id”>
<!–用 result 属性来映射非主键字段,property 为实体类属性名,column
为数据表中的属性–>
<result property = “orderno” column =”order_no”/>
<result property=”price” column=”order_price” />
</reslutMap>
8、 模糊查询 like 语句该怎么写?
string wildcardname = “%smi%”;
list<name> names = mapper.selectlike(wildcardname);<select id=”selectlike”>
select * from foo where bar like #{value}
</select>
string wildcardname = “smi”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>
9、通常一个 Xml 映射文件,都会写一个 Dao 接口与之对应,请问,这个 Dao 接口的工作原理是什么?Dao 接口里的方法,参数不同时,方法能重载吗?
举例:com.mybatis3.mappers^tudentDao.findStudentByld,可以唯
—找到 namespace 为 co m.mybatis3. map pe rs.Stud ent Dao 下面 id 为
findStudentByld MapperStatement.
Mapper接口里的方法,是不能重载的,因为是使用全限名+方法名的保存和寻找策略。Mapper授口的工作原理是JDK动态代理,Mybatis运行时会使用JDK
动态代理为Mapper接口生成代理对象proxy,代理对象会拦截接口方法,转而
执行MapperStatement所代表的sql,然后将sql执行结果返回.
10、Mybatis 是如何进行分页的?分页插件的原理是什么?
Mybatis使用RowBounds对象讲行分页,它是针对ResultSet结果集执行的内存分页,而非物理分页.可以在sql内直接书写带有物理分页的参数来完成物理分页功能,也可以使用分页播件来完成物理分页.
分页播件的基本原理是使用Mybatis提供的插件接口,实现自定义插件,在插件的栏截方法内拦截方法内拦截待执行的sql,然后重写sql, 根据dialect方言,添加对应的物理分页语句和物理分页参数.
11、Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式?
第二种是使用 sql 列的别名功能,将列的别名书写为对象属性名。
12、如何执行批量插入?
<insert id=”insertname”>
insert into names (name) values (#{value})
</insert>
list < string > names = new arraylist();
names.add(“fred”);
names.add(“barney”);
names.add(“betty”);
names.add(“wilma”);
// 注意这里 executortype.batch
sqlsession sqlsession =
sqlsessionfactory.opensession(executortype.batch);
try {
namemapper mapper = sqlsession.getmapper(namemapper.class);
for (string name: names) {
mapper.insertname(name);
}
sqlsession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
}
finally {
sqlsession.close();
}
13、如何获取自动生成的(主)键值?
如果采用自增长策略,自动生成的键值在 insert 方法执行完后可以被设置到传入的参数对象中。
<insert id=”insertname” usegeneratedkeys=”true” keyproperty=”
id”>
insert into names (name) values (#{name})
</insert>
name name = new name();
name.setname(“fred”);
int rows = mapper.insertname(name);
// 完成后,id 已经被设置到对象中
system.out.println(“rows inserted = ” + rows);
system.out.println(“generated key value = ” + name.getid());
14、在 mapper 中如何传递多个参数?
public UserselectUser(String name,String area);
对应的 xml,#{0}代表接收的是 dao 层中的第一个参数,#{1}代表 dao 层中第二
参数,更多参数一致往后加即可。
<select id="selectUser"resultMap="BaseResultMap">
select * fromuser_user_t whereuser_name = #{0}
anduser_area=#{1}
</select>
public interface usermapper {
user selectuser(@param(“username”) string
username,@param(“hashedpassword”) string hashedpassword);
}
<select id=”selectuser” resulttype=”user”>
select id, username, hashedpassword
from some_table
where username = #{username}
and hashedpassword = #{hashedpassword}
</select>
try {
//映射文件的命名空间.SQL 片段的 ID,就可以调用对应的映射文件中的
SQL
//由于我们的参数超过了两个,而方法中只有一个 Object 参数收集,因此
我们使用 Map 集合来装载我们的参数
Map < String, Object > map = new HashMap();
map.put("start", start);
map.put("end", end);
return sqlSession.selectList("StudentID.pagination", map);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
}
15、Mybatis 动态 sql 有什么用?执行原理?有哪些动态 sql?
16、Xml 映射文件中,除了常见的 select|insert|updae|delete标签之外,还有哪些标签?
17、Mybatis 的 Xml 映射文件中,不同的 Xml 映射文件,id 是否可以重复?
18、为什么说 Mybatis 是半自动 ORM 映射工具?它与全自动的区别在哪里?
19、 一对一、一对多的关联查询 ?
<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>
20、MyBatis 实现一对一有几种方式?具体怎么操作的?
21、MyBatis 实现一对多有几种方式,怎么操作的?
22、Mybatis 是否支持延迟加载?如果支持,它的实现原理是什么?
23、Mybatis 的一级、二级缓存:
24、什么是 MyBatis 的接口绑定?有哪些实现方式?
25、使用 MyBatis 的 mapper 接口调用时有哪些要求?
26、Mapper 编写有哪几种方式?
第一种:接口实现类继承 SqlSessionDaoSupport:使用此种方法需要编写
mapper 接口,mapper 接口实现类、mapper.xml 文件。
1、在 sqlMapConfig.xml 中配置 mapper.xml 的位置
<mappers>
<mapper resource="mapper.xml 文件的地址" />
<mapper resource="mapper.xml 文件的地址" />
</mappers>
<bean id=" " class="mapper 接口的实现">
<property name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
</bean>
第二种:使用 org.mybatis.spring.mapper.MapperFactoryBean:
<mappers>
<mapper resource="mapper.xml 文件的地址" />
<mapper resource="mapper.xml 文件的地址" />
</mappers>
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper 接口地址" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
第三种:使用 mapper 扫描器:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper 接口包地址
"></property>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
</bean>
27、简述 Mybatis 的插件运行原理,以及如何编写一个插件。
最后:
有关于mybatis的面试详解就到这里了,有机会在后面的备战春招系列专题里面会给大家分享微服务,ZooKeeper ,Dubbo,kafka 等面试专题。所有的专题我也总结到一个pdf文档了
整个系列大概有1000道面试真题详解,需要源文件可关注微信公众号:Java程序员聚集地。