MyBatis 概览
文章目录
一)简介
1.1 是什么
一款优秀的持久层框架,支持自定义SQL、存储过程和高级映射
1.2 xml构建
1.2.1 配置文件
- 数据源
- 事务管理器
- mapper位置
- …等等
1.2.2 mapper配置
xml
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
注解
package org.mybatis.example;
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
1.2.3 代码层面
// 1. 获取SqlSessionFactory
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
// 2. 获取SqlSession
// 3. 执行SQL:普通方式(SQL的标识符)、映射
try (SqlSession session = sqlSessionFactory.openSession()) {
Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
}
1.3 名词解析
命名空间
作用域和生命周期
对象 | 作用 | 作用域 | 安全 |
---|---|---|---|
SqlSessionFactoryBuilder | 创建sqlSessionFactory | 建议局部方法变量 | |
SqlSessionFactory | 建议应用作用域 | 线程安全 | |
SqlSession | 建议请求或方法作用域 | 线程不安全 |
二)主要配置项
属性
properties。
-
定义:xml、
SqlSessionFactoryBuilder.build(..)
-
使用
-
加载顺序及优先级
加载顺序:properties元素体内 properties元素的resource属性——配置文件,或url属性指定的路径 方法参数传递,如上面build(..) 优先级 降序:方法参数,配置文件,properties元素
-
默认值
设置
settings。
- 缓存开关
- 延迟加载
- 自动生成主键
- 执行器
- 超时时间
- 分页开关
- 结果处理器开关
- 嵌套的结果处理器开关
- 驼峰命名自动映射开关
- …等等
类型别名
typeAliases。
仅用于xml配置,为降低冗余的全限定名书写
类型处理器
typeHandlers。
Java类型与数据库类型的映射关系
对象工厂
objectFactory。
插件
plugins。
环境配置
environments。
- 多数据源(每个factory只能选择一种)
- 事务管理器
数据库厂商标识
databaseIdProvider。
映射器
mappers。
三)映射器
3.1 缓存配置
默认情况下,只启用了本地的会话缓存
一级缓存
一级缓存,即本地会话缓存——SqlSession
(全局)二级缓存
二级缓存,即全局缓存——SqlSessionFactory
3.2 增删查改
3.3 可重用语句块——sql
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>,
<include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>
<sql id="sometable">
${prefix}Table
</sql>
<sql id="someinclude">
from
<include refid="${include_target}"/>
</sql>
<select id="select" resultType="map">
select
field1, field2, field3
<include refid="someinclude">
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>
3.4 动态SQL
- if
- choose(when, otherwise)
- trim(where, set)
- foreach
3.5 resultMap
其他
-
crud的属性
-
resultMap的属性
- 高级结果映射:mybatis.org
- 嵌套查询
- 嵌套结果
- 集合嵌套
-
存储过程
- 多结果集
-
#和$
#,占位符,安全设置参数 $,不转义字符串直接替换
四)执行器
Simple——Statement
普通执行器
Reuse——PreparedStatement
- 重用预处理语句
Batch
- 重用语句
- 批量更新
五)常见面试题
-
定义:自定义SQL、存储过程、高级映射——持久层框架
-
缓存:一级本地——会话,二级全局——命名空间
-
分页:
-
实现:使用
RowBounds
对象、编写sql
、MyBatis
的分页插件 -
分页插件原理:实现
MyBatis
接口,实现自定义插件。在插件的拦截方法内拦截待执行sql,然后重写sqlsql:select * from student; 拦截重写后:select t.* from (select * from student) t limit 0,10
-
-
动态sql
- 有哪些:if、choose(when, otherwise)、trim(where, set)、foreach、bind
- 执行原理:使用ongl,从sql参数对象中计算表达式的值,根据值动态拼接sql
-
#和KaTeX parse error: Expected 'EOF', got '#' at position 2: :#̲预编译处理,字符串替换(不转义)
- #,更安全,可预防sql注入
-
延迟加载
- 支持与否:仅支持association关联对象和collection关联集合对象的延迟加载
- 实现原理:使用cglib创建目标对象的代理对象,当调用目标方法时,拦截,查询并设置值,返回
-
关联
- 一对一:联合查询、嵌套查询
- 一对多:嵌套查询
-
执行器
- simple:
Statement
,用完关闭 - reuse:以sql查找
Statement
,存在就使用,不存在就创建,用完后不关闭 - batch:批处理,重用
- simple: