<div class="article">

				<p>通过@<a name="baidusnap0"></a><b style="color:black;background-color:#ffff66">Configuration</b>使用MyBatis配置类的资料比较少,大部分都是通过XML的形式。找了好久,最终还是通过官方的文档找到了解决方法:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/</p>

Using a ConfigurationCustomizer

The MyBatis-Spring-Boot-Starter provide opportunity to customize a MyBatis configuration generated by auto-configuration using Java Config. The MyBatis-Spring-Boot-Starter will search beans that implements the ConfigurationCustomizer interface by automatically, and call a method that customize a MyBatis configuration. (Available since 1.2.1 or above)

For example:

// @Configuration class
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
// customize ...
}
};
}

但是如果你用了MyBatisPlus的话,以上代码就可能会失效,打断点也进不来。我是看了近半小时的源代码才偶然发现问题所在。MybatisPlusAutoConfiguration类的sqlSessionFactory方法中有一段代码:

MybatisConfiguration configuration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new MybatisConfiguration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}

我们可以看到这段代码标红的地方就是通过ConfigurationCustomizer获取自定义配置,初看这代码也没什么问题,但是为什么自己写的mybatisConfigurationCustomizer会失效呢?关键就在ConfigurationCustomizer,这里引用的是MyBatisPlus自定义的一个和MyBatis同名的接口,com.baomidou.mybatisplus.spring.boot.starter.ConfigurationCustomizer,因此必须使用MyBatisPlus的ConfigurationCustomizer才行,修改后代码:

import org.apache.ibatis.type.JdbcType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.spring.boot.starter.ConfigurationCustomizer; @Configuration

public class MyBatisPlusConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.<b style="color:black;background-color:#ffff66">Configuration</b> <b style="color:black;background-color:#ffff66">configuration</b>) {
<b style="color:black;background-color:#ffff66">configuration</b>.setCacheEnabled(true);
<b style="color:black;background-color:#ffff66">configuration</b>.setMapUnderscoreToCamelCase(true);
<b style="color:black;background-color:#ffff66">configuration</b>.setCallSettersOnNulls(true);
<b style="color:black;background-color:#ffff66">configuration</b>.setJdbcTypeForNull(JdbcType.NULL);
}
};
}

}

完美运行。

原文地址:http://www.yyjjssnn.cn/articles/839.html

MyBatisPlus环境下使用MyBatis的配置类的更多相关文章

  1. GITHUB个人博客搭建-Pelican 在Windows环境下的安装及配置

    GITHUB个人博客搭建-Pelican 在Windows环境下的安装及配置 前言 此篇博客主要为Pelican在Windows平台下的配置安装所写,在此过程中主要参考资料烟雨林博客.poem_of_ ...

  2. libCURL开源库在VS2010环境下编译安装,配置详解

    libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...

  3. 联想电脑win7旗舰版环境下的如何成功配置AppServ

    联想电脑win7旗舰版环境下的如何成功配置AppServ 毕业设计中需要用Mysql数据库,并且想找一个方便Mysql数据库编程的开发工具,百度搜索了一下,AppServ集成环境安装包能快速搭建环境. ...

  4. 实验七:Xen环境下cirrOS的安装配置

    实验名称: Xen环境下cirrOS的安装配置 实验环境: 这里的cirrOS和实验六中的busybox的启动方式相同,唯一的区别就是我们使用的cirrOS镜像中,已经包含了根文件系统.内核文件以及r ...

  5. 【转】mysql8.0 在window环境下的部署与配置

    [转]mysql8.0 在window环境下的部署与配置 今天在阿里云window服务器上配置mysql环境,踩了一些坑,分享出来.需要的朋友可以看看.额,或许有人要吐槽我为什么不在linux上去配置 ...

  6. Linux环境下NodeJS的安装配置(HelloWorld)

    Linux环境下NodeJS的安装配置(HelloWorld) 最简单的环境安装,测试helloworld.给初学者!! 安装脚本,请仔细阅读逐行执行: #!/bin/bash #检查是否已经安装 r ...

  7. Mac 环境下svn服务器的配置

    Mac 环境下svn服务器的配置 本文目录 • 一.创建代码仓库,用来存储客户端所上传的代码 • 二.配置svn的用户权限 • 三.使用svn客户端功能 在Windows环境中,我们一般使用Torto ...

  8. Java入门——在Linux环境下安装JDK并配置环境变量

    Java入门——在Linux环境下安装JDK并配置环境变量 摘要:本文主要说明在Linux环境下JDK的安装,以及安装完成之后环境变量的配置. 使用已下载的压缩包进行安装 下载并解压 在Java的官网 ...

  9. aarch64环境下,搭建并配置服务器tomcat:

    aarch64环境下,搭建并配置服务器tomcat: 环境说明及下载相关文件: 1. ARM环境:aarch64开发板 2.JDK安装包: jdk-8u231-linux-arm64-vfp-hflt ...

随机推荐

  1. linux 清空history以及记录原理

    1.当前session执行的命令,放置缓存中,执行exit时,把缓存信息写入~/.bash_history 2.当session直接被kill时,缓存中的历史命令不会写入~/.bash_history ...

  2. python之tkinter使用-单级菜单

    # 菜单功能说明:单级菜单 import tkinter as tk root = tk.Tk() root.title('菜单选择') root.geometry('200x60') # 设置窗口大 ...

  3. 深度学习网络压缩模型方法总结(model compression)

    两派 1. 新的卷机计算方法 这种是直接提出新的卷机计算方式,从而减少参数,达到压缩模型的效果,例如SqueezedNet,mobileNet SqueezeNet: AlexNet-level ac ...

  4. codeforces279B

    Books CodeForces - 279B When Valera has got some free time, he goes to the library to read some book ...

  5. MyBatis在表名作为参数时遇到的问题

    之前在用MyBatis的时候没用过表名作为参数,最近使用到了. 基于注释使用MyBatis的Dao层代码如下: @Repository public interface Base1102Dao { @ ...

  6. QAU 18校赛 J题 天平(01背包 判断能否装满)

    问题 J: 天平 时间限制: 1 Sec  内存限制: 128 MB提交: 36  解决: 9[提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n个重 ...

  7. Leetcode 20.有效的括号 By Python

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...

  8. 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列

    前言 前面学习了 RabbitMQ 基础,现在主要记录下学习 Spring Boot 整合 RabbitMQ ,调用它的 API ,以及中间使用的相关功能的记录. 相关的可以去我的博客/RabbitM ...

  9. eclipse中用maven创建web项目

    上一节中完成了本地的maven环境搭建,在eclipse中怎么创建一个maven项目呢 一.eclipse中配置maven环境 eclipse版本4.3 1.打开菜单Help->Eclipse ...

  10. 51nod 1462 树据结构 | 树链剖分 矩阵乘法

    题目链接 51nod 1462 题目描述 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d o = ...