天天看點

springboot中使用mybatis封裝phoenix

使用hbase api來通路hbase的時候,如果字段太多,寫起來的是很枯燥的

使用phoenix通路hbase,可以支援sql,相當于jdbc。用mybatis封裝phoenix後,可以自動管理字段的映射,在一表超級多字段的情況下,可以減少工作量。

mybatis封裝phoenix隻需要一個配置類就可以搞定了

本項目是使用maven建構的

這裡使用的是mybatis-plus

首先添加依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.6</version>
        </dependency>

        <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>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
           

建立一個config類來配置

@Configuration
@PropertySource(value = "classpath:application.yml")
@MapperScan(basePackages = {
        "com.ma.phoenix.test.dao"
    },
    sqlSessionFactoryRef = HBasePhoenixDataSourceConfig.HBASEPHOENIX_SQL_SESSION_FACTORY
)
public class HBasePhoenixDataSourceConfig {

    static final String HBASEPHOENIX_SQL_SESSION_FACTORY = "hbasePhoenixSqlSessionFactory";
    static final String MAPPER_LOCATION = "classpath:com/ma/phoenix/test/dao/*.xml";


    @Value("${spring.datasource.phoenix.jdbc-url}")
    private String url;

    @Value(value = "${spring.datasource.phoenix.driver-class-name}")
    private String driverClass;


    /**
     * 1.擷取資料源
     * @return
     */
    @Bean(name = "hBasePhoenixDataSource")
    public DataSource hBasePhoenixDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        //dataSource.setConnectProperties(properties);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    /**
     * 2.建立事務管理
     * @param dataSource
     * @return
     */
    @Bean(name = "hbasePhoenixTransactionManager")
    public DataSourceTransactionManager hbasePhoenixTransactionManager(
            @Qualifier("hBasePhoenixDataSource")DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 3.建立 session工廠
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = HBASEPHOENIX_SQL_SESSION_FACTORY)
    public SqlSessionFactory hbasePhoenixSqlSessionFactory(
            @Qualifier("hBasePhoenixDataSource")DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(HBasePhoenixDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }

    public static void main(String[] args) throws IOException {
        Resource[] resources = new PathMatchingResourcePatternResolver()
                .getResources(HBasePhoenixDataSourceConfig.MAPPER_LOCATION);
        System.out.println(resources.length);
    }


}
           

在配置檔案application.yml中添加進資料源資訊

spring:
  datasource:
    phoenix:
      jdbc-url: jdbc:phoenix:xx.xx.xx.xx
      driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver
           

xx.xx.xx.xx 是hbase所在的伺服器ip位址

springboot中使用mybatis封裝phoenix

@Configuration 是一定要的注解

@PropertySource(value = “classpath:application.yml”) 這個用來擷取資源檔案,可以是properties檔案

@MapperScan中

basePackages 用來指定映射接口的包路徑,可以寫多個,用逗号","分割開

sqlSessionFactoryRef 指定建立的sqlSessionFactory

springboot中使用mybatis封裝phoenix

MybatisSqlSessionFactoryBean 還可以配置其他參數,例如指定mybatis的總配置檔案的路徑等等

main方法是用來擷取MAPPER_LOCATION 下的xml映射檔案數量的,由于不是放在資源檔案夾中,需要在pom.xml檔案中添加配置

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>
           

到這裡就ok啦

繼續閱讀