日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

mybatis plus当月数据查询_Springboot+mybatis(plus)+druid多数据源

發布時間:2025/3/12 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis plus当月数据查询_Springboot+mybatis(plus)+druid多数据源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

我不太喜歡AOP讀自定義注解來切換數據源.因為如果我一個業務里需要同時查2個數據源的數據而又不想把這個業務拆成2個方法的時候,就比較麻煩了.

所以我打算根據package來掃描注入不同的DataSource.可能是我搜索的姿勢不太對 , 資料比較少.也會碰到不少坑.所以記錄一下.

正文:

mybatis 和 mybatisplus的區別不大 ,一起記了.

springboot 和 mybatis jar就不說了.

druid的jar??

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.1</version> </dependency>
  • 配置數據庫連接信息
  • spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:# psi 庫psi:url: jdbc:postgresql://127.0.0.1:5432/psidbdriver-class-name: org.postgresql.Driverusername: usernamepassword: password# b2b 庫b2badapter:url: jdbc:mysql://127.0.0.1:13306/b2b?useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8&allowMultiQueries=truedriver-class-name: com.mysql.jdbc.Driverusername: usernamepassword: username

    其實就是在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個數據源就是通過類上的

    @MapperScan(basePackages = "com.a.b.c.d.e.b2b.dao")

    來掃描項目不同包下的Mapper文件來注入不同的數據源.

    如果,萬一,在某些情況下. 2個MapperScan掃描到重復的包怎么辦.....誰有@Primary就誰上..

    4.最后的最后.去掉springboot/jpa,druid等的自動配置

    在啟動類上(當然你也可以在yml里去配)加上

    @SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class })如果是Mybatis的話,.. 只用去掉DataSourceAutoConfiguration.class應該就行了

    /..

    然后, 沒了吧. 就可以用了.

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的mybatis plus当月数据查询_Springboot+mybatis(plus)+druid多数据源的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。