SpringBoot+ES+MyBatis配置

  1. application.yml配置
spring:
  datasource:
    es:
      url: jdbc:elasticsearch://192.168.0.197:9300/
  #      driver-class-name: com.alibaba.druid.proxy.DruidDriver
  #      type: com.pqtel.stkcore.mybatis.EsDataSourceFactory
  #hikari:
  # 池中维护的最小空闲连接数
  minimum-idle: 10
  # 池中最大连接数,包括闲置和使用中的连接
  maximum-pool-size: 20
  # 此属性控制从池返回的连接的默认自动提交行为。默认为true
  auto-commit: true
  # 允许最长空闲时间
  idle-timeout: 30000
  # 此属性表示连接池的用户定义名称,主要显示在日志记录和JMX管理控制台中,以标识池和池配置。 默认值:自动生成
  pool-name: custom-hikari
  #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
  max-lifetime: 1800000
  # 数据库连接超时时间,默认30秒,即30000
  connection-timeout: 30000
  # 连接测试sql 这个地方需要根据数据库方言差异而配置 例如 oracle 就应该写成  select 1 from dual
  connection-test-query: SELECT 1


# mybatis 相关配置
mybatis:
  type-aliases-package: com.pqtel.stkcore.bean
  mapper-locations: classpath:mapper/*.xml

logging:
  level:
    com.pqtel.stkcore: debug

server:
  port: 6035
  1. ESDataSourceConfig.java
package com.pqtel.stkcore.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.ElasticSearchDruidDataSourceFactory;
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.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;


@Configuration
@MapperScan(basePackages = "com.pqtel.stkcore.mapper", sqlSessionTemplateRef = "esSqlSessionTemplate")
@EnableAutoConfiguration(exclude={ElasticsearchAutoConfiguration.class, RestClientAutoConfiguration.class})
public class ESDataSourceConfig {
    @Value("${spring.datasource.es.url}")
    private String url;

    @Primary
    @Bean(name = "esDataSource")
    @ConfigurationProperties(prefix="spring.datasource.es")
    public DataSource dataSource() {
        Properties properties = new Properties();
        properties.put("url",url);
        properties.put(DruidDataSourceFactory.PROP_CONNECTIONPROPERTIES, "client.transport.ignore_cluster_name=true");

        DruidDataSource druidDataSource = null;
        try {
            druidDataSource = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }

    @Bean(name = "esSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("esDataSource") DataSource druidDataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(druidDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "esSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("esSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
        return template;
    }
}

  1. Application.java
package com.pqtel.stkcore;

import com.pqtel.stkcore.service.HBaseService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.pqtel.stkcore.mapper")
public class StkcoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(StkcoreApplication.class, args);
        HBaseService.getIstance();
    }

}

  1. pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.3.RELEASE
         
    
    com.pqtel
    stkcore
    0.0.1
    stkcore
    stkcore project for 1400

    
        1.8
        2.10.0
        2.0.6
        6.8.6
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.nlpcn
            elasticsearch-sql
            6.8.6
            system
            ${project.basedir}/lib/elasticsearch-sql-6.8.6.0.jar
        
        
        
            org.elasticsearch.client
            x-pack-transport
            ${elasticsearch.version}
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            ${elasticsearch.version}
        
        
            com.alibaba
            druid
            1.1.16
        
        
            com.alibaba
            fastjson
            1.2.47
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.apache.hadoop
            hadoop-client
            ${hadoop.version}
        
        
            org.apache.hadoop
            hadoop-common
            ${hadoop.version}
        
        
            org.apache.hbase
            hbase-client
            ${hbase.version}
        
        
            org.apache.hbase
            hbase-common
            ${hbase.version}
        
        
            org.apache.hbase
            hbase-mapreduce
            ${hbase.version}
        
        
            com.google.guava
            guava
            18.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    
    
        
            elastic.co
            https://artifacts.elastic.co/maven
        
    


你可能感兴趣的:(SpringBoot+ES+MyBatis配置)