javascript
mybatis plus当月数据查询_Springboot+mybatis(plus)+druid多数据源
前言:
我不太喜歡AOP讀自定義注解來切換數(shù)據(jù)源.因為如果我一個業(yè)務里需要同時查2個數(shù)據(jù)源的數(shù)據(jù)而又不想把這個業(yè)務拆成2個方法的時候,就比較麻煩了.
所以我打算根據(jù)package來掃描注入不同的DataSource.可能是我搜索的姿勢不太對 , 資料比較少.也會碰到不少坑.所以記錄一下.
正文:
mybatis 和 mybatisplus的區(qū)別不大 ,一起記了.
springboot 和 mybatis jar就不說了.
druid的jar??
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.1</version> </dependency>其實就是在spring.datasource.druid下再指定2個連接.
2.有了連接信息, 就去代碼配置DataSource的Bean了.
@Configuration public class DataSourceConfig {/*** PSI 庫連接配置前綴 (與 yml 中的連接信息前綴一致 , 下同)*/private static final String PSI_DATASOURCE_PREFIX = "spring.datasource.druid.psi";/*** PSI 庫連接Bean名字*/public static final String PSI_DATASOURCE_BEAN_NAME = "psiDataSource";/*** B2B 庫中間庫連接配置前綴*/private static final String B2B_DATASOURCE_PREFIX = "spring.datasource.druid.b2badapter";/*** B2B 庫中間庫連接Bean名字*/public static final String B2B_DATASOURCE_BEAN_NAME = "b2bDataSource";@Primary@Bean(name = PSI_DATASOURCE_BEAN_NAME)@ConfigurationProperties(prefix = PSI_DATASOURCE_PREFIX)public DruidDataSource psi() {return new DruidDataSource();}@Bean(name = B2B_DATASOURCE_BEAN_NAME)@ConfigurationProperties(prefix = B2B_DATASOURCE_PREFIX)public DruidDataSource b2b() {return new DruidDataSource();} }這個@Primary還是有點用, 等下說.
3.配置每個庫對應的SqlSessionFactory
以第一個庫(psi)舉例
@Configuration @MapperScan(basePackages = "com.a.b.c.d.e.psi.dao", sqlSessionTemplateRef = "psiSqlSessionTemplate") public class PsiDataSourceConfig {@Autowired@Qualifier(DataSourceConfig.PSI_DATASOURCE_BEAN_NAME)private DataSource psi;@Primary@Bean("psiSqlSessionFactory")public SqlSessionFactory psiSqlSessionFactory() throws Exception {MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();factoryBean.setDataSource(psi);MybatisConfiguration configuration = new MybatisConfiguration();configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);configuration.setJdbcTypeForNull(JdbcType.NULL);factoryBean.setConfiguration(configuration);//指定xml路徑.factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/a/b/c/d/e/psi/mapper/*Dao.xml"));return factoryBean.getObject();}@Primary@Bean("psiSqlSessionTemplate")public SqlSessionTemplate psiSqlSessionTemplate() throws Exception {SqlSessionTemplate template = new SqlSessionTemplate(psiSqlSessionFactory()); // 使用上面配置的Factoryreturn template;} }算了, 第二個也放上來吧
@Configuration @MapperScan(basePackages = "com.a.b.c.d.e.b2b.dao", sqlSessionTemplateRef = "b2bSqlSessionTemplate") public class B2BDataSourceConfig {@Autowired@Qualifier(DataSourceConfig.B2B_DATASOURCE_BEAN_NAME)private DataSource b2b;@Bean("b2bSqlSessionFactory")public SqlSessionFactory b2bSqlSessionFactory() throws Exception {MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();factoryBean.setDataSource(b2b);MybatisConfiguration configuration = new MybatisConfiguration();configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);configuration.setJdbcTypeForNull(JdbcType.NULL);factoryBean.setConfiguration(configuration);//指定xml路徑.factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/a/b/c/d/e/b2b/mapper/*Dao.xml"));factoryBean.setPlugins(new Interceptor[]{new PaginationInterceptor(),new PerformanceInterceptor(),new OptimisticLockerInterceptor()});return factoryBean.getObject();}@Bean("b2bSqlSessionTemplate")public SqlSessionTemplate b2bSqlSessionTemplate() throws Exception {SqlSessionTemplate template = new SqlSessionTemplate(b2bSqlSessionFactory()); // 使用上面配置的Factoryreturn template;} }這個配置是MybatisPlus的MybatisSqlSessionFactoryBean,通過這個類才能使用MybatisPlus的BaseMapper如果只是Mybatis的話...
@Bean public SqlSessionFactory mysqlSqlSessionFactory() throws Exception {SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(b2b);//指定xml路徑.factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/a/b/c/d/e/b2b/mapper/*Dao.xml"));return factoryBean.getObject(); }普通的SqlSessionFactoryBean就行了..
這2個數(shù)據(jù)源就是通過類上的
@MapperScan(basePackages = "com.a.b.c.d.e.b2b.dao")來掃描項目不同包下的Mapper文件來注入不同的數(shù)據(jù)源.
如果,萬一,在某些情況下. 2個MapperScan掃描到重復的包怎么辦.....誰有@Primary就誰上..
4.最后的最后.去掉springboot/jpa,druid等的自動配置
在啟動類上(當然你也可以在yml里去配)加上
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class })如果是Mybatis的話,.. 只用去掉DataSourceAutoConfiguration.class應該就行了/..
然后, 沒了吧. 就可以用了.
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的mybatis plus当月数据查询_Springboot+mybatis(plus)+druid多数据源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息安全技术 网络安全等级保护测评要求_
- 下一篇: python stringvar函数_T