application.yml中數據源配置:
#數據庫連接 天氣數據
spring:
datasource:
keshangjdbc:
url: jdbc:oracle:thin:@//IP:1565/ods
username:
password:
# driver-class-name: oracle.jdbc.driver.OracleDriver
#數據庫連接 客商數據
weatherjdbc:
url: jdbc:oracle:thin:@//IP:1565/ods
username:
password:
# driver-class-name: oracle.jdbc.driver.OracleDriver
對每中鏈接寫一個配置類,主要是通過aop的方式鏈接注入到SqlSessionTemplate中,
客商的配置類如下:
package com.neusoft.interf.dataSources;
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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
/**
* @author
* @Time:2018年12月19日 上午11:08:35
* @version 1.0
*/
@Configuration
@MapperScan(basePackages = "com.neusoft.interf.mapper.keShang", sqlSessionTemplateRef = "keShangSqlSessionTemplate") //代表掃描的dao層接口,在dao層接口處理前注入數據庫連接
public class keShangDataSource {
@Bean(name = "keShangData")
@ConfigurationProperties(prefix = "spring.datasource.keshangjdbc") // application.properteis中對應屬性的前綴
//這裏特別注意的是application.properteis中對應屬性一定要和DruidDataSource的屬性字段一直
@Primary
public DataSource keShangData() {
return new DruidDataSource();
}
@Bean(name = "keShangSqlSessionFactory")
@Primary
public SqlSessionFactory keShangSqlSessionFactory(@Qualifier("keShangData") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//工程上默認使用的是Mybatis的DefaultVFS進行掃描,但是在springboot的環境下,Mybatis的DefaultVFS這個掃包會出現問
//題,所以只能修改VFS,爲了清晰可見,
VFS.addImplClass(SpringBootVFS.class);//手動觸發掃描
bean.setTypeAliasesPackage("com.neusoft.interf.entity");//對應的實體類文件夾
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/keShang/*.xml"));//對應的xml文件夾
return bean.getObject();
}
@Bean(name = "keShangTransactionManager")
@Primary
public DataSourceTransactionManager keShangTransactionManager(@Qualifier("keShangData") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "keShangSqlSessionTemplate")
@Primary
public SqlSessionTemplate keShangSqlSessionTemplate(@Qualifier("keShangSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
天氣的配置類如下:
package com.neusoft.interf.dataSources;
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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
/**
* @author
* @Time:2018年12月19日 上午11:08:35
* @version 1.0
*/
@Configuration
@MapperScan(basePackages = "com.neusoft.interf.mapper.weather", sqlSessionTemplateRef = "weatherSqlSessionTemplate")
public class weatherDataSource {
@Bean(name = "weatherData")
@ConfigurationProperties(prefix = "spring.datasource.weatherjdbc") // application.properteis中對應屬性的前綴
public DataSource weatherData() {
return new DruidDataSource();
// return DataSourceBuilder.create().build();
}
@Bean(name = "weatherSqlSessionFactory")
public SqlSessionFactory weatherSqlSessionFactory(@Qualifier("weatherData") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//工程上默認使用的是Mybatis的DefaultVFS進行掃描,但是在springboot的環境下,Mybatis的DefaultVFS這個掃包會出現問
//題,所以只能修改VFS,爲了清晰可見,
VFS.addImplClass(SpringBootVFS.class);//手動觸發掃描
bean.setTypeAliasesPackage("com.neusoft.interf.entity");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/weather/*.xml"));
return bean.getObject();
}
@Bean(name = "weatherTransactionManager")
public DataSourceTransactionManager weatherTransactionManager(@Qualifier("weatherData") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "weatherSqlSessionTemplate")
public SqlSessionTemplate weatherSqlSessionTemplate(@Qualifier("weatherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
如果有多個數據源,配置類中的@Primary只能寫在其中一個,否則會報錯。
此處有一個 技巧:
靜態工具類中如果調用,springboot中bean,通過@Autowired引入bean,@PostConstruct註解方法中,把此bean
賦給靜態變量
@Autowired
private DictService dictService;
@Autowired
private static DictService dictService1;
@PostConstruct
public void init() {
dictService1 = dictService;
}
public static List<Dict> getDictList(String type) {
Map<String, Object> dictMap = Maps.newHashMap();
List<Dict> dicts = null;
try {
dicts = dictService1.findAllList();
} catch (Exception e) {
e.printStackTrace();
}
for (Dict dict : dicts) {
List<Dict> dictList = (List<Dict>) dictMap.get(dict.getType());
if (dictList != null) {
dictList.add(dict);
} else {
dictMap.put(dict.getType(), Lists.newArrayList(dict));
}
}
List<Dict> dictList = (List<Dict>) dictMap.get(type);
if (dictList == null) {
dictList = Lists.newArrayList();
}
return dictList;
}
}
1.@PostConstruct說明
被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,並且只會被服務器調用一次,類似於Servlet的inti()方法。被@PostConstruct修飾的方法會在構造函數之後,init()方法之前運行。
2.@PreDestroy說明
被@PreDestroy修飾的方法會在服務器卸載Servlet的時候運行,並且只會被服務器調用一次,類似於Servlet的destroy()方法。被@PreDestroy修飾的方法會在destroy()方法之後運行,在Servlet被徹底卸載之前。(詳見下面的程序實踐)