springboot集成mybatis多数据源,小驼峰配置,字符串为空判断

集成步骤:

步骤一:多数据源pom引入



    org.springframework.boot
    spring-boot-starter-data-jpa






    com.alibaba
    druid
    ${druid.version}



    mysql
    mysql-connector-java
    ${mysql.driver.version}


    org.postgresql
    postgresql
    42.2.5.jre7



    com.oracle
    ojdbc6
    11.2.0.3


步骤二:配置文件yml

# spring配置
spring:
  # 数据源配置
  datasource:
    self:
      jdbc-url: jdbc:mysql://地址:端口/vedio-big-data?allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: 账号
      password: 密码
      driver-class-name: com.mysql.jdbc.Driver

      # Druid连接池配置
      type: com.alibaba.druid.pool.DruidDataSource
      # 初始化
      initialSize: 3
      # 最大
      maxActive: 20
      # 最小
      minIdle: 3
      # 最大连接等待超时时间
      maxWait: 60000
      # 打开PSCache,并且指定每个连接PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      # 配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
      filters: stat, wall, log4j2
      logSlowSql: true

  elasticsearch:
    jest:
      uris: http://100.11.209.171:9200,http://100.11.209.151:9200,http://100.11.209.173:9200
      username: elastic
      password: jxjp123
      #读取超时
      read-timeout: 20s
      #连接超时
      connection-timeout: 20s
      multi-threaded: true


  # spring jpa 配置
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: none
    database: mysql

# 海康-车辆统计数据源配置
vehicle:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    jdbc-url: jdbc:oracle:thin:@地址:端口/ORCL
    username: 账号
    password: 密码

# 海康-wifi统计数据源配置
wifi:
  datasource:
    driver-class-name: org.postgresql.Driver
    jdbc-url: jdbc:postgresql://地址:端口/was
    username: 账号 
    password: 密码

步骤三:多数据源配置类

@Configuration
public class DataSourceConfig {

   @Bean(name = "wifiDataSource")
   @Qualifier("wifiDataSource")//该注解指定注入的Bean的名称,Spring框架使用byName方式寻找合格的 bean,这样就消除了byType方式产生的歧义
   @ConfigurationProperties(prefix="wifi.datasource")//读取配置文件里前缀 为"spring.datasource.primary"的语句
   public DataSource wifiDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "vehicleDataSource")
   @Qualifier("vehicleDataSource")
   @ConfigurationProperties(prefix="vehicle.datasource")
   public DataSource vehicleDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "selfDataSource")
   @ConfigurationProperties(prefix="spring.datasource.self")
   public DataSource selfDataSource() {
      return DataSourceBuilder.create().build();
   }


   @Bean(name = "wifiJdbcTemplate")
   public JdbcTemplate firstJdbcTemplate(@Qualifier("wifiDataSource") DataSource dataSource) {
   return new JdbcTemplate(dataSource);
   }

   @Bean(name = "vehicleJdbcTemplate")
   public JdbcTemplate secondJdbcTemplate(@Qualifier("vehicleDataSource") DataSource dataSource) {
      return new JdbcTemplate(dataSource);
   }

   @Bean(name = "selfSessionFactory")
   @Qualifier("selfSessionFactory")
   public SqlSessionFactory selfSessionFactory(@Qualifier("selfDataSource") DataSource dataSource) throws Exception{
      SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
      sessionFactoryBean.setDataSource(dataSource);
      //小驼峰配置
      org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
      configuration.setMapUnderscoreToCamelCase(true);
      sessionFactoryBean.setConfiguration(configuration);
      Interceptor[] plugins =  new Interceptor[]{pageHelper()};
      sessionFactoryBean.setPlugins(plugins);
      return sessionFactoryBean.getObject();
   }

   @Bean(name = "selfSessionTemplate")
    public SqlSessionTemplate selfSessionTemplate(@Qualifier("selfSessionFactory")SqlSessionFactory sqlSessionFactory) throws Exception{
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
        return template;
   }

   @Bean
   public PlatformTransactionManager txManager(@Qualifier("selfDataSource")DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
   }

   @Bean
   public PageHelper pageHelper(){
      PageHelper pageHelper = new PageHelper();
      Properties p = new Properties();
      //1.offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
      p.setProperty("offsetAsPageNum", "true");
      //2.rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
      p.setProperty("rowBoundsWithCount", "true");
      //3.reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
      p.setProperty("reasonable", "true");
      pageHelper.setProperties(p);
      return pageHelper;
   }

 

步骤四:mybatis注解开发,字符串非空写法(subType1 != ​​​​​​​\"\"

@Select("")
 ListfindDeviceForGis(@Param("baseDeviceEntity")BaseDeviceEntity baseDeviceEntity);

你可能感兴趣的:(spring)