Skip to content

Commit

Permalink
加入Phoenix集成mybatis例子
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyofin committed Jul 1, 2019
1 parent 642efe2 commit 8ff3091
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 0 deletions.
60 changes: 60 additions & 0 deletions hbase-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,65 @@
<artifactId>phoenix-core</artifactId>
<version>4.14.0-HBase-1.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions hbase-starter/src/main/java/phoenix_mybatis/UserInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package phoenix_mybatis;

/**
* on 18-3-11.
*/
public class UserInfo {

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package phoenix_mybatis.dao;

import org.apache.ibatis.annotations.*;
import phoenix_mybatis.UserInfo;

import java.util.List;

/**
* on 18-3-11.
*/
@Mapper
public interface UserInfoMapper {

@Insert("upsert into USER_INFO (ID,NAME) VALUES (#{user.id},#{user.name})")
public void addUser(@Param("user") UserInfo userInfo);

@Delete("delete from USER_INFO WHERE ID=#{userId}")
public void deleteUser(@Param("userId") int userId);

@Select("select * from USER_INFO WHERE ID=#{userId}")
@ResultMap("userResultMap")
public UserInfo getUserById(@Param("userId") int userId);

@Select("select * from USER_INFO WHERE NAME=#{userName}")
@ResultMap("userResultMap")
public UserInfo getUserByName(@Param("userName") String userName);

@Select("select * from USER_INFO")
@ResultMap("userResultMap")
public List<UserInfo> getUsers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package phoenix_mybatis.mybatis;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

/**
* on 18-3-11.
*/
public class HikariDataSourceFactory extends UnpooledDataSourceFactory {

public HikariDataSourceFactory() {
this.dataSource = new HikariDataSource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package phoenix_mybatis.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

/**
* on 18-3-11.
*/
@Configuration
@MapperScan(basePackages = PhoenixDataSourceConfig.PACKAGE,
sqlSessionFactoryRef = "PhoenixSqlSessionFactory")
public class PhoenixDataSourceConfig {

static final String PACKAGE = "phoenix_mybatis.**";

@Bean(name = "PhoenixDataSource")
@Primary
public DataSource phoenixDataSource() throws IOException {
ResourceLoader loader = new DefaultResourceLoader();
InputStream inputStream = loader.getResource("classpath:application.properties")
.getInputStream();
Properties properties = new Properties();
properties.load(inputStream);
Set<Object> keys = properties.keySet();
Properties dsProperties = new Properties();
for (Object key : keys) {
if (key.toString().startsWith("datasource")) {
dsProperties.put(key.toString().replace("datasource.", ""), properties.get(key));
}
}
HikariDataSourceFactory factory = new HikariDataSourceFactory();
factory.setProperties(dsProperties);
inputStream.close();
return factory.getDataSource();
}

@Bean(name = "PhoenixSqlSessionFactory")
@Primary
public SqlSessionFactory phoenixSqlSessionFactory(
@Qualifier("PhoenixDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
ResourceLoader loader = new DefaultResourceLoader();
String resource = "classpath:mybatis-config.xml";
factoryBean.setConfigLocation(loader.getResource(resource));
factoryBean.setSqlSessionFactoryBuilder(new SqlSessionFactoryBuilder());
return factoryBean.getObject();
}
}
9 changes: 9 additions & 0 deletions hbase-starter/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mybatis.config-location=mybatis-config.xml
datasource.jdbcUrl=jdbc:phoenix:localhost:2181
datasource.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver
datasource.maxPoolSize=20
datasource.minIdle=2
datasource.validationTimeout=300000
datasource.idleTimeout=600000
datasource.connectionTestQuery=select 1+1
mybatis.mapperLocations=
5 changes: 5 additions & 0 deletions hbase-starter/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
log4j.rootLogger=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
23 changes: 23 additions & 0 deletions hbase-starter/src/main/resources/mybatis-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="phoenix_mybatis.dao.UserInfoMapper">
<resultMap id="userResultMap"
type="phoenix_mybatis.UserInfo">
<id column="ID" property="id"/>
<result column="NAME" property="name"/>
</resultMap>
</mapper>
58 changes: 58 additions & 0 deletions hbase-starter/src/test/java/phoenix_mybatis/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package phoenix_mybatis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import phoenix_mybatis.dao.UserInfoMapper;
import phoenix_mybatis.mybatis.PhoenixDataSourceConfig;

import java.util.List;

/**
* on 18-3-11.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Import(PhoenixDataSourceConfig.class)
@PropertySource("classpath:application.properties")
@ComponentScan("phoenix_mybatis.**")
@MapperScan("phoenix_mybatis.**")
public class BaseTest {

@Autowired
UserInfoMapper userInfoMapper;

@Test
public void addUser() {
UserInfo userInfo = new UserInfo();
userInfo.setId(1);
userInfo.setName("Jerry");
userInfoMapper.addUser(userInfo);
}

@Test
public void getUserById() {
UserInfo userInfo = userInfoMapper.getUserById(1);
System.out.println(String.format("ID=%s;NAME=%s", userInfo.getId(), userInfo.getName()));
}

@Test
public void getUserByName() {
UserInfo userInfo = userInfoMapper.getUserByName("Jerry");
System.out.println(String.format("ID=%s;NAME=%s", userInfo.getId(), userInfo.getName()));
}

@Test
public void deleteUser() {
userInfoMapper.deleteUser(1);

List<UserInfo> userInfos = userInfoMapper.getUsers();
for (UserInfo userInfo : userInfos) {
System.out.println(String.format("ID=%s;NAME=%s", userInfo.getId(), userInfo.getName()));
}
}
}

0 comments on commit 8ff3091

Please sign in to comment.