1、添加数据库驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加MySQL驱动,这里如果不指定版本默认是8.0的版本,当然你也可以指定版本,8.0的版本这里有一问比较坑的问题:
- MySQL数据库默认使用的是美国的时区,而我们连接的时候用的是中国的北京时间,然后比美国晚上8个小时,所以当我们在连接数据库的时候要设置一下时区为东八区ServerTimezone=UTC
- 驱动类
- MySQL5.x的版本使用的驱动类是com.mysql.jdbc.Driver
- MySQL8.x的版本使用的驱动类是com.mysql.cj.jdbc.Driver
2、添加数据库连接池-阿里开源的druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
Druid首先是一个数据库连接池。Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBossDataSource。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。Druid是阿里巴巴开发的号称为监控而生的数据库连接池!
3、依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
通常我们在集成一些SpringBoot提供支持的技术的时候,所添加的依赖都是以spring-boot-starter开头,格式:spring-boot-starter-xxx;但是刚才我们添加的Mybatis的依赖却是mybatis-spring-boot-starter,是以mybatis开头的,这个能其实是SpringBoot默认是不支持mybatis的,它默认支持的是它自己生态内的持久层框架JPA,由于SpringBoot是大势所趋,所以mybatis就主动去迎合SpringBoot生态,自己开发了mybatis的stater。以后大家凡是看到xxx-spring-boot-starter的依赖,都是SpringBoot没有主动提供支持的技术。
4、yml中配置数据源和mybatis配置
5、spring.factories
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
实例化以下bean
SqlSessionFactory、SqlSessionTemplate、
MapperScannerRegistrarNotFoundConfiguration
如果没有使用@MapperScan这个注解,会去类路径下自动搜索@Mapper注解的类生成MappeFactoryBean
6、比较原始的mybatis配置,MapperFactoryBean,只能针对一个接口
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
@Override
public T getObject() throws Exception {
return getSqlSession().getMapper(this.mapperInterface);
}
7、注解@Mapper对应的配置
指定的basePackage来自动装配MapperFactoryBean,但如果指定了多个DataSource,则必须指定 sqlSessionFactoryBeanName属性
7.1、xml配置
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>
7.2、注解@MapperScan配置
8、springboot整合多数据源
两个数据源,对应两个SqlSessionFactory,@MapperScan扫描不同包下的接口
spring:
datasource:
test1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 1234
test2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 1234
@Configuration
@MapperScan(basePackages = "com.example.mapper.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource test1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager test1TransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource test2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager test2TransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
9、多数据源动态切换
10、如果要写配置文件时有相应的提示,需要加一个依赖
<!-- 自定义配置的提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
编译,target/classes/META-INF下会生成一个spring-configuration-metadata.json文件,在配置文件中写相关配置时就有提示啦(如果已存在该配置项,则不会提示)。