DataSource 配置
package com.cheng.dynamic.config; import java.util.Properties; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment; @Configuration
public class DataSourceConfig {
@Autowired
private Environment env; @Bean(name = "primaryDS")
@Primary
public DataSource primaryDataSource() throws Exception{
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
ds.setUniqueResourceName("primaryRN");
ds.setPoolSize(5);
ds.setXaProperties(build("cheng.primary.datasource."));
return ds;
} @Bean(name = "secondaryDS")
public DataSource dataSource() throws Exception{
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
ds.setUniqueResourceName("secondaryRN");
ds.setPoolSize(5);
ds.setXaProperties(build("cheng.secondary.datasource."));
return ds;
} private Properties build(String prefix) {
Properties prop = new Properties();
prop.put("url", env.getProperty(prefix + "url"));
prop.put("username", env.getProperty(prefix + "username"));
prop.put("password", env.getProperty(prefix + "password"));
prop.put("driverClassName", env.getProperty(prefix + "driverClassName")); return prop;
}
}
JtaTransactionManagerConfig
package com.cheng.dynamic.config; import javax.transaction.UserTransaction; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.jta.JtaTransactionManager; import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager; @Configuration
public class JtaTransactionManagerConfig {
@Bean(name = "xatx")
public JtaTransactionManager regTransactionManager () {
UserTransactionManager userTransactionManager = new UserTransactionManager();
UserTransaction userTransaction = new UserTransactionImp();
return new JtaTransactionManager(userTransaction, userTransactionManager);
}
}
RepositoryPrimaryConfig
package com.cheng.dynamic.config; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @Configuration
@MapperScan(basePackages = "com.cheng.dynamic.repository.primary", sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class RepositoryPrimaryConfig {
@Autowired
private Environment env;
@Autowired
@Qualifier("primaryDS")
private DataSource primaryDS; @Bean(name="sqlSessionFactoryPrimary")
@Primary
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception{
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(primaryDS);
fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));//指定基包
fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));//指定xml文件位置
return fb.getObject();
} @Bean(name = "sqlSessionTemplatePrimary")
@Primary
public SqlSessionTemplate sqlSessionTemplatePrimary(@Qualifier("sqlSessionFactoryPrimary") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}
}
RepositorySecondaryConfig
package com.cheng.dynamic.config; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @Configuration
@MapperScan(basePackages = "com.cheng.dynamic.repository.secondary", sqlSessionTemplateRef = "sqlSessionTemplateSecondary")
public class RepositorySecondaryConfig {
@Autowired
private Environment env;
@Autowired
@Qualifier("secondaryDS")
private DataSource secondaryDS; @Bean(name="sqlSessionFactorySecondary")
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception{
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(secondaryDS);
fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage2"));//指定基包
fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations2")));//指定xml文件位置
return fb.getObject();
} @Bean(name = "sqlSessionTemplateSecondary")
public SqlSessionTemplate sqlSessionTemplateSecondary(@Qualifier("sqlSessionFactorySecondary") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
Test1Service
package com.cheng.dynamic.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.cheng.dynamic.entity.Test1;
import com.cheng.dynamic.entity.Test2;
import com.cheng.dynamic.repository.primary.Test1Dao; @Service
@Transactional
public class Test1Service {
@Autowired
private Test1Dao test1Dao;
@Autowired
private Test2Service test2Service; public void test(){
System.out.println(test1Dao);
} public void add(Test1 t){
System.out.println(test1Dao.add(t));
System.out.println(t.getId());
} public void save(){
Test1 t1 = new Test1();
t1.setName("t1");
add(t1);
// System.out.println(1/0);
Test2 t2 = new Test2();
t2.setName("t2");
test2Service.add(t2);
}
}
测试类
package com.cheng.dynamic.service; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; @RunWith(SpringRunner.class)
@SpringBootTest()
@Transactional(transactionManager = "xatx")
@Rollback(false)
public class Test1ServiceTest {
@Autowired
private Test1Service test1Service;
@Test
public void testTest() throws Exception {
test1Service.save();
} }
druid监控配置
package com.cheng.dynamic.config; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter; @Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean<StatViewServlet> druidServlet() {
ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); //控制台管理用户,加入下面2行 进入druid后台就需要登录
//servletRegistrationBean.addInitParameter("loginUsername", "admin");
//servletRegistrationBean.addInitParameter("loginPassword", "admin");
return servletRegistrationBean;
} @Bean
public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.addInitParameter("profileEnable", "true");
return filterRegistrationBean;
} @Bean
public StatFilter statFilter(){
StatFilter statFilter = new StatFilter();
statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。
statFilter.setMergeSql(true); //SQL合并配置
statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。
return statFilter;
} @Bean
public WallFilter wallFilter(){
WallFilter wallFilter = new WallFilter();
//允许执行多条SQL
WallConfig config = new WallConfig();
config.setMultiStatementAllow(true);
wallFilter.setConfig(config);
return wallFilter;
}
}
完整代码:https://github.com/lucheng/jta.git
其它:
SpringBoot 多数据源配置 https://github.com/lucheng/dynamic.git
SpringBoot 多数据源 + 动态数据源配置:https://github.com/lucheng/dynamicDatasource.git