备忘:SpringBoot集成Mybatis(多数据源)
适用场景:
1.需要通过代码方式配置多数据源
2.配置文件中的key需要自定义时
版本对照表:
名称 版本
2.X
mybatis-spring-boot-starter
1.3X
1.Gradle构建脚本配置:
compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
2.mybatis配置
import org.apache.ibatis.session.Configuration;import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
@SpringBootConfiguration
@Order(value = 1)
public class MybatisConfig {
@Bean(name = "mybatisConfiguration")
@Scope("prototype")//方便多数据源使用
@ConfigurationProperties("mybatis.configuration")
public Configuration configuration() {
return new Configuration();
}
}
3.application.properties 配置
# ----------------------------------------# Mybatis Configuration
# ----------------------------------------
mybatis.configuration.cache-enabled=false
mybatis.configuration.lazy-loading-enabled=true
mybatis.configuration.multiple-result-sets-enabled=true
mybatis.configuration.use-column-label=true
mybatis.configuration.use-generated-keys=false
mybatis.configuration.auto-mapping-behavior=PARTIAL
mybatis.configuration.auto-mapping-unknown-column-behavior=WARNING
mybatis.configuration.default-executor-type=REUSE
#mybatis.configuration.default-statement-timeout=100
mybatis.configuration.default-fetch-size=10
mybatis.configuration.safe-row-bounds-enabled=false
mybatis.configuration.map-underscore-to-camel-case=false
mybatis.configuration.local-cache-scope=STATEMENT
mybatis.configuration.jdbc-type-for-null=OTHER
mybatis.configuration.lazy-load-trigger-methods=equals,clone,hashCode,toString
mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
4.应用启动配置
@EnableApolloConfig@SpringBootApplication(exclude = {MybatisAutoConfiguration.class})
public class MainApplication {
public static void main(String[] args) {
EnvConfig.initApollo();
SpringApplication.run(MainApplication.class, args).addApplicationListener(new ApplicationStartup());
}
}
各个配置的详细说明请参考:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
或从源码配置文件中查看详细配置
以上是 备忘:SpringBoot集成Mybatis(多数据源) 的全部内容, 来源链接: utcz.com/z/510547.html