-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
hbase-starter/src/main/java/phoenix_mybatis/dao/UserInfoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
14 changes: 14 additions & 0 deletions
14
hbase-starter/src/main/java/phoenix_mybatis/mybatis/HikariDataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
hbase-starter/src/main/java/phoenix_mybatis/mybatis/PhoenixDataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
11 changes: 11 additions & 0 deletions
11
hbase-starter/src/main/resources/phoenix_mybatis/dao/UserInfoMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |