diff --git a/README.md b/README.md index 9b9e34b..9dcae7e 100644 --- a/README.md +++ b/README.md @@ -78,13 +78,13 @@ ## 十、HBase 1. [HBase基本环境搭建(Standalone /pseudo-distributed mode)](https://github.com/heibaiying/BigData-Notes/blob/master/notes/installation/Hbase%E5%9F%BA%E6%9C%AC%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA.md) - 2. [HBase系统架构及数据结构](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hbase%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E5%8F%8A%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md) 3. [HBase常用Shell命令](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hbase%20Shell.md) 4. HBase Java API 5. HBase 协处理器 6. HBase 备份与恢复 7. [HBase的SQL中间层——Phoenix](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Hbase%E7%9A%84SQL%E5%B1%82%E2%80%94%E2%80%94Phoenix.md) +8. [Spring/Spring Boot 整合 Mybatis + Phoenix]() ## 十一、Kafka ## 十二、Zookeeper ## 十三、Scala diff --git a/notes/Spring+Mybtais+Phoenix整合.md b/notes/Spring+Mybtais+Phoenix整合.md new file mode 100644 index 0000000..aa27ba2 --- /dev/null +++ b/notes/Spring+Mybtais+Phoenix整合.md @@ -0,0 +1,385 @@ +# Spring/Spring Boot 整合 Mybatis + Phoenix + + +## 一、前言 + +使用Spring+Mybatis操作Phoenix和操作其他的关系型数据库(如Mysql,Oracle)在配置上是基本相同的,下面会分别给出Spring/Spring Boot 整合步骤,完整代码见本仓库: + ++ [Spring + Mybatis + Phoenix](https://github.com/heibaiying/BigData-Notes/tree/master/code/Phoenix/spring-mybatis-phoenix) ++ [SpringBoot + Mybatis + Phoenix](https://github.com/heibaiying/BigData-Notes/tree/master/code/Phoenix/spring-boot-mybatis-phoenix) + +## 二、Spring + Mybatis + Phoenix + +### 2.1 项目结构 + +
+ +### 2.2 主要依赖 + +除了Spring相关依赖外,还需要导入`phoenix-core`和对应的Mybatis依赖包 + +```xml + + + org.mybatis + mybatis-spring + 1.3.2 + + + org.mybatis + mybatis + 3.4.6 + + + + org.apache.phoenix + phoenix-core + 4.14.0-cdh5.14.2 + +``` + +### 2.3 数据库配置文件 + +在数据库配置文件 `jdbc.properties` 中配置数据库驱动和zookeeper地址 + +```properties +# 数据库驱动 +phoenix.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver +# zookeeper地址 +phoenix.url=jdbc:phoenix:192.168.0.105:2181 +``` + +### 2.4 配置数据源和会话工厂 + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +### 2.5 Mybtais参数配置 + +新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) + +```xml + + + + + + + + + + + + +``` + +### 2.6 查询接口 + +```java +public interface PopulationDao { + + List queryAll(); + + void save(USPopulation USPopulation); + + USPopulation queryByStateAndCity(@Param("state") String state, @Param("city") String city); + + void deleteByStateAndCity(@Param("state") String state, @Param("city") String city); +} +``` + +```xml + + + + + + + + + UPSERT INTO us_population VALUES( #{state}, #{city}, #{population} ) + + + + + + DELETE FROM us_population WHERE state=#{state} AND city = #{city} + + + +``` + +### 2.7 单元测试 + +```java +@RunWith(SpringRunner.class) +@ContextConfiguration({"classpath:springApplication.xml"}) +public class PopulationDaoTest { + + @Autowired + private PopulationDao populationDao; + + @Test + public void queryAll() { + List USPopulationList = populationDao.queryAll(); + if (USPopulationList != null) { + for (USPopulation USPopulation : USPopulationList) { + System.out.println(USPopulation.getCity() + " " + USPopulation.getPopulation()); + } + } + } + + @Test + public void save() { + populationDao.save(new USPopulation("TX", "Dallas", 66666)); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } + + @Test + public void update() { + populationDao.save(new USPopulation("TX", "Dallas", 99999)); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } + + + @Test + public void delete() { + populationDao.deleteByStateAndCity("TX", "Dallas"); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } +} +``` + +## 三、SpringBoot + Mybatis + Phoenix + +### 3.1 项目结构 + +
+ +### 3.2 主要依赖 + +```xml + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + org.apache.phoenix + phoenix-core + 4.14.0-cdh5.14.2 + + +``` + +spring boot 与 mybatis 版本的对应关系: + +| MyBatis-Spring-Boot-Starter 版本 | MyBatis-Spring 版本 | Spring Boot 版本 | +| -------------------------------- | ------------------- | ---------------- | +| **1.3.x (1.3.1)** | 1.3 or higher | 1.5 or higher | +| **1.2.x (1.2.1)** | 1.3 or higher | 1.4 or higher | +| **1.1.x (1.1.1)** | 1.3 or higher | 1.3 or higher | +| **1.0.x (1.0.2)** | 1.2 or higher | 1.3 or higher | + +### 3.3 配置数据源 + +在application.yml 中配置数据源,spring boot 2.x 版本默认采用Hikari作为数据库连接池,Hikari是目前java平台性能最好的连接池,性能好于druid。 + +```yaml +spring: + datasource: + #zookeeper地址 + url: jdbc:phoenix:192.168.0.105:2181 + driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver + + # 如果不想配置对数据库连接池做特殊配置的话,以下关于连接池的配置就不是必须的 + # spring-boot 2.X 默认采用高性能的 Hikari 作为连接池 更多配置可以参考 https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby + type: com.zaxxer.hikari.HikariDataSource + hikari: + # 池中维护的最小空闲连接数 + minimum-idle: 10 + # 池中最大连接数,包括闲置和使用中的连接 + maximum-pool-size: 20 + # 此属性控制从池返回的连接的默认自动提交行为。默认为true + auto-commit: true + # 允许最长空闲时间 + idle-timeout: 30000 + # 此属性表示连接池的用户定义名称,主要显示在日志记录和JMX管理控制台中,以标识池和池配置。 默认值:自动生成 + pool-name: custom-hikari + #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟 + max-lifetime: 1800000 + # 数据库连接超时时间,默认30秒,即30000 + connection-timeout: 30000 + # 连接测试sql 这个地方需要根据数据库方言差异而配置 例如 oracle 就应该写成 select 1 from dual + connection-test-query: SELECT 1 + +# mybatis 相关配置 +mybatis: + configuration: + # 是否打印sql语句 调试的时候可以开启 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +``` + +### 3.4 新建查询接口 + +上面Spring+Mybatis我们使用了XML的方式来写SQL,为了体现Mybatis支持多种方式,这里使用注解的方式来写SQL。 + +```java +@Mapper +public interface PopulationDao { + + @Select("SELECT * from us_population") + List queryAll(); + + @Insert("UPSERT INTO us_population VALUES( #{state}, #{city}, #{population} )") + void save(USPopulation USPopulation); + + @Select("SELECT * FROM us_population WHERE state=#{state} AND city = #{city}") + USPopulation queryByStateAndCity(String state, String city); + + + @Delete("DELETE FROM us_population WHERE state=#{state} AND city = #{city}") + void deleteByStateAndCity(String state, String city); +} +``` + +### 3.5 单元测试 + +```java +@RunWith(SpringRunner.class) +@SpringBootTest +public class PopulationTest { + + @Autowired + private PopulationDao populationDao; + + @Test + public void queryAll() { + List USPopulationList = populationDao.queryAll(); + if (USPopulationList != null) { + for (USPopulation USPopulation : USPopulationList) { + System.out.println(USPopulation.getCity() + " " + USPopulation.getPopulation()); + } + } + } + + @Test + public void save() { + populationDao.save(new USPopulation("TX", "Dallas", 66666)); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } + + @Test + public void update() { + populationDao.save(new USPopulation("TX", "Dallas", 99999)); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } + + + @Test + public void delete() { + populationDao.deleteByStateAndCity("TX", "Dallas"); + USPopulation usPopulation = populationDao.queryByStateAndCity("TX", "Dallas"); + System.out.println(usPopulation); + } + +} + +``` + + + +## 附:建表语句 + +上面单元测试涉及到的测试表的建表语句如下: + +```sql +CREATE TABLE IF NOT EXISTS us_population ( + state CHAR(2) NOT NULL, + city VARCHAR NOT NULL, + population BIGINT + CONSTRAINT my_pk PRIMARY KEY (state, city)); + +-- 测试数据 +UPSERT INTO us_population VALUES('NY','New York',8143197); +UPSERT INTO us_population VALUES('CA','Los Angeles',3844829); +UPSERT INTO us_population VALUES('IL','Chicago',2842518); +UPSERT INTO us_population VALUES('TX','Houston',2016582); +UPSERT INTO us_population VALUES('PA','Philadelphia',1463281); +UPSERT INTO us_population VALUES('AZ','Phoenix',1461575); +UPSERT INTO us_population VALUES('TX','San Antonio',1256509); +UPSERT INTO us_population VALUES('CA','San Diego',1255540); +UPSERT INTO us_population VALUES('CA','San Jose',912332); +``` + diff --git a/pictures/spring-boot-mybatis-phoenix.png b/pictures/spring-boot-mybatis-phoenix.png new file mode 100644 index 0000000..174451b Binary files /dev/null and b/pictures/spring-boot-mybatis-phoenix.png differ diff --git a/pictures/spring-mybatis-phoenix.png b/pictures/spring-mybatis-phoenix.png new file mode 100644 index 0000000..4e19d01 Binary files /dev/null and b/pictures/spring-mybatis-phoenix.png differ