use of cn.hutool.db.Db in project Jpom by dromara.
the class BaseDbCommonService method insert.
/**
* 插入数据
*
* @param t 数据
*/
public void insert(T t) {
if (!DbConfig.getInstance().isInit()) {
// ignore
log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
return;
}
Db db = Db.use();
db.setWrapper((Character) null);
try {
Entity entity = this.dataBeanToEntity(t);
db.insert(entity);
} catch (Exception e) {
throw warpException(e);
}
}
use of cn.hutool.db.Db in project Jpom by dromara.
the class BaseDbCommonService method listPage.
/**
* 分页查询
*
* @param where 条件
* @param page 分页
* @return 结果
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public PageResultDto<T> listPage(Entity where, Page page) {
if (!DbConfig.getInstance().isInit()) {
// ignore
log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
return PageResultDto.EMPTY;
}
where.setTableName(getTableName());
PageResult<Entity> pageResult;
Db db = Db.use();
db.setWrapper((Character) null);
try {
pageResult = db.page(where, page);
} catch (Exception e) {
throw warpException(e);
}
//
List<T> list = pageResult.stream().map(entity -> {
T entityToBean = this.entityToBean(entity, this.tClass);
this.fillSelectResult(entityToBean);
return entityToBean;
}).collect(Collectors.toList());
PageResultDto<T> pageResultDto = new PageResultDto(pageResult);
pageResultDto.setResult(list);
if (pageResultDto.isEmpty() && pageResultDto.getPage() > 1) {
Assert.state(pageResultDto.getTotal() <= 0, "筛选的分页有问题,当前页码查询不到任何数据");
}
return pageResultDto;
}
use of cn.hutool.db.Db in project Jpom by dromara.
the class BaseDbCommonService method getByKey.
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public T getByKey(String keyValue, boolean fill, Consumer<Entity> consumer) {
if (StrUtil.isEmpty(keyValue)) {
return null;
}
if (!DbConfig.getInstance().isInit()) {
// ignore
log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
return null;
}
Entity where = new Entity(tableName);
where.set(key, keyValue);
Db db = Db.use();
db.setWrapper((Character) null);
if (consumer != null) {
consumer.accept(where);
}
Entity entity;
try {
entity = db.get(where);
} catch (Exception e) {
throw warpException(e);
}
T entityToBean = this.entityToBean(entity, this.tClass);
if (fill) {
this.fillSelectResult(entityToBean);
}
return entityToBean;
}
use of cn.hutool.db.Db in project Jpom by dromara.
the class BaseDbCommonService method count.
/**
* 查询记录条数
*
* @param where 条件
* @return count
*/
public long count(Entity where) {
if (!DbConfig.getInstance().isInit()) {
// ignore
log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
return 0;
}
where.setTableName(getTableName());
Db db = Db.use();
db.setWrapper((Character) null);
try {
return db.count(where);
} catch (Exception e) {
throw warpException(e);
}
}
use of cn.hutool.db.Db in project Jpom by dromara.
the class BaseDbCommonService method queryList.
/**
* 查询列表
*
* @param where 条件
* @return List
*/
public List<Entity> queryList(Entity where) {
if (!DbConfig.getInstance().isInit()) {
// ignore
log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
return null;
}
where.setTableName(getTableName());
Db db = Db.use();
db.setWrapper((Character) null);
try {
return db.find(where);
} catch (Exception e) {
throw warpException(e);
}
}
Aggregations