程序员刘杨

———没有写不出的程序,只有不努力的程序员。

2018年10月12日   码农之路   7,791 次浏览

祝大家新年快乐,有任何问题可与我联系:点击这里给我发消息

通过@Configuration使用MyBatis配置类的资料比较少,大部分都是通过XML的形式。找了好久,最终还是通过官方的文档找到了解决方法:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

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.Configuration configuration) {
				configuration.setCacheEnabled(true);
				configuration.setMapUnderscoreToCamelCase(true);
				configuration.setCallSettersOnNulls(true);
				configuration.setJdbcTypeForNull(JdbcType.NULL);
			}
		};
	}
}

 

完美运行。

赞 赏
申明:除非注明,本站文章均为原创,转载请以链接形式标明本文地址。 如有问题,请于一周内与本站联系,本站将在第一时间对相关内容进行处理。
本文地址: http://www.yyjjssnn.cn/articles/839.html

>>> Hello World <<<

这篇内容是否帮助到你了呢?

如果你有任何疑问或有建议留给其他朋友,都可以给我留言。

:wink: :twisted: :surprised: :smile: :smile9: :smile8: :smile7: :smile6: :smile5: :smile56: :smile55: :smile54: :smile53: :smile52: :smile51: :smile50: :smile4: :smile49: :smile48: :smile47: :smile46: :smile45: :smile44: :smile43: :smile42: :smile41: :smile40: :smile3: :smile39: :smile38: :smile37: :smile36: :smile35: :smile34: :smile33: :smile32: :smile31: :smile30: :smile2: :smile29: :smile28: :smile27: :smile26: :smile25: :smile24: :smile23: :smile22: :smile21: :smile20: :smile1: :smile19: :smile18: :smile17: :smile16: :smile15: :smile14: :smile13: :smile12: :smile11: :smile10: :smile0: :sad: :rolleyes1: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow:

友情链接: 程序员刘杨 刘杨
Copyright 2003~2018 保留所有权利 | 网站地图
备案号:湘ICP备14001005号-2

湘公网安备 43011102001322号