Mybatis高频面试题(二)
接口绑定
Mybatis的接口绑定有两种方式:
-
一种是通过注解绑定,在接口方法上加@Select、@Updata等注解,注解后面跟上对应的Sql语句
sql语句比较简单的时候,可以是使用注解绑定
-
一种是通过xml里面写SQL来绑定,需要指定xml文件的namespace为接口的全限定名,id为对应的接口方法
sql语句比较复杂的时候,推荐使用xml方式进行绑定
package com.test.demo.mapper;
import org.apache.ibatis.annotations.Select;
public interface StudentMapper {
//基于注解绑定
@Select("select * from student where Sno = #{sNo}")
Student selectByNumber(Integer sNo);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis//DTD Mapper 3.0//EN"
"http://mybatis/dtd/mybatis-3-mapper.dtd">
<!--基于xml方式绑定,注意namespace和id名称正确与否-->
<mapper namespace = "com.test.demo.mapper.StudentMapper">
<select id="selectByNumber" parameterType="java.lang.Integer" resultType="com.test.demo.Student">
SELECT * FROM student where Sno = #{sNo}
</select>
</mapper>
Mybatis实现一对一的方式
以用户和订单举例,
一对一 :一个订单只属于一个用户 → \rightarrow →订单对用户是一对一关系
一个用户只能有一个订单 → \rightarrow →用户对订单是一对一关系
一对多 :一个用户可以拥有多个订单 → \rightarrow →用户对订单是一对多关系
多对多 :一个订单可以有多种商品,并且一种商品可以被多个订单包含 → \rightarrow →商品和订单是多对多关系
一对一有两种方式:联合查询和嵌套查询
-
联合查询是几个表一起进行select,只查询一次。通过在resutMap里面配置association节点,
实现相应属性对应类相应属性
<select id="selectUnionNumber" parameterType="java.lang.Integer" resultMap="unionMap"> SELECT * FROM sc, student where sc.Sno = student.Sno and sc.Cno = #{cNo} </select> <resultMap id="unionMap" type="com.test.demo.SC"> <association property="student" javaType="com.test.demo.Student"> <result property="sName" column="Sname"/> <result property="sAge" column="Sage"/> <result property="sNo" column="Sno"/> </association> </resultMap>
-
嵌套查询是先查一个表,再根据结果中的外键属性值,去另一个表查询,也是通过resultMap中association配置
不过需要指定第二次查询的查询接口,和相应属性列
<mapper namespace = "com.test.demo.mapper.SCMapper"> <select id="selectByNumber" parameterType="java.lang.Integer" resultMap="lazyMap"> SELECT * FROM sc where Cno = #{cNo} </select> <resultMap id="lazyMap" type="com.test.demo.SC"> <association property="student" javaType="com.test.demo.Student" select="com.test.demo.mapper.StudentMapper.selectByNumber" column="Sno"> </association> </resultMap> </mapper>
<select id="selectByNumber" parameterType="java.lang.Integer" resultType="com.test.demo.Student"> SELECT * FROM student where Sno = #{sNo} </select>
Mybatis分页方式
数据库分页是什么?
分页可以这样理解,一大堆数据,我们每页显示多少条数据,这样就叫分页。说个形象的,你有一本写给梦中女孩的记事本,里面密密麻麻记录了你追她的点点滴滴,现在你想做个分类,分成每1页根据时间顺序显示你追她的点点滴滴,这样每一页就比较少了,方便查看。不太恰当,囧。
链接:https://www.zhihu/question/23407956/answer/24486052
-
集合分页或者称为内存分页
在进行sql查询时一次性查找到所有数据保存在list中,再使用subList获取满足条件的记录
-
sql语句物理分页
<select id="query" parameterType="map" resultType="com.test.demo.Student"> select * from student limit #{currIndex} , #{pageSize} </select>
-
插件分页
插件原理
Mybatis 仅可以编写针对 ParameterHandler、ResultSetHandler、StatementHandler、
Executor 这 4 种接口的插件,Mybatis 通过动态代理,为需要拦截的接口生成代理对象以实
现接口方法拦截功能,每当执行这 4 种接口对象的方法时,就会进入拦截方法,具体就是
InvocationHandler 的 invoke()方法。实现
实现
Interceptor
接口,并重写intercept()方法,从而实现拦截指定方法,实现重写待执行的sql语句 的目的
举例:select *from student
拦截 sql 后重写为
select * from studnet limit 0, 10
参考文章
Mybatis自定义插件
自定义mybatis分页插件
精心整理了15道面试官喜欢问的MyBatis面试题
更多推荐
Mybatis高频面试题(二)
发布评论