Spring Boot + MyBatis + Phoenix集成示例

分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。

Spring Boot + MyBatis + Phoenix集成示例_第1张图片

1. 初始化数据

/usr/local/Cellar/phoenix/apache-phoenix-4.14.1-HBase-1.2-bin/examples/init.sql

-- 创建订单
CREATE TABLE IF NOT EXISTS tbl_order (
	id BIGINT not null primary key,
	order_code char(20),
	total_amount decimal(10,2),
	create_time date,
	user_id bigint
);

-- 插入数据
upsert into tbl_order values(1, 'A001', 10.5, '2019-3-19 23:35:00', 1);
upsert into tbl_order values(2, 'A002', 60.0, '2019-3-19 23:36:00', 2);
upsert into tbl_order values(3, 'B001', 66.6, '2019-3-20 01:01:00', 3);
upsert into tbl_order values(4, 'C001', 66.4, '2019-3-20 02:01:00', 3);

执行sql文件

cd /usr/local/Cellar/phoenix/apache-phoenix-4.14.1-HBase-1.2-bin/bin
./psql.py localhost:2181 ../examples/init.sql

2. pom.xml

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.14version>
dependency>
<dependency>
    <groupId>org.apache.hadoopgroupId>
    <artifactId>hadoop-clientartifactId>
    <version>3.2.0version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        exclusion>
    exclusions>
dependency>

<dependency>
    <groupId>org.apache.phoenixgroupId>
    <artifactId>phoenix-coreartifactId>
    <version>4.14.1-HBase-1.2version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        exclusion>
        <exclusion>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
        exclusion>
    exclusions>
dependency>

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <optional>trueoptional>
dependency>

3. application.properties

# datasource
spring.datasource.driver-class-name=org.apache.phoenix.jdbc.PhoenixDriver
spring.datasource.url=jdbc:phoenix:127.0.0.1:2181
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.data-username=
spring.datasource.data-password=

# mybatis
mybatis.typeAliasesPackage=com.example.phoenix.entity
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

logging.level.com.example.phoenix.mapper=debug

4. entity

@Data
@ToString
@RequiredArgsConstructor
public class Order {
    private Long id;
    private String orderCode;
    private BigDecimal totalAmount;
    private Date createTime;
    private Long userId;
}

5. mapper

public interface OrderMapper {
    List<Order> getOrders();

    void updateOrder(@Param("id") Long id, @Param("totalAmount") BigDecimal totalAmount);
}

6. mapper/OrderMapper.xml



<mapper namespace="com.example.phoenix.mapper.OrderMapper">
    
    <select id="getOrders" resultType="Order">
        SELECT * FROM tbl_order
        WHERE id >= 1
        ORDER BY create_time DESC
        LIMIT 2 OFFSET 1
    select>

    <update id="updateOrder">
        UPSERT INTO tbl_order(id, total_amount) VALUES(#{id}, #{totalAmount})
    update>

mapper>

7. Application

@SpringBootApplication
@MapperScan("com.example.phoenix.mapper")
public class SpringbootMybatisPhoenixApplication {

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

}

8. test

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisPhoenixApplicationTests {

    @Autowired
    private OrderMapper orderMapper;

    @Test
    public void testGetOrders() {
        List<Order> orders = orderMapper.getOrders();
        orders.forEach(System.out::println);
    }

    @Test
    public void testUpdateOrder() {
        orderMapper.updateOrder(2L, BigDecimal.valueOf(88.8));
    }
}

注意:首次连接会打印一条info级别的日志,这不是错误。只会在首次连接的时候会打印此日志。
Spring Boot + MyBatis + Phoenix集成示例_第2张图片
Spring Boot + MyBatis + Phoenix集成示例_第3张图片

分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。

你可能感兴趣的:(#,HBase)