本篇博文主要包含:
- springboot整合多数据源实例
-分包结构(推荐使用)
-使用自定义注解 - 事物管理实例
-用于多数据源:使用springboot+jta-atomikos 分布式事物管理 - 日志管理
-使用log4j记录日志
-log4j结合AOP统一处理Web请求日志
-springboot + mybatis设置将SQL语句打印到控制台 - 使用@Scheduled创建定时任务
- 使用@Async实现异步调用
- application配置文件里自定义参数
- 多环境配置
- 发布打包
- 缓存支持–注解配置与EhCache使用
一、springboot整合多数据源
如何区分数据源?
1) 分包结构(推荐使用),如:wmq.fly.test01包:访问test01数据库;wmq.fly.test02包:访问test02数据库。
2) 使用自定义注解,如:@DataSource01:访问test01数据库;@DataSource02:访问test02数据库。
分布式事务解决方案:jpa+automikos(传统项目)
- 项目目录:
数据库目录:
建表语句:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
)
- application.yml 配置文件中新增两个自定义数据源
spring:
datasource:
url: jdbc:mysql://localhost:3308/school?characterEncoding=utf-8
#url: jdbc:mysql://localhost:8066/test?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
###自定义数据源
test1:
url: jdbc:mysql://localhost:3308/test01?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
test2:
url: jdbc:mysql://localhost:3308/test02?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
- 配置文件中新增两个数据源
DataSource1Config 类,配置test01数据源,用@Primary标记为主数据源:
package wmq.fly.multipleDataSource.source;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration // 注册到springboot容器中
//wmq.fly.multipleDataSource.test01.dao下的的*mapper.java使用test01数据源
@MapperScan(basePackages = "wmq.fly.multipleDataSource.test01.dao", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
/**
* 配置test1数据库
*/
@Bean(name = "test1DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.test1") //从配置文件中读取spring.datasource.test1开头的配置
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* test1 sql会话工厂
*/
@Bean(name = "test1SqlSessionFactory")
@Primary //设置为主链接,启动项目时使用此链接,不然不知道使用哪个而报错
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//从xml配置文件中读取配置信息
// bean.setMapperLocations(
// new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
/**
* test1 事物管理
*/
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSource2Config ,配置test02数据源,类:
package wmq.fly.multipleDataSource.source;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration // 注册到springboot容器中
//wmq.fly.multipleDataSource.test02.dao下的的*mapper.java使用test02数据源
@MapperScan(basePackages = "wmq.fly.multipleDataSource.test02.dao", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
/**
* 配置test2数据库
*/
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* test2 sql会话工厂
*/
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(
// new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
return bean.getObject();
}
/**
* test2 事物管理
*/
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 实体类User类
package wmq.fly.multipleDataSource.entity;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- 创建分包Mapper
multipleDataSource.test01.dao下的UserMapper01 类,使用test01数据源:
package wmq.fly.multipleDataSource.test01.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import wmq.fly.multipleDataSource.entity.User;
public interface UserMapper01 {
@Select("SELECT * FROM USERS WHERE NAME = #{name}")
User findByName(@Par