当前位置:   article > 正文

Spring Boot 项目使用 Mybatis_如何判断java项目中有没有使用mydatis-plus

如何判断java项目中有没有使用mydatis-plus

application.yml文件里配置数据源,可多个

  1. # 数据库1
  2. spring.datasource.test1.url: jdbc:mysql://ip1:port1/test1?autoReconnect=true&characterEncoding=UTF-8&useTimezone=true&serverTimezone=GMT%2B8
  3. spring.datasource.test1.username: test1
  4. spring.datasource.test1.password: test1
  5. spring.datasource.test1.driver-class-name: com.mysql.cj.jdbc.Driver
  6. # 数据库2
  7. spring.datasource.test2.url: jdbc:mysql://ip2:port2/test2?autoReconnect=true&characterEncoding=UTF-8&useTimezone=true&serverTimezone=GMT%2B8
  8. spring.datasource.test2.username: test2
  9. spring.datasource.test2.password: test2
  10. spring.datasource.test2.driver-class-name: com.mysql.cj.jdbc.Driver
  11. # 通用连接池配置
  12. spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
  13. spring.datasource.initialSize: 5
  14. spring.datasource.minIdle: 5
  15. spring.datasource.maxActive: 20
  16. spring.datasource.maxWait: 60000
  17. spring.datasource.timeBetweenEvictionRunsMillis: 60000
  18. spring.datasource.validationQuery: SELECT 1
  19. spring.datasource.testWhileIdle: true
  20. spring.datasource.testOnBorrow: false
  21. spring.datasource.testOnReturn: false
  22. spring.datasource.poolPreparedStatements: true
  23. spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
  24. spring.datasource.filters: stat
  25. spring.datasource.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

根据配置的数据库个数,创建对应个数的连接配配置类:

1.常规读取mapper文件的配置

  1. @Configuration
  2. @MapperScan(basePackages = "com.xxx.datasource.test1.mapper", sqlSessionTemplateRef = "test1SqlSessionTemplate")
  3. public class Test1Datasource {
  4. @Bean(name = "test1DataSource")
  5. @Primary
  6. public DataSource getDataSource(Environment env) throws Exception {
  7. Properties props = new Properties();
  8. props.put("driverClassName", env.getProperty("spring.datasource.test1.driver-class-name"));
  9. props.put("url", env.getProperty("spring.datasource.test1.url"));
  10. props.put("username", env.getProperty("spring.datasource.test1.username"));
  11. props.put("password", env.getProperty("spring.datasource.test1.password"));
  12. return DruidDataSourceFactory.createDataSource(props);
  13. }
  14. @Bean(name = "test1SqlSessionFactory")
  15. public SqlSessionFactory getSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
  16. throws Exception {
  17. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  18. bean.setDataSource(dataSource);
  19. // 设置mybatis的主配置文件 记住是主配置文件啊
  20. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  21. // 设置mapper.xml文件的路径 {resourceMapXML};
  22. Resource[] resource = resolver.getResources("classpath*:mapper/*.xml");
  23. bean.setMapperLocations(resource);
  24. return bean.getObject();
  25. }
  26. @Bean(name = "test1TransactionManager")
  27. @Primary
  28. public DataSourceTransactionManager getTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
  29. return new DataSourceTransactionManager(dataSource);
  30. }
  31. @Bean(name = "test1SqlSessionTemplate")
  32. @Primary
  33. public SqlSessionTemplate
  34. getSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
  35. return new SqlSessionTemplate(sqlSessionFactory);
  36. }
  37. }

2.使用mybatis-plus时的配置

  1. @Configuration
  2. @MapperScan(basePackages = "com.xxx.datasource.test2.mapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
  3. public class Test2PlusDataSource {
  4. @Bean(name = "test2PlusDataSource")
  5. public DataSource getDataSource(Environment env) throws Exception {
  6. Properties props = new Properties();
  7. props.put("driverClassName", env.getProperty("spring.datasource.test2.driver-class-name"));
  8. props.put("url", env.getProperty("spring.datasource.test2.url"));
  9. props.put("username", env.getProperty("spring.datasource.test2.username"));
  10. props.put("password", env.getProperty("spring.datasource.test2.password"));
  11. return DruidDataSourceFactory.createDataSource(props);
  12. }
  13. /**
  14. * mybatis-plus分页插件
  15. */
  16. @Bean(name = "test2PaginationInterceptor")
  17. public Interceptor paginationInterceptor() {
  18. PaginationInterceptor page = new PaginationInterceptor();
  19. page.setDialectType("mysql");
  20. return page;
  21. }
  22. /**
  23. * 注入mybatis的 DatabaseIdProvider 作为一个bean,方便被引用
  24. *
  25. * @return bean
  26. */
  27. @Bean
  28. public DatabaseIdProvider getDatabaseIdProvider() {
  29. return new DefaultDatabaseIdProvider();
  30. }
  31. /**
  32. * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定 配置文件和mybatis-boot的配置文件同步 采用多参数注入方式,避免通过autowired注入导致的报错
  33. *
  34. * @param dataSource 注入数据源
  35. * @param databaseIdProvider 注入DatabaseIdProvider
  36. * @param properties 注入 MybatisPlusProperties
  37. * @param interceptors 注入 Interceptor 主要为分页信息
  38. * @return MybatisSqlSessionFactoryBean
  39. * @throws IOException IOException
  40. */
  41. @Bean(name = "test2SqlSessionFactory")
  42. public MybatisSqlSessionFactoryBean test2SqlSessionFactoryBean(
  43. @Qualifier("test2PlusDataSource") DataSource dataSource, DatabaseIdProvider databaseIdProvider,
  44. MybatisPlusProperties properties, Interceptor[] interceptors) throws IOException {
  45. MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
  46. try {
  47. mybatisPlus.setDataSource(dataSource);
  48. } catch (Exception e) {
  49. log.error(ExceptionUtils.getStackTrace(e));
  50. }
  51. //DefaultVFS在获取jar上存在问题,使用springboot
  52. //SpringBoot完整包部署,手动设置SpringBootVFS
  53. mybatisPlus.setVfs(SpringBootVFS.class);
  54. // 设置分页插件
  55. if (interceptors != null) {
  56. mybatisPlus.setPlugins(interceptors);
  57. }
  58. MybatisConfiguration mc = new MybatisConfiguration();
  59. mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
  60. // 数据库和java都是驼峰,就不需要
  61. mc.setMapUnderscoreToCamelCase(true);
  62. mybatisPlus.setConfiguration(mc);
  63. if (databaseIdProvider != null) {
  64. mybatisPlus.setDatabaseIdProvider(databaseIdProvider);
  65. }
  66. mybatisPlus.setTypeAliasesPackage("com.xxx.entity.test2");
  67. if (properties != null) {
  68. mybatisPlus.setTypeHandlersPackage(properties.getTypeHandlersPackage());
  69. mybatisPlus.setMapperLocations(properties.resolveMapperLocations());
  70. }
  71. // 设置mapper.xml文件的路径
  72. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  73. Resource[] resource = resolver.getResources("classpath:mapper/test2/**/*.xml");
  74. mybatisPlus.setMapperLocations(resource);
  75. return mybatisPlus;
  76. }
  77. @Bean(name = "test2MpTransactionManager")
  78. public DataSourceTransactionManager getTransactionManager(@Qualifier("test2PlusDataSource") DataSource dataSource) {
  79. return new DataSourceTransactionManager(dataSource);
  80. }
  81. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/201452
推荐阅读
相关标签
  

闽ICP备14008679号