spring boot 及 spring mvc 的简化配置及内容升华版。
在 spring boot 中如何整和mybatis 呢?
第一部:maven 依赖(可以在创建 spring boot 项目是勾选中)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
第二部:在application.yml 中配置 mybatis
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapper/*.xml
对就是这么简单就可以完成配置。到现在已经完全抛弃 mybatis 自己的核心配置文件了。
番外篇:mybatis 有一个增强版框架 mybatis-plus 我们也可以整和它。
第一部当然也是要添加依赖
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.2.0</version>
</dependency>
然后使用@Configuration 添加配置
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
public class MyBatisPulsConfig {
@Autowired
private DataSource dataSource;
@Autowired
private MybatisProperties properties;
@Autowired
@SuppressWarnings("unused")
private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Autowired(required = false)
private Interceptor[] interceptors;
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType("mysql");
return paginationInterceptor;
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException{
MybatisSqlSessionFactoryBean mybatisPuls = new MybatisSqlSessionFactoryBean();
mybatisPuls.setDataSource(dataSource); // 设置数据源
mybatisPuls.setVfs(SpringBootVFS.class);
// 使用 PathMatchingResourcePatternResolver 避免路径找不到
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
mybatisPuls.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
if(StringUtils.hasText(this.properties.getConfigLocation()));
if(!ObjectUtils.isEmpty(this.interceptors)) {
mybatisPuls.setPlugins(this.interceptors);
}
GlobalConfiguration globalConfig = new GlobalConfiguration();
globalConfig.setDbType(DBType.MYSQL.name()); //设置数据库类型
//使用ID_WORKER_STR,因为前后端分离使用整形,前端JS会有精度丢失
globalConfig.setIdType(IdType.ID_WORKER_STR.getKey());
globalConfig.setSqlInjector(new AutoSqlInjector());
MybatisConfiguration mc = new MybatisConfiguration();
// 对于完全自定义的mapper需要加此项配置,才能实现下划线转驼峰
mc.setMapUnderscoreToCamelCase(true);
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
mybatisPuls.setConfiguration(mc);
if(this.databaseIdProvider != null) {
mybatisPuls.setDatabaseIdProvider(this.databaseIdProvider);
}
if(StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
mybatisPuls.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if(StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
mybatisPuls.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if(!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
mybatisPuls.setMapperLocations(this.properties.resolveMapperLocations());
}
return mybatisPuls;
}
}
来源:oschina
链接:https://my.oschina.net/u/3744526/blog/3036680