整合
其实整合很简单,如果是用gradle的话,在build.gradle文件里加入
1 |
|
如果是用maven的话在pom.xml文件里加入
单库配置:
引入之后,默认情况下,Spring Boot会自动为我们配置好一个DataSource,它会在classpath中搜索H2、hsqldb等内存数据库的jar包,如果找到了,就会自动配置一个内存数据库的DataSource。
如果在application.yml或application.property
中指定了spring.datasource.*
的相关配置参数,Spring Boot就会使用该配置创建一个DataSource。
然后会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。
spring.datasource.url=jdbc:mysql:
//localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-
class
-name=com.mysql.jdbc.Driver
更多参数请查看DataSourceProperties
多库配置:
由于业务需要,项目要同时使用多个数据库进行业务开发:
首先,我们必须在application.property中自定义两个数据源的配置,一个使用first.datasource.*,另一个使用second.datasource.*,为了能使别人一眼看出连接的是什么库,可以使用数据库命名,比如user库,则可以使用user.datasource.*,在使用多数据源的时候,所有必要配置都不能省略。
first.datasource.url=jdbc:mysql:
//localhost/first
first.datasource.username=dbuser1
first.datasource.password=dbpass1
first.datasource.driver-
class
-name=com.mysql.jdbc.Driver
first.datasource.type=com.alibaba.druid.pool.DruidDataSource
//我用的是Druid,也可以不加用默认的
second.datasource.url=jdbc:mysql:
//localhost/second
second.datasource.username=dbuser2
second.datasource.password=dbpass2
second.datasource.driver-
class
-name=com.mysql.jdbc.Driver
second.datasource.type=com.alibaba.druid.pool.DruidDataSource
直接上代码,我的做法是将两个数据源用两个配置类创建:
@Configuration
@MapperScan
(basePackages = {
"com.user.server.dao"
}, sqlSessionTemplateRef =
"userSqlSessionTemplate"
)
public
class
UserMybatisConfig {
@Bean
(name =
"userDataSource"
)
@Primary
//必须加此注解,不然报错,下一个类则不需要添加
@ConfigurationProperties
(prefix =
"first.datasource"
)
// prefix值必须是application.properteis中对应属性的前缀
public
DataSource userDataSource() {
return
DataSourceBuilder.create().build();
}
@Bean
public
SqlSessionFactory userSqlSessionFactory(
@Qualifier
(
"userDataSource"
) DataSource dataSource)
throws
Exception {
SqlSessionFactoryBean bean =
new
SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resolver =
new
PathMatchingResourcePatternResolver();
try
{
bean.setMapperLocations(resolver.getResources(
"classpath*:com/user/server/dao/mapping/*.xml"
));
return
bean.getObject();
}
catch
(Exception e) {
e.printStackTrace();
throw
new
RuntimeException(e);
}
}
@Bean
public
SqlSessionTemplate userSqlSessionTemplate(
@Qualifier
(
"userSqlSessionFactory"
) SqlSessionFactory sqlSessionFactory)
throws
Exception {
SqlSessionTemplate template =
new
SqlSessionTemplate(sqlSessionFactory);
// 使用上面配置的Factory
return
template;
}
}
@Configuration
@MapperScan
(basePackages = {
"com.airmi.server.dao"
}, sqlSessionTemplateRef =
"autoTestSqlSessionTemplate"
)
public
class
AutoTestMybatisConfig {
@Bean
@ConfigurationProperties
(prefix =
"autotest.datasource"
)
public
DataSource autoTestDataSource() {
return
DataSourceBuilder.create().build();
}
@Bean
public
SqlSessionTemplate autoTestSqlSessionTemplate(
@Qualifier
(
"autoTestSqlSessionFactory"
) SqlSessionFactory sqlSessionFactory)
throws
Exception {
SqlSessionTemplate template =
new
SqlSessionTemplate(sqlSessionFactory);
return
template;
}
@Bean
public
SqlSessionFactory autoTestSqlSessionFactory(
@Qualifier
(
"autoTestDataSource"
) DataSource dataSource)
throws
Exception {
SqlSessionFactoryBean bean =
new
SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resolver =
new
PathMatchingResourcePatternResolver();
try
{
bean.setMapperLocations(resolver.getResources(
"classpath*:com/airmi/server/dao/mapping/*.xml"
));
return
bean.getObject();
}
catch
(Exception e) {
e.printStackTrace();
throw
new
RuntimeException(e);
}
}
}
@Primary //该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让autowire注解报错,官网要求当多个数据源时,必须指定一个datasource,另一个datasource则不用添加。
@Qualifier 根据名称进行注入,通常是在具有相同的多个类型的实例的一个注入(例如有多个DataSource类型的实例)。
@MapperScan
(basePackages = {
"com.user.server.dao"
}, sqlSessionTemplateRef =
"userSqlSessionTemplate"
) basePackages为mapper所在的包,sqlSessionTemplateRef要引用的实例。
user代码结构如下: