Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
medzeus2 committed Oct 9, 2019
0 parents commit 27355a6
Show file tree
Hide file tree
Showing 20 changed files with 1,060 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Eclipse
.classpath
.project
.settings/

# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
log/
target/

# Java
*.class
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DBlib是常用关于数据库的操作库

### 为什么要有dblib
dblib总结使用JDBC数据库过程中常用的执行的方法,类似Spring JdbcTemplate。目前Spring JDBCTemplate主要对分页
支持较差
74 changes: 74 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -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>top.zeus2</groupId>
<artifactId>dblib</artifactId>
<packaging>pom</packaging>
<version>0.1</version>
<modules>
<module>../gold</module>
</modules>

<name>dblib</name>
<!-- FIXME change it to the project's website -->
<url>http://www.zeus2.top</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4.0-atlassian-hosted</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.28</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

</build>
</project>
8 changes: 8 additions & 0 deletions src/main/java/top/zeus2/data/DbType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package top.zeus2.data;

public enum DbType {
UNKNOWN,
ORACLE,
DB2,
MYSQL
}
118 changes: 118 additions & 0 deletions src/main/java/top/zeus2/data/PageJdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package top.zeus2.data;

import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import top.zeus2.data.impl.DataSourceTypeUtil;
import top.zeus2.data.page.IPageSqlAnalysis;
import top.zeus2.data.page.PagedSqlManager;
import top.zeus2.data.sql.SqlBuilder;

public class PageJdbcTemplate extends JdbcTemplate implements PageOperator {

/** 默认构造函数. */
public PageJdbcTemplate() {}

public PageJdbcTemplate(DataSource dataSource) {
super(dataSource);
}

/**
* 分页查询操作.
*
* @param sql sql语句
* @param args 查询参数
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param requireType 泛型对象类
* @return 分页结果
*/
@Override
public <T> PageResult<T> queryForPageResult(
String sql, Object[] args, PageRequest pageRequest, Class<T> requireType)
throws DataAccessException {
return queryForPageResult(sql, args, pageRequest, new BeanPropertyRowMapper<T>(requireType));
}

/**
* 分页查询操作
*
* @param sql sql语句
* @param args 查询参数
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param rowMapper
* @return 分页结果
*/
@Override
public <T> PageResult<T> queryForPageResult(
String sql, Object[] args, PageRequest pageRequest, RowMapper<T> rowMapper)
throws DataAccessException {
IPageSqlAnalysis psa =
PagedSqlManager.create(DataSourceTypeUtil.getDbTypefromDataSource(this.getDataSource()));
PageResult<T> pageResult = new PageResult<T>();
psa.setSqlText(sql);

String sql_count = psa.getCountSql();
String sql_data = psa.getPageSql(pageRequest);
Long count = -1L;
if (pageRequest.isFetchcount()) {
count = (Long) this.queryForObject(sql_count, args, Long.class);
}
pageResult.setCount(count);

List<T> data = queryForBeanList(sql_data, args, rowMapper);
pageResult.setData(data);

return pageResult;
}

/**
* 分页查询操作
*
* @param sqlBuilder sql分解片段
* @param args 查询参数
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param rowMapper
* @return 分页结果
*/
@Override
public <T> PageResult<T> queryForPageResult(
SqlBuilder sqlBuilder, Object[] args, PageRequest pageRequest, RowMapper<T> rowMapper)
throws DataAccessException {
return null;
}

/**
* 通过SQL查询BeanList对象
*
* @param sql sql 文本
* @param args 查询参数,如果没有传入null
* @param requireType T的类型
* @return t的List数组
* @throws DataAccessException
*/
@Override
public <T> List<T> queryForBeanList(String sql, Object[] args, Class<T> requireType)
throws DataAccessException {
return queryForBeanList(sql, args, new BeanPropertyRowMapper<T>(requireType));
}

/**
* 通过SQL查询BeanList对象
*
* @param sql sql 文本
* @param args 查询参数,如果没有传入null
* @param rowMapper 自定义对象映射关系
* @return t的List数组
* @throws DataAccessException
*/
@Override
public <T> List<T> queryForBeanList(String sql, Object[] args, RowMapper<T> rowMapper)
throws DataAccessException {
List<T> results = this.query(sql, args, new RowMapperResultSetExtractor<>(rowMapper));
return results;
}
}
80 changes: 80 additions & 0 deletions src/main/java/top/zeus2/data/PageOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package top.zeus2.data;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import top.zeus2.data.sql.SqlBuilder;

