最近刚接触Spring boot,正是因为他的及简配置方便开发,促使我下定决心要用它把之前写的项目重构,那么问题来了,spring boot怎么整合mybatis呢,下面几个配置类来搞定。
在我的代码当中是实现了数据库读写分离的,所以代码仅做参考,如有需要可以加我微信:benyzhous
【后续更新】
1、文件结构
用来获取数据库连接配置信息,配置从中读取
也就是MyBatis配置核心入口,构建连接创建SqlSessionFactory
2、下面直接贴代码,有问题的话可以留言或者加我的微信公众号:cha-baba,或者个人微信号:benyzhous
相关配置
# Server settings
server:
port:8080
address:localhost
# DATASOURCE
jdbc:
driverClass:
url: jdbc:mysql://127.0.0.1:3306/local-kaishustory?useUnicode=true&characterEncoding=utf-8
username: root
password: root
# SPRING PROFILES
spring:
# HTTP ENCODING
http:
: UTF-8
: true
: true
# WeiXin Configuration
weixin:
mp:
appid: xx
secret: ee
token: weixin
aeskey:
# MyBatis
mybatis:
typeAliasesPackage: .**.domain
mapperLocations: classpath:/com/modou/**/mapper/*.xml
configLocation: classpath:/
# LOGGING
logging:
level:
:DEBUG
package ;
import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Configuration
@EnableTransactionManagement
public class DataBaseConfiguration implements EnvironmentAware {
private RelaxedPropertyResolver propertyResolver;
private static Logger log = ();
@Override
public void setEnvironment(Environment env) {
= new RelaxedPropertyResolver(env, "jdbc.");
}
@Bean(name="writeDataSource", destroyMethod = "close", initMethod="init")
@Primary
public DataSource writeDataSource() {
("Configruing Write DataSource");
DruidDataSource datasource = new DruidDataSource();
(("url"));
(("driverClassName"));
(("username"));
(("password"));
return datasource;
}
@Bean(name="readOneDataSource", destroyMethod = "close", initMethod="init")
public DataSource readOneDataSource() {
("Configruing Read One DataSource");
DruidDataSource datasource = new DruidDataSource();
(("url"));
(("driverClassName"));
(("username"));
(("password"));
return datasource;
}
@Bean(name="readTowDataSource", destroyMethod = "close", initMethod="init")
public DataSource readTowDataSource() {
("Configruing Read Two DataSource");
DruidDataSource datasource = new DruidDataSource();
(("url"));
(("driverClassName"));
(("username"));
(("password"));
return datasource;
}
@Bean(name="readDataSources")
public List<DataSource> readDataSources(){
List<DataSource> dataSources = new ArrayList<DataSource>();
(readOneDataSource());
(readTowDataSource());
return dataSources;
}
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
*
* 获取第二个数据库的连接信息,在中配置,并指定特定的前缀
*
*/
@Configuration
@ConditionalOnClass({ , })
@AutoConfigureAfter({ })
@MapperScan(basePackages={".**.mapper","."})
public class MybatisConfiguration implements EnvironmentAware{
private static Log logger = ();
private RelaxedPropertyResolver propertyResolver;
@Resource(name="writeDataSource")
private DataSource writeDataSource;
@Resource(name="readDataSources")
private List<Object> readDataSources;
@Override
public void setEnvironment(Environment environment) {
= new RelaxedPropertyResolver(environment,"mybatis.");
}
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory() {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
(roundRobinDataSouceProxy());
(propertyResolver
.getProperty("typeAliasesPackage"));
sessionFactory
.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(propertyResolver
.getProperty("mapperLocations")));
sessionFactory
.setConfigLocation(new DefaultResourceLoader()
.getResource(propertyResolver
.getProperty("configLocation")));
return ();
} catch (Exception e) {
("Could not confiure mybatis session factory");
return null;
}
}
@Bean
public RoundRobinRWRoutingDataSourceProxy roundRobinDataSouceProxy(){
RoundRobinRWRoutingDataSourceProxy proxy = new RoundRobinRWRoutingDataSourceProxy();
(writeDataSource);
(readDataSources);
("READ");
("WRITE");
return proxy;
}
@Bean
@ConditionalOnMissingBean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(writeDataSource);
}
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Created by chababa on 15/8/22.
*/
@Configuration
@ComponentScan(basePackages={"",""})
@EnableAutoConfiguration
public class Application implements CommandLineRunner{
@Autowired
HelloWorldService helloWorldService;
public static void main(String[] args) {
(, args);
}
@Override
public void run(String... args) throws Exception {
(());
}
}
3、maven 相关依赖[我是基于我的多模块依赖,这里只是一个示意],其中配置了jrebel热部署插件,需要搭配jrebel6.2.1,具体配置和下载请转向 /xiaoyu411502/article/details/48047369
<?xml version="1.0"?>
<project
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."
xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId></groupId>
<artifactId>weixin-boot-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../weixin-boot-parent</relativePath>
</parent>
<artifactId>weixin-boot-services</artifactId>
<name>weixin-boot-services</name>
<url></url>
<properties>
<>UTF-8</>
<>1.2.</>
</properties>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>weixin-boot-sdk</artifactId>
<version>${}</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-plugin-rw</artifactId>
<version>${}</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>persistence-api</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mapper</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-generator-core</artifactId>
</dependency>
</dependencies>
</project>