MyBatis 概览

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

  • 重用语句
  • 批量更新

五)常见面试题

  1. 定义:自定义SQL、存储过程、高级映射——持久层框架

  2. 缓存:一级本地——会话,二级全局——命名空间

  3. 分页:

    1. 实现:使用RowBounds对象、编写sqlMyBatis的分页插件

    2. 分页插件原理:实现MyBatis接口,实现自定义插件。在插件的拦截方法内拦截待执行sql,然后重写sql

      sql:select * from student;
      拦截重写后:select t.* from (select * from student) t limit 0,10
      
  4. 动态sql

    1. 有哪些:if、choose(when, otherwise)、trim(where, set)、foreach、bind
    2. 执行原理:使用ongl,从sql参数对象中计算表达式的值,根据值动态拼接sql
  5. #和KaTeX parse error: Expected 'EOF', got '#' at position 2: :#̲预编译处理,字符串替换(不转义)

    1. #,更安全,可预防sql注入
  6. 延迟加载

    1. 支持与否:仅支持association关联对象和collection关联集合对象的延迟加载
    2. 实现原理:使用cglib创建目标对象的代理对象,当调用目标方法时,拦截,查询并设置值,返回
  7. 关联

    1. 一对一:联合查询、嵌套查询
    2. 一对多:嵌套查询
  8. 执行器

    1. simple:Statement,用完关闭
    2. reuse:以sql查找Statement,存在就使用,不存在就创建,用完后不关闭
    3. batch:批处理,重用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值