zoukankan      html  css  js  c++  java
  • SpringBoot 使用Phoenix操作HBase教程3(使用MyBatis)

    借助 Apache Phoenix,我们可以使用标准 SQL 和 JDBC 接口来操作 HBase。前文演示了 Spring Boot 项目使用 JdbcTemplate 来操作 HBase 数据库,本文接着演示使用 MyBatis 来操作 HBase 数据库。

    三、使用 MyBatis 操作 HBase

    (1)执行下面命令创建一个名为 student 的表:

    注意:在 phoenix 中,默认情况下,库名,表名,字段名等会自动转换为大写,若要小写,使用双引号,如"student"。

    CREATE TABLE IF NOT EXISTS "student"(
    id VARCHAR primary key,
    name VARCHAR,          
    age VARCHAR);

    2,项目配置

    (1)首先编辑项目的 pom.xml 文件,除了添加 Phoenix 相关依赖外,还添加 MyBatis 依赖(高亮部分):
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 去掉springboot默认日志配置 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
     
        <!-- 引入log4j2依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
     
        <!-- 引入lombok依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
     
        <!-- MyBatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
     
        <!-- phoenix相关依赖配置 -->
        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>5.0.0-HBase-2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>joda-time</groupId>
                    <artifactId>joda-time</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

    (2)接着在 application.properties 中配置数据库连接信息:

    spring.datasource.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver
    spring.datasource.url=jdbc:phoenix:81.68.xx.xx:2181

    yml版本

    spring:
        datasource:
            driverClassName:org.apache.phoenix.jdbc.PhoenixDriver
            url:jdbc:phoenix:81.68.xx.xx:2181

    3,编写代码

    (1)首先创建数据表对应的 Student 实体类:
    @Setter
    @Getter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class Student {
        private String id;
        private String name;
        private String age;
    }

    (3)接着创建 StudentMapper 接口:

    要指明一个类是 Mapper 有如下两种方式:

          一种如本样例所示,在 StudentMapper 上添加 @Mapper 注解,表明该接口是一个 MyBatis 中的 Mapper。这种方式就是需要在每一个 Mapper 上都添加注解。

         另一种更简单的方式是在配置类上添加 @MapperScan("com.example.demo.mapper") 注解,表示扫描 com.example.demo.mapper 包下的所有接口作为 Mapper。这样就不需要在每个接口上配置 @Mapper 注解了。

    @Mapper
    public interface StudentMapper {
        int upsertStudent(Student student);
        int deleteStudentById(String id);
        Student getStudentById(String id);
        List<Student> getAllStudents();
    }

    (4)接着在 StudentMapper 相同的位置创建 StudentMapper.xml 文件,内容如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.phoenixdemo.mapper.StudentMapper">
        <update id="upsertStudent" parameterType="com.example.phoenixdemo.model.Student">
            UPSERT INTO "student" VALUES (#{id}, #{name}, #{age})
        </update>
        <delete id="deleteStudentById" parameterType="String">
            DELETE FROM "student" WHERE id=#{id}
        </delete>
        <select id="getStudentById" parameterType="String" resultType="com.example.phoenixdemo.model.Student">
            SELECT * FROM "student" WHERE id=#{id}
        </select>
        <select id="getAllStudents" resultType="com.example.phoenixdemo.model.Student">
            SELECT * FROM "student"
        </select>
    </mapper>

    (5)由于在 Maven 工程中,XML 配置文件建议写在 resources 目录下,但上面的 StudentMapper.xml 文件写在包下,Maven 在运行时会忽略包下的 XML 文件。因此需要在 pom.xml 文件中重新指明资源文件位置,配置如下:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- 重新指明资源文件位置 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    (6)最后我们在 Controller 中通过调用 StudentMapper 进行数据的增、删、改、查操作。

    @RestController
    public class HelloController {
     
        @Autowired
        StudentMapper studentMapper;
     
        @GetMapping("/test")
        public void test() {
     
            // 插入数据
            System.out.println("\n--- 开始插入数据 ---");
            studentMapper.upsertStudent(new Student("1001","大刘","20"));
            studentMapper.upsertStudent(new Student("1002","小星","22"));
            studentMapper.upsertStudent(new Student("1003","hangge","100"));
     
            // 删除数据
            System.out.println("\n--- 开始删除数据 ---");
            studentMapper.deleteStudentById("1002");
     
            // 查询数据
            System.out.println("\n--- 查询单条数据 ---");
            Student student = studentMapper.getStudentById("1001");
            System.out.println(student);
     
            System.out.println("\n--- 查询多条数据 ---");
            List<Student> list = studentMapper.getAllStudents();
            System.out.println(list);
        }
    }

    4,运行测试

    在浏览器中访问 http://localhost:8080/test 地址,可以看到控制台打印出的日志如下:

     

  • 相关阅读:
    2019-2020-1 20175201 20175215 20175229实验五 通讯协议设计
    2019-2020-1 20175201 20175215 20175229实验四 外设驱动程序设计
    2019-2020-1 实验三-并发程序 20175215
    20175201 20175215 20175229 实验二 固件程序设计
    冲刺博客汇总
    2018-2019-2 20175215 实验五《网络编程与安全》实验报告
    Int和Integer(课上测试)
    MySort(选做)
    2019-2020-2 20175234 赵诗玥 《网络对抗技术》 Exp1 PC平台逆向破解
    2019-2020-2 20175234 赵诗玥《网络对抗技术》 Exp0 Kali安装
  • 原文地址:https://www.cnblogs.com/shanheyongmu/p/15661693.html
Copyright © 2011-2022 走看看