一、整合多数据源
1、pom文件引入依赖
<!-引入mybatis->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-引入jpa->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-引入mysql->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-引入oracle->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
2、配置properties文件属性
spring.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.mysql.jdbc-url=XXX
spring.datasource.mysql.username=XXX
spring.datasource.mysql.password=XXX
spring.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.oracle.jdbc-url=XXX
spring.datasource.oracle.username=XXX
spring.datasource.oracle.password=XXX
springboot2.0版本后,数据源的url属性名称为jdbc-url,2.0前为url
3、配置DataSource、SqlSessionFactory、TransactionManager、SqlSessionTemplate
@Configuration
@MapperScan(basePackages = {"oracle数据源扫描dao包的路径"}, sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {
//创建数据源bean对象
@Bean(name = "oracleDataSource")
@ConfigurationProperties(prefix = "spring.datasource.oracle")//指定使用配置属性的前缀
@Primary
public DataSource oracleDataSource() {
return DataSourceBuilder.create().build();
}
//创建SqlSessionFactory的bean对象
@Bean(name = "oracleSqlSessionFactory")
@Primary
public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//mapper文件扫描路径
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper文件路径"));
return bean.getObject();
}
//创建TransactionManager的bean对象
@Bean(name = "oracleTransactionManager")
@Primary
public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
//创建SqlSessionTemplate的bean对象
@Bean(name = "oracleSqlSessionTemplate")
@Primary
public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
mysql数据源配置方法同上
二、拦截器的使用
在@SpringBootApplication类下,新建过滤方法
@Bean
public FilterRegistrationBean ParameterFilterRegister() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new ParameterFilter());
//拦截规则
registration.addUrlPatterns("/*");
//过滤器名称
registration.setName("parameterFilter");
//初始化参数
Map<String,Object> initParameters = new HashMap<String,Object>();
initParameters.put("xxx", "xxx");
registration.setInitParameters(initParameters);
//过滤器顺序,按从小到大执行
registration.setOrder(1);
return registration;
}
三、获取ApplicationContext对象
编写一个实体类实现ApplicationContextAware接口
package com.bdvo.SmartPush.base.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil
implements ApplicationContextAware
{
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
* @param name
* @return
*/
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
四、其他配置
1、添加项目访问前缀
server.servlet.context-path=/XXX
2、开启日志
logging.level.XXX=DEBUG //xxx即需打印日志的包 日志级别从低到高 TRACE < DEBUG < INFO < WARN < ERROR < FATA
springboot默认的日志是
logback,
若想使用其他日志框架(如log4j)需引入pom依赖
更多的配置参数可以通过新建配置文件来设置(当然也可以写在application.properties里)
Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
3、分区配置文件
spring.profiles.active=dev 可以指定引用application-dev.properties,通过这个属性可以区分测试、生产配置文件