第一步:引入需要的maven
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.13.0-HBase-1.3</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.4.Final</version>
</dependency>
第二步:配置spring boot的数据源
package com.kevin.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
*@Aurhor kevin liu
*@CreateDate 2017-11-22 16:25
*@描述: hbase-phoenix 数据源模版
*/
@Configuration
@PropertySource(value="classpath:application.properties")
@MapperScan(basePackages = HbasePhoenixDataSourceConfig.PACKAGE,sqlSessionFactoryRef = HbasePhoenixDataSourceConfig.HBASEPHOENIX_SQL_SESSION_FACTORY)
public class HbasePhoenixDataSourceConfig {
static final String HBASEPHOENIX_SQL_SESSION_FACTORY = "hbasePhoenixSqlSessionFactory";
static final String PACKAGE = "com.kevin.dao.hbase";
static final String MAPPER_LOCATION = "classpath:mapper/hbase/*.xml";
@Value("${hbase.phoenix.datasource.url}")
private String url;
@Value("${hbase.phoenix.datasource.driverClassNam}")
private String driverClass;
@Bean(name = "hbasePhoenixDataSource")
public DataSource hbasePhoenixDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
return dataSource;
}
@Bean(name = "hbasePhoenixTransactionManager")
public DataSourceTransactionManager hbasePhoenixTransactionManager(@Qualifier("hbasePhoenixDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = HBASEPHOENIX_SQL_SESSION_FACTORY)
public SqlSessionFactory hbasePhoenixSqlSessionFactory(@Qualifier("hbasePhoenixDataSource") DataSource hbasePhoenixDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(hbasePhoenixDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(HbasePhoenixDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}