spring/spring boot+mybatis+phoenix
This commit is contained in:
56
code/Phoenix/spring-boot-mybatis-phoenix/pom.xml
Normal file
56
code/Phoenix/spring-boot-mybatis-phoenix/pom.xml
Normal file
@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-boot-mybatis-phoenix</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-mybatis-phoenix</name>
|
||||
<description>mybatis project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--spring 1.5 x 以上版本对应 mybatis 1.3.x (1.3.1)
|
||||
关于更多spring-boot 与 mybatis 的版本对应可以参见 <a href="http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/">-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<!--phoenix core-->
|
||||
<dependency>
|
||||
<groupId>org.apache.phoenix</groupId>
|
||||
<artifactId>phoenix-core</artifactId>
|
||||
<version>4.14.0-cdh5.14.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootMybatisApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootMybatisApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.springboot.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
public class USPopulation {
|
||||
|
||||
private String state;
|
||||
private String city;
|
||||
private long population;
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.heibaiying.springboot.dao;
|
||||
|
||||
import com.heibaiying.springboot.bean.USPopulation;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface PopulationDao {
|
||||
|
||||
@Select("SELECT * from us_population")
|
||||
List<USPopulation> 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);
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
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
|
@ -0,0 +1,53 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import com.heibaiying.springboot.bean.USPopulation;
|
||||
import com.heibaiying.springboot.dao.PopulationDao;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PopulationTest {
|
||||
|
||||
@Autowired
|
||||
private PopulationDao populationDao;
|
||||
|
||||
@Test
|
||||
public void queryAll() {
|
||||
List<USPopulation> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user