登陆

Spring Boot + MyBatis + Phoenix集成示例

admin 2019-10-31 130人围观 ,发现0个评论

本号主要用于分享企业中常用的技术,更加侧重于实用,欢迎关注,便于浏览其它更多实用的历史文章。

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/CellaSpring Boot + MyBatis + Phoenix集成示例r/phoenix/apache-phoenix-4.14.1-HBase-1.2-bin/bin
./psql.py localhost:2181 ../examples/init.sql

2. pom.xml


com.alibaba
druid
1.1.14


org.apache.hadoop
hadoop-client
3.2.0


org.slf4j
slfSpring Boot + MyBatis + Phoenix集成示例4j-log4j12





org.apache.phoenix
phoenix-core
4.14.1-HBase-1.2


org.slf4j
slf4j-log4j12


log4j
log4j




log4j
log4j
1.2.17


org.projectlombok
lombok
true

3. application.properties

# datasource
spring.datasource.driver-class-name=org.apache.phoenix.jdbc.PhoenixDriver
spring.datasource.urSpring Boot + MyBatis + Phoenix集成示例l=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 getOrders();
void updateOrder(@Param("id") Long id, @Param("totalAmount") BigDecimal totalAmount);
}

6. mapper/OrderMapper.xml







UPSERT INTO tbl_order(id, total_amount) VALUES(#{id}, #{totalAmount})


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 orders = orderMapper.getOrders();
orders.forEach(System.out::println);
}
@Test
public void testUpdateOrder() {
orderMapper.updateOrder(2L, BigDecimal.valueOf(88.8));
}
}

注意:首次连接会打印一条info级别的日志,这不是错误。只会在首次连接的时候会打印此日志。

本号主要用于分享企业中常用的技术,更加侧重于实用,欢迎关注,便于浏览其它更多实用的历史文章。

请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP