MyBatis编程式开发


MyBatis编程式开发步骤
  1. MyBatis和MySQL Jar包依赖
  2. 全局配置文件mybatis-config.xml
  3. 映射器Mapper.xml
  4. Mapper接口

编程式开发伪代码
    @Test
public void show() {
SqlSession session = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
Student student = session.selectOne("com.fly.crud.mapper.StudentMapper.selectDataById", 1);
log.info("student jsonStr is {}", JSON.toJSONString(student));
} catch (Exception e) {
log.error("MyBatis 编程式开发异常.", e);
} finally {
if (Objects.nonNull(session)) {
session.close();
}
}
}

MyBatis编程式开发核心对象
  1. SqlSessionFactoryBuilder
  2. SqlSessionFactory
  3. SqlSession
  4. Mapper

MyBatis编程式开发核心对象的生命周期
对象 生命周期
SqlSessionFactoryBuilder 方法局部(method)
SqlSessionFactory(Singleton) 应用级别(application)
SqlSession 请求和操作
Mapper 方法局部

MyBatis核心配置
标签 作用
configuration MyBatis根标签
properties mybatis全局属性
settings mybatis设置
typeAliases mybatis类型别名
typeHadlers mybatis类型处理器
objectFactory mybatis对象工程
plugins mybatis插件
environments mybatis环境变量
databaseIdProvider mybatis数据库厂商标识
mappers mybatis映射器

settings 默认源码解读
private void settingsElement(Properties props) {
// 指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示关闭自动映射;PARTIAL 只会自动映射没有定义嵌套结果映射的字段。 FULL 会自动映射任何复杂的结果集(无论是否嵌套)
configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
/**
* 指定发现自动映射目标未知列(或未知属性类型)的行为。
* NONE: 不做任何反应
* WARNING: 输出警告日志('org.apache.ibatis.session.AutoMappingUnknownColumnBehavior' 的日志等级必须设置为 WARN)
* FAILING: 映射失败 (抛出 SqlSessionException)
*/
configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
// 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存
configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
// 指定 Mybatis 创建可延迟加载对象所用到的代理工具。
configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
// 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。
configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
// 开启时,任一方法的调用都会加载该对象的所有延迟加载属性。 否则,每个延迟加载属性会按需加载(参考 lazyLoadTriggerMethods)。
configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
// 是否允许单个语句返回多结果集(需要数据库驱动支持)
configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
// 使用列标签代替列名。实际表现依赖于数据库驱动,具体可参考数据库驱动的相关文档,或通过对比测试来观察
configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
// 允许 JDBC 支持自动生成主键,需要数据库驱动支持。如果设置为 true,将强制使用自动生成主键。尽管一些数据库驱动不支持此特性,但仍可正常工作(如 Derby)
configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
// 配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(PreparedStatement); BATCH 执行器不仅重用语句还会执行批量更新。
configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
// 设置超时时间,它决定数据库驱动等待数据库响应的秒数。
configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
// 为驱动的结果集获取数量(fetchSize)设置一个建议值。此参数只可以在查询设置中被覆盖。
configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
// 指定语句默认的滚动策略。(新增于 3.5.2)
configuration.setDefaultResultSetType(resolveResultSetType(props.getProperty("defaultResultSetType")));
// 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
// 是否允许在嵌套语句中使用分页(RowBounds)。如果允许使用则设置为 false。
configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
/** MyBatis 利用本地缓存机制(Local Cache)防止循环引用和加速重复的嵌套查询。
* 默认值为 SESSION,会缓存一个会话中执行的所有查询。
* 若设置值为 STATEMENT,本地缓存将仅用于执行语句,对相同 SqlSession 的不同查询将不会进行缓存。
*/
configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
// 当没有为参数指定特定的 JDBC 类型时,空值的默认 JDBC 类型。 某些数据库驱动需要指定列的 JDBC 类型,多数情况直接用一般类型即可,比如 NULL、VARCHAR 或 OTHER。
configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
// 指定对象的哪些方法触发一次延迟加载
configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
// 是否允许在嵌套语句中使用结果处理器(ResultHandler)。如果允许使用则设置为 false。
configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
// 指定动态 SQL 生成使用的默认脚本语言。
configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
// 指定 Enum 使用的默认 TypeHandler 。(新增于 3.4.5)
configuration.setDefaultEnumTypeHandler(resolveClass(props.getProperty("defaultEnumTypeHandler")));
// 指定当结果集中值为 null 的时候是否调用映射对象的 setter(map 对象时为 put)方法,这在依赖于 Map.keySet() 或 null 值进行初始化时比较有用。注意基本类型(int、boolean 等)是不能设置成 null 的。
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
// 允许使用方法签名中的名称作为语句参数名称。 为了使用该特性,你的项目必须采用 Java 8 编译,并且加上 -parameters 选项。(新增于 3.4.1)
configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
// 当返回行的所有列都是空时,MyBatis默认返回 null。 当开启这个设置时,MyBatis会返回一个空实例。 请注意,它也适用于嵌套的结果集(如集合或关联)。(新增于 3.4.2)
configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
// 指定 MyBatis 增加到日志名称的前缀。
configuration.setLogPrefix(props.getProperty("logPrefix"));
/**
* 指定一个提供 Configuration 实例的类。 这个被返回的 Configuration 实例用来加载被反序列化对象的延迟加载属性值。
* 这个类必须包含一个签名为static Configuration getConfiguration() 的方法。(新增于 3.2.3)
*/
configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
}

