MyBatisPlus环境下使用MyBatis的配置类
<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的配置类的更多相关文章
- GITHUB个人博客搭建-Pelican 在Windows环境下的安装及配置
GITHUB个人博客搭建-Pelican 在Windows环境下的安装及配置 前言 此篇博客主要为Pelican在Windows平台下的配置安装所写,在此过程中主要参考资料烟雨林博客.poem_of_ ...
- libCURL开源库在VS2010环境下编译安装,配置详解
libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...
- 联想电脑win7旗舰版环境下的如何成功配置AppServ
联想电脑win7旗舰版环境下的如何成功配置AppServ 毕业设计中需要用Mysql数据库,并且想找一个方便Mysql数据库编程的开发工具,百度搜索了一下,AppServ集成环境安装包能快速搭建环境. ...
- 实验七:Xen环境下cirrOS的安装配置
实验名称: Xen环境下cirrOS的安装配置 实验环境: 这里的cirrOS和实验六中的busybox的启动方式相同,唯一的区别就是我们使用的cirrOS镜像中,已经包含了根文件系统.内核文件以及r ...
- 【转】mysql8.0 在window环境下的部署与配置
[转]mysql8.0 在window环境下的部署与配置 今天在阿里云window服务器上配置mysql环境,踩了一些坑,分享出来.需要的朋友可以看看.额,或许有人要吐槽我为什么不在linux上去配置 ...
- Linux环境下NodeJS的安装配置(HelloWorld)
Linux环境下NodeJS的安装配置(HelloWorld) 最简单的环境安装,测试helloworld.给初学者!! 安装脚本,请仔细阅读逐行执行: #!/bin/bash #检查是否已经安装 r ...
- Mac 环境下svn服务器的配置
Mac 环境下svn服务器的配置 本文目录 • 一.创建代码仓库,用来存储客户端所上传的代码 • 二.配置svn的用户权限 • 三.使用svn客户端功能 在Windows环境中,我们一般使用Torto ...
- Java入门——在Linux环境下安装JDK并配置环境变量
Java入门——在Linux环境下安装JDK并配置环境变量 摘要:本文主要说明在Linux环境下JDK的安装,以及安装完成之后环境变量的配置. 使用已下载的压缩包进行安装 下载并解压 在Java的官网 ...
- aarch64环境下,搭建并配置服务器tomcat:
aarch64环境下,搭建并配置服务器tomcat: 环境说明及下载相关文件: 1. ARM环境:aarch64开发板 2.JDK安装包: jdk-8u231-linux-arm64-vfp-hflt ...
随机推荐
- HDU 2012 素数判定
http://acm.hdu.edu.cn/showproblem.php?pid=2012 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括 ...
- [转帖] linux下面 vim 数字键无法插入的解决办法
感谢原作者: https://blog.csdn.net/guoyuqi0554/article/details/11477597 这个问题困扰自己好久了.. 刚才解决了 rlwrap的问题 这会儿 ...
- children和childNodes 的区别
1.childNodes 属性,标准的,它返回指定元素的子元素集合,包括html节点,所有属性,文本.可以通过nodeType来判断是哪种类型的节点,只有当nodeType==时才是元素节点,是属性节 ...
- linux 十个命令
http://www.roncoo.com/article/detail/124514
- CSS变形transform(2d)
前面的话 CSS变形transform是一些效果的集合,主要是移动.旋转.缩放和倾斜这四种基本操作,还可以通过设置matrix矩阵来实现更复杂的效果.变形transform可以实现2D和3D两种效果. ...
- Apache虚拟主机
基本概述 虚拟Web主机 在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机 httpd支持的虚拟主机类型 基于域名的虚拟主机 基于IP地址的虚拟主机 基于端口的虚拟主机 ...
- python 模块之-os
python os模块 import os os.getcwd() # python脚本所在工作目录的路径 os.chdir(r'/tmp') # 切换目录到tmp os. ...
- jenkins--svn+Email自动触发2(jenkins系统配置)
jenkins系统配置-SonarQube servers配置: 邮件通知设置: 邮件调试问题: 在 系统设置 --> Extended E-mail Notification: 找到 Enab ...
- BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...
- 架构师成长之路2.2-PXE+Kickstart安装部署
点击返回架构师成长之路 架构师成长之路2.2-PXE+Kickstart安装部署 系统测试环境: 实验环境:VMware Workstation 12 系统平台:CentOS Linux releas ...