/** 关于分页的接口 */
public interface PageOperator {

/**
* 分页查询操作.
*
* @param sql sql语句
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param args 查询参数
* @param requireType 泛型对象类
* @param <T> 返回对象泛型类型
* @return 分页结果
*/
<T> PageResult<T> queryForPageResult(
String sql, @Nullable Object[] args, PageRequest pageRequest, Class<T> requireType)
throws DataAccessException;

/**
* 分页查询操作
*
* @param sql sql语句
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param args 查询参数
* @param <T> 返回对象泛型类型
* @return 分页结果
*/
<T> PageResult<T> queryForPageResult(
String sql, @Nullable Object[] args, PageRequest pageRequest, RowMapper<T> rowMapper)
throws DataAccessException;

/**
* 分页查询操作
*
* @param sqlBuilder sql分解片段
* @param pageRequest 分页请求,例如第几页,每页多少条
* @param args 查询参数
* @param <T> 返回对象泛型类型
* @return 分页结果
*/
<T> PageResult<T> queryForPageResult(
SqlBuilder sqlBuilder,
@Nullable Object[] args,
PageRequest pageRequest,
RowMapper<T> rowMapper)
throws DataAccessException;

/**
* 通过SQL查询BeanList对象
*
* @param sql sql 文本
* @param args 查询参数,如果没有传入null
* @param <T> 泛型实体对象
* @param requireType T的类型
* @return t的List数组
* @throws DataAccessException
*/
<T> List<T> queryForBeanList(String sql, @Nullable Object[] args, Class<T> requireType)
throws DataAccessException;

/**
* 通过SQL查询BeanList对象
*
* @param sql sql 文本
* @param args 查询参数,如果没有传入null
* @param <T> 泛型实体对象
* @param rowMapper 自定义对象映射关系
* @return t的List数组
* @throws DataAccessException
*/
<T> List<T> queryForBeanList(String sql, @Nullable Object[] args, RowMapper<T> rowMapper)
throws DataAccessException;
}
71 changes: 71 additions & 0 deletions src/main/java/top/zeus2/data/PageRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package top.zeus2.data;

import lombok.Data;

/**
* 自动生成分页请求,支持start end模式及 页面大小页码模式。
*/
@Data
public class PageRequest {

private int start;
private int end;
private int pagesize;
private int pageindex;
/** 是否获取记录总数,在分页时,第二页就没必要获取记录总数 */
private boolean fetchcount = true;

private PageRequest() {}

/**
* 根据start end创建分页请求
*
* @param start 起始记录数
* @param end 结束记录数
* @return
*/
public static PageRequest buildwithStartEnd(int start, int end) {
if (start <= 0) {
throw new IllegalArgumentException("start cannot less than 1");
}
if (end - start <= 0) {
throw new IllegalArgumentException("end cannot less than start");
}

PageRequest request = new PageRequest();
request.setPagesize(end - start + 1);
request.setStart(start);
request.setEnd(end);
if ((start - 1) % request.getPagesize() != 0) {
throw new IllegalArgumentException(
"start end not correct, cannot convert to pageindex and pagesize");
}
int pageindex = (start - 1) / request.getPagesize() + 1;
request.setPageindex(pageindex);
return request;
}

/**
* 按照index索引分页
*
* @param pageindex 分页索引 第几页
* @param pagesize 分页大小 每页多少条
* @return PageRequest instance
*/
public static PageRequest buildwithIndexSize(int pageindex, int pagesize) {
if (pageindex <= 0) {
throw new IllegalArgumentException("pageindex cannot less than 1");
}
if (pagesize <= 0) {
throw new IllegalArgumentException("pagesize cannot less than 0");
}
int start = (pageindex - 1) * pagesize + 1;
int end = start + pagesize;
PageRequest request = new PageRequest();
request.setPageindex(pageindex);
request.setPagesize(pagesize);
request.setStart(start);
request.setEnd(end);
return request;
}
}
Loading

0 comments on commit 27355a6

Please sign in to comment.