MyBatis 最佳实践


批量插入伪代码
        SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = new ArrayList<>(16);
for (int i = 0; i < 5; i++) {
int hashCodeV = UUID.randomUUID().toString().hashCode();
hashCodeV = hashCodeV>0?hashCodeV:-hashCodeV;
Student student = new Student("滕飞"+i,String.valueOf(hashCodeV));
studentList.add(student);
}
mapper.insertDataBatch(studentList);
sqlSession.commit();
} catch (IOException e) {
log.error("StudentMapper insertDataBatch fail.", e);
} finally {
if (Objects.nonNull(sqlSession)) {
sqlSession.close();
}
}

批量查询伪代码
        SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Integer> ids = Arrays.asList(1,12,13,14,15);
List<Student> students = mapper.selectDataByIdBatch(ids);
log.info("students jsonStr is {}",JSON.toJSONString(students));
} catch (IOException e) {
log.error("StudentMapper insertDataBatch fail.", e);
} finally {
if (Objects.nonNull(sqlSession)) {
sqlSession.close();
}
}

延迟加载伪代码
        SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
StaffAndDepartment staffAndDepartment = mapper.findStaffWithDepartmentQuery(1);
System.out.println("-----------:"+staffAndDepartment.getClass());
// 如果开启了延迟加载,会在使用的时候才发出SQL
// equals,clone,hashCode,toString也会触发延迟加载
// System.out.println("-----------调用toString方法:"+blog);
// System.out.println("-----------getAuthor:"+staffAndDepartment.toString());
// 如果 aggressiveLazyLoading = true ,也会触发加载,否则不会
System.out.println("-----------getName:"+staffAndDepartment.getName());
} catch (IOException e) {
log.error("系统异常.", e);
} finally {
if (Objects.nonNull(sqlSession)) {
sqlSession.close();
}
}

翻页
  • 逻辑翻页和物理翻页区别

    • 逻辑分页-假分页:将数据全部加载到内存,根据rowBound里面的偏移量,和条数,将偏移量之前的都去掉,只去从偏移量开始后指定数量的那一部分;
    • 物理分页- 真分页:将分页信息硬编码到sql语句中,到库里直接查询需要的那部分数据
逻辑翻页伪代码
        SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
int start = 0; // offset
int pageSize = 5; // limit
RowBounds rowBounds = new RowBounds(start, pageSize);
List<Staff> staffs = mapper.findStaffAll(rowBounds);
log.info("staffs jsonStr is {}", JSON.toJSONString(staffs));
} catch (IOException e) {
log.error("系统异常.", e);
} finally {
if (Objects.nonNull(sqlSession)) {
sqlSession.close();
}
}
物理翻页伪代码
/**
* mapper 配置
**/
@Select("select name from staff limit #{start},#{end}")
List<Staff> findStaffPhysical(@Param("start") int start, @Param("end") int end); SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
List<Staff> staffs = mapper.findStaffPhysical(0, 5);
log.info("staffs jsonStr is {}", JSON.toJSONString(staffs));
} catch (IOException e) {
log.error("系统异常.", e);
} finally {
if (Objects.nonNull(sqlSession)) {
sqlSession.close();
}
}

应用分析与最佳实践章节就总结到这儿,下一个章节写MyBatis体系结构与工作原理。欲知后事如何且听下回分解。

