
com.github.edgar615.util.db.Jdbc Maven / Gradle / Ivy
The newest version!
package com.github.edgar615.util.db;
import com.github.edgar615.util.search.Example;
import com.github.edgar615.util.search.MoreExample;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 数据访问层的接口.
*
* @author Edgar Date 2017/5/22
*/
public interface Jdbc {
/**
* 新增数据.
*
* @param persistent 持久化对象
* @param 主键类型
*/
void insert(Persistent persistent);
/**
* insert一条数据,并返回自增主键
*
* @param persistent 持久化对象
* @param 主键类型
*/
ID insertAndGeneratedKey(Persistent persistent);
/**
* 批量插入
*
* @param persistentList 持久化对象的集合
* @param 主键列席
* @param 持久化对象
*/
> void batchInsert(List persistentList);
/**
* 根据主键删除.
*
* @param elementType 持久化对象
* @param id 主键
* @param 主键类型
* @param 持久化对象
* @return 删除记录数
*/
> int deleteById(Class elementType, ID id);
/**
* 根据条件删除.
*
* @param elementType 持久化对象
* @param example 查询条件
* @param 主键类型
* @param 持久化对象
* @return 删除记录数
*/
> int deleteByExample(Class elementType, Example example);
/**
* 根据主键更新,忽略实体中的null
*
* @param persistent 持久化对象
* @param addOrSub 需要做增加或者减去的字段,value为正数表示增加,负数表示减少
* @param nullFields 需要设为null的字段
* @param id 主键ID
* @param 主键类型
* @return 修改记录数
*/
int updateById(Persistent persistent,
Map addOrSub,
List nullFields,
ID id);
/**
* 根据条件更新,忽略实体中的null
*
* @param persistent 持久化对象
* @param addOrSub 需要做增加或者减去的字段,value为正数表示增加,负数表示减少
* @param nullFields 需要设为null的字段
* @param example 查询条件
* @param 主键类型
* @return 修改记录数
*/
int updateByExample(Persistent persistent,
Map addOrSub,
List nullFields, Example example);
/**
* 根据主键查找.
*
* @param elementType 持久化对象
* @param id 主键
* @param fields 返回的属性列表
* @param 主键类型
* @param 持久化对象
* @return 持久化对象
*/
> T findById(Class elementType, ID id, List fields);
/**
* 根据条件查找.
*
* @param elementType 持久化对象,
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param 主键类型
* @param 持久化对象
* @return 持久化对象列表
*/
> List findByExample(Class elementType, Example example);
/**
* 根据条件查找.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param start 开始索引
* @param limit 查询数量
* @param 主键类型
* @param 持久化对象
* @return 持久化对象列表
*/
> List findByExample(Class elementType, Example example,
int start,
int limit);
/**
* 根据条件查找.
*
* @param elementType 持久化对象
* @param example 查询条件
* @param 主键类型
* @param 持久化对象
* @return 记录数
*/
> int countByExample(Class elementType, Example example);
/**
* 根据主键更新,忽略实体中的null.
*
* @param persistent 持久化对象
* @param id 主键
* @param 主键类型
* @return 修改记录数
*/
default int updateById(Persistent persistent, ID id) {
return updateById(persistent, new HashMap<>(), new ArrayList<>(), id);
}
/**
* 根据条件更新,忽略实体中的null.
*
* @param persistent 持久化对象
* @param example 查询条件
* @param 条件集合
* @return 修改记录数
*/
default int updateByExample(Persistent persistent, Example example) {
return updateByExample(persistent, new HashMap<>(), new ArrayList<>(), example);
}
/**
* 根据条件更新,忽略实体中的null,支持or查询.
*
* @param persistent 持久化对象
* @param example 查询条件
* @param 条件集合
* @return 修改记录数
*/
default int updateByMoreExample(Persistent persistent, MoreExample example) {
return updateByMoreExample(persistent, new HashMap<>(), new ArrayList<>(), example);
}
/**
* 分页查找.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param page 页码
* @param pageSize 每页数量
* @param 主键类型
* @param 持久化对象
* @return 分页对象
*/
default > Pagination pagination(Class elementType,
Example example,
int page, int pageSize) {
Preconditions.checkArgument(page > 0, "page must greater than 0");
Preconditions.checkArgument(pageSize > 0, "pageSize must greater than 0");
//查询总数
final int totalRecords = countByExample(elementType, example);
if (totalRecords == 0) {
return Pagination.newInstance(1, pageSize, totalRecords, Lists.newArrayList());
}
int pageCount = totalRecords / pageSize;
if (totalRecords > pageSize * pageCount) {
pageCount++;
}
if (pageCount < page) {
page = pageCount;
}
int offset = (page - 1) * pageSize;
List records = findByExample(elementType, example, offset, pageSize);
return Pagination.newInstance(page, pageSize, totalRecords, records);
}
/**
* 根据条件查找.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param 主键类型
* @param 持久化对象
* @return 持久化对象,如果未找到任何数据,返回null
*/
default > T findFirstByExample(Class elementType,
Example example) {
List result = findByExample(elementType, example, 0, 1);
if (result == null || result.isEmpty()) {
return null;
}
return result.get(0);
}
/**
* 根据主键查找.
*
* @param elementType 持久化对象
* @param id 主键
* @param 主键类型
* @param 持久化对象
* @return 持久化对象,如果未找到任何数据,返回null
*/
default > T findById(Class elementType, ID id) {
return findById(elementType, id, Lists.newArrayList());
}
/**
* 根据条件查找,并返回总数.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param start 开始索引
* @param limit 查询数量
* @param 主键类型
* @param 持久化对象
* @return 分页对象
*/
default > Page page(Class elementType, Example example,
int start,
int limit) {
List records = findByExample(elementType, example, start, limit);
//如果records的数量小于limit,说明已经没有记录,直接计算总数
if (records.size() > 0 && records.size() < limit) {
int total = start + records.size();
return Page.newInstance(total, records);
}
//通过数据库查询总数
final int totalRecords = countByExample(elementType, example);
return Page.newInstance(totalRecords, records);
}
/**
* 分页查找,支持or查询.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param page 页码
* @param pageSize 每页数量
* @param 主键类型
* @param 持久化对象
* @return 分页对象
*/
default > Pagination paginationMoreExample(Class elementType,
MoreExample example,
int page, int pageSize) {
Preconditions.checkArgument(page > 0, "page must greater than 0");
Preconditions.checkArgument(pageSize > 0, "pageSize must greater than 0");
//查询总数
final int totalRecords = countByMoreExample(elementType, example);
if (totalRecords == 0) {
return Pagination.newInstance(1, pageSize, totalRecords, Lists.newArrayList());
}
int pageCount = totalRecords / pageSize;
if (totalRecords > pageSize * pageCount) {
pageCount++;
}
if (pageCount < page) {
page = pageCount;
}
int offset = (page - 1) * pageSize;
List records = findByMoreExample(elementType, example, offset, pageSize);
return Pagination.newInstance(page, pageSize, totalRecords, records);
}
/**
* 根据条件查找,支持or查询.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param 主键类型
* @param 持久化对象
* @return 持久化对象,如果未找到任何数据,返回null
*/
default > T findFirstByMoreExample(Class elementType,
MoreExample example) {
List result = findByMoreExample(elementType, example, 0, 1);
if (result == null || result.isEmpty()) {
return null;
}
return result.get(0);
}
/**
* 根据条件查找,并返回总数,支持or查询.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param start 开始索引
* @param limit 查询数量
* @param 主键类型
* @param 持久化对象
* @return 分页对象
*/
default > Page pageMoreExample(Class elementType,
MoreExample example,
int start,
int limit) {
List records = findByMoreExample(elementType, example, start, limit);
//如果records的数量小于limit,说明已经没有记录,直接计算总数
if (records.size() > 0 && records.size() < limit) {
int total = start + records.size();
return Page.newInstance(total, records);
}
//通过数据库查询总数
final int totalRecords = countByMoreExample(elementType, example);
return Page.newInstance(totalRecords, records);
}
/**
* 根据条件删除.支持or查询.
*
* @param elementType 持久化对象
* @param example 查询条件
* @param 主键类型
* @param 持久化对象
* @return 删除记录数
*/
> int deleteByMoreExample(Class elementType, MoreExample example);
/**
* 根据条件更新,忽略实体中的null,支持or查询.
*
* @param persistent 持久化对象
* @param addOrSub 需要做增加或者减去的字段,value为正数表示增加,负数表示减少
* @param nullFields 需要设为null的字段
* @param example 查询条件
* @param 主键类型
* @return 修改记录数
*/
int updateByMoreExample(Persistent persistent,
Map addOrSub,
List nullFields, MoreExample example);
/**
* 根据条件查找,支持or查询.
*
* @param elementType 持久化对象,
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param 主键类型
* @param 持久化对象
* @return 持久化对象列表
*/
> List findByMoreExample(Class elementType,
MoreExample example);
/**
* 根据条件查找,支持or查询.
*
* @param elementType 持久化对象
* @param example 查询参数的定义,包括查询条件、排序规则等
* @param start 开始索引
* @param limit 查询数量
* @param 主键类型
* @param 持久化对象
* @return 持久化对象列表
*/
> List findByMoreExample(Class elementType,
MoreExample example,
int start,
int limit);
/**
* 根据条件查找,支持or查询.
*
* @param elementType 持久化对象
* @param example 查询条件
* @param 主键类型
* @param 持久化对象
* @return 记录数
*/
> int countByMoreExample(Class elementType, MoreExample example);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy