spring/spring boot+mybatis+phoenix
This commit is contained in:
74
code/Phoenix/spring-mybatis-phoenix/pom.xml
Normal file
74
code/Phoenix/spring-mybatis-phoenix/pom.xml
Normal file
@ -0,0 +1,74 @@
|
||||
<?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>
|
||||
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-mybatis-phoenix</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.6.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--spring jdbc-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
<!--phoenix core-->
|
||||
<dependency>
|
||||
<groupId>org.apache.phoenix</groupId>
|
||||
<artifactId>phoenix-core</artifactId>
|
||||
<version>4.14.0-cdh5.14.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class USPopulation {
|
||||
|
||||
private String state;
|
||||
private String city;
|
||||
private long population;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.USPopulation;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PopulationDao {
|
||||
|
||||
List<USPopulation> 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);
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
# <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
phoenix.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver
|
||||
# zookeeper<65><72>ַ
|
||||
phoenix.url=jdbc:phoenix:192.168.0.105:2181
|
@ -0,0 +1,24 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.heibaiying.dao.PopulationDao">
|
||||
|
||||
|
||||
<select id="queryAll" resultType="com.heibaiying.bean.USPopulation">
|
||||
SELECT * FROM us_population
|
||||
</select>
|
||||
|
||||
<insert id="save">
|
||||
UPSERT INTO us_population VALUES( #{state}, #{city}, #{population} )
|
||||
</insert>
|
||||
|
||||
<select id="queryByStateAndCity" resultType="com.heibaiying.bean.USPopulation">
|
||||
SELECT * FROM us_population WHERE state=#{state} AND city = #{city}
|
||||
</select>
|
||||
|
||||
<delete id="deleteByStateAndCity">
|
||||
DELETE FROM us_population WHERE state=#{state} AND city = #{city}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
|
||||
<!-- mybatis 配置文件 -->
|
||||
<configuration>
|
||||
<settings>
|
||||
<!-- 开启驼峰命名 -->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
<!-- 打印查询sql -->
|
||||
<setting name="logImpl" value="STDOUT_LOGGING"/>
|
||||
</settings>
|
||||
|
||||
</configuration>
|
||||
|
||||
<!--更多settings配置项可以参考官方文档: <a href="http://www.mybatis.org/mybatis-3/zh/configuration.html"/>-->
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
|
||||
|
||||
<!-- 开启注解包扫描-->
|
||||
<context:component-scan base-package="com.heibaiying.*"/>
|
||||
|
||||
<!--指定配置文件的位置-->
|
||||
<context:property-placeholder location="classpath:jdbc.properties"/>
|
||||
|
||||
<!--配置数据源-->
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<!--Phoenix配置-->
|
||||
<property name="driverClassName" value="${phoenix.driverClassName}"/>
|
||||
<property name="url" value="${phoenix.url}"/>
|
||||
</bean>
|
||||
|
||||
<!--配置 mybatis 会话工厂 -->
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<!--指定mapper文件所在的位置-->
|
||||
<property name="mapperLocations" value="classpath*:/mappers/**/*.xml"/>
|
||||
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
|
||||
</bean>
|
||||
|
||||
<!--扫描注册接口 -->
|
||||
<!--作用:从接口的基础包开始递归搜索,并将它们注册为 MapperFactoryBean(只有至少一种方法的接口才会被注册;, 具体类将被忽略)-->
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<!--指定会话工厂 -->
|
||||
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
|
||||
<!-- 指定mybatis接口所在的包 -->
|
||||
<property name="basePackage" value="com.heibaiying.dao"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
@ -0,0 +1,50 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.USPopulation;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration({"classpath:springApplication.xml"})
|
||||
public class PopulationDaoTest {
|
||||
|
||||
@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