【MyBatis深入剖析】应用分析与最佳实践(下)的更多相关文章

  1. Guava Cache 原理分析与最佳实践

    前言 目前大部分互联网架构 Cache 已经成为了必可不少的一环.常用的方案有大家熟知的 NoSQL 数据库(Redis.Memcached),也有大量的进程内缓存比如 EhCache .Guava ...

  2. 【MyBatis深入剖析】应用分析与最佳实践

    ##### 文章目标1. 了解ORM框架的发展历史,了解MyBatis特性2. 掌握MyBatis编程式开发方法和核心对象3. 掌握MyBatis核心配置含义4. 掌握MyBatis的高级用法与扩展方 ...

  3. mybatis 3.x源码深度解析与最佳实践(最完整原创)

    mybatis 3.x源码深度解析与最佳实践 1 环境准备 1.1 mybatis介绍以及框架源码的学习目标 1.2 本系列源码解析的方式 1.3 环境搭建 1.4 从Hello World开始 2 ...

  4. java-mybaits-015-mybatis逆向工程最佳实践【基础mybatis-generator、tk.mybatis、mubatis-plus】

    一.概述 三款框架的功能对比 Mybatis-generator 通用Mapper Mybatis-Plus 代码生成器 支持自动生成Model,Mapper,Mapper XML文件 生成方式不够灵 ...

  5. 《AngularJS深度剖析与最佳实践》简介

    由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...

  6. paip.前端加载时间分析之道优化最佳实践

    paip.前端加载时间分析之道优化最佳实践 1.另存为 ,查看文件尺寸..和图片. 2.view the 另存为的htm静态的文件单个的加载,看时间...可以排除编程语言的问题and 数据库.. ## ...

  7. Bug预防体系(上千bug分析后总结的最佳实践)

    Bug预防体系(上千bug分析后总结的最佳实践) 原创 2017-08-16俞美玲 光荣之路 吴老的<selenium webdriver 实战宝典>出版了!  web常见产品问题及预防 ...

  8. 30分钟带你了解Springboot与Mybatis整合最佳实践

    前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...

  9. JavaScript Web 应用最佳实践分析

    [编者按]本文作者为 Mathias Schäfer,旨在回顾在客户端大量使用JavaScript 的最佳 Web应用实践.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 对笔者来说,Jav ...

随机推荐

  1. 面试指南」JS 模块化、组件化、工程化相关的 15 道面试题

    JS 模块化.组件化.工程化相关的 15 道面试题 1.什么是模块化? 2.简述模块化的发展历程? 3.AMD.CMD.CommonJS 与 ES6 模块化的区别? 4.它们是如何使用的? 5.exp ...

  2. Selenium系列(十六) - Web UI 自动化基础实战(3)

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  3. Java构造器(构造方法/constructor)

    我们先来看一下什么是构造器: 1.构造器也叫构造方法或构造函数,分为有参构造器和无参构造器: 2.构造器也是一种方法,只不过是一种特殊的方法,它会在对象创建的时候被调用: 3.构造器最大的作用就是在创 ...

  4. spring5之容器始末源码赏析 (一)总览

    首先,本系列并不是以介绍spring5 的新特性为主,之所以以spring5为标题,是因为即将赏析的源码来自最新的spring版本.虽说是spring最新版本,但是容器的整个生命周期与之前版本相比,并 ...

  5. ThreadAbortException是可以传递的

    今天在写线程Aborted代码时,发现嵌套的try catch中的ThreadAbortException错误是可以从内部传递到外部的,想想这也是必然的,在内部该线程已经中断了,外部必然是中断了,再仔 ...

  6. Ubuntu添加新用户并给普通用户赋予root新权限

    添加新用户 首先用adduser命令添加普通用户: #adduser newusername 只有在root权限才可以添加新用户 修改密码: #passwd username 赋予root权限 方法1 ...

  7. Markdown语法快速学习

    Markdown 简洁语法说明 0.前言 一直以来都是以word文档做笔记,存在很多问题,比如代码格式.高亮等.这次公司要求使用markdown,感觉眼前一亮,以前word的问题都得到了解决,而且可以 ...

  8. JS 剑指Offer(六) 用两个栈实现队列

    题目:用两个栈实现队列,实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入整数和在队列头部删除整数,若队列中没有元素deleteHead返回-1 分析:在队列的尾部插入 ...

  9. C# 快速开发框架搭建—环境搭建

    一.新建MVC项目 打开vs2013新建空的解决方案,在解决方案中增加一个MVC项目,如图: 删除不需要的文件,剩下如图所示的文件夹: 首先创建一个MVC5控制器(Login,登入使用),该控制器无需 ...

  10. P3381 【模板】最小费用最大流 题解

    CSDN同步 原题链接 前置知识: 从三种算法剖析网络流本质 简要题意: 给定网络图,求图的最大流,以及流量为最大流时的最小费用. 现在假设你们看了那篇网络流博客之后,所有人都会了 \(\text{E ...