You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
database/mybatis/src/main/java/config/MyBatisConfig.java

107 lines
4.3 KiB

package config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import model.DataSourceModel;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.util.ResourceUtils;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;
public class MyBatisConfig extends InitConfig {
private static final String CONFIG_NAME = "classpath:database.json";
@Autowired
private DefaultListableBeanFactory beanFactory;
/**
* 注册数据源
*
* @param dbName 数据库名
* @param dbPort 端口
* @param username 用户名
* @param password 密码
* @param type 数据库类型
* @return
*/
private DruidDataSource dataSource(String dbHost, String dbName, int dbPort, String username, String password, DBType type) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
String url = "";
switch (type) {
case mysql57:
url = type.getUrl(dbHost, dbPort, dbName);
dataSource.setUrl(url);
dataSource.setDriverClassName(type.getDriver().getName());
break;
}
LOG.info("数据源初始化\nurl=" + url + "\nusername=" + username + "\npassword=" + password);
return dataSource;
}
private DruidDataSource dataSource(DataSourceModel dataSourceModel) {
return dataSource(dataSourceModel.getHost(), dataSourceModel.getDbName(), dataSourceModel.getPort(), dataSourceModel.getUsername(), dataSourceModel.getPassword(), dataSourceModel.getDbType());
}
private void init() {
//从根数据源读取其他数据源信息,并初始化
List<DataSourceModel> dataSourceModels;
try {
dataSourceModels = JSON.parseObject(FileUtils.readFileToString(ResourceUtils.getFile(CONFIG_NAME), "UTF-8"), new TypeReference<List<DataSourceModel>>() {
});
} catch (IOException e) {
LOG.error("解析数据库配置失败");
throw new RuntimeException(e);
}
dataSourceModels.forEach(dataSourceModel -> {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
DataSource dataSource=dataSource(dataSourceModel);
factoryBean.setDataSource(dataSource);
DataSourceTransactionManager transactionManager=new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
beanFactory.registerSingleton(dataSourceModel.getTransactionManagerBean(), transactionManager);
Configuration configuration = new Configuration();
PathMatchingResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
try {
String path="classpath*:/"+dataSourceModel.getPackageName().replaceAll("\\.","/")+"/*.xml";
LOG.info("Mapper匹配规则"+path);
factoryBean.setMapperLocations(resolver.getResources(path));
} catch (IOException e) {
throw new RuntimeException("mapper匹配规则解析出错");
}
factoryBean.setConfiguration(configuration);
try {
if(factoryBean.getObject()!=null){
beanFactory.registerSingleton(dataSourceModel.getFactoryBean(), factoryBean.getObject());
}else{
LOG.error("初始化数据库配置" + JSON.toJSONString(dataSourceModel) + "失败");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("初始化数据库配置" + JSON.toJSONString(dataSourceModel) + "失败");
}
});
}
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
init();
}
}