fix db method

This commit is contained in:
bwcx_jzy 2023-03-16 17:46:23 +08:00
parent 415ba0a6df
commit ef49954856
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
5 changed files with 249 additions and 334 deletions

View File

@ -28,11 +28,6 @@ package io.jpom.common;
*/
public class ServerConst extends Const {
/**
* String const
*/
public static final String ID_STR = "id";
public static final String GROUP_STR = "group";
/**

View File

@ -25,7 +25,6 @@ package io.jpom.controller.docker;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import io.jpom.common.JsonMessage;
import io.jpom.common.validator.ValidatorItem;
import io.jpom.controller.docker.base.BaseDockerSwarmInfoController;
@ -138,9 +137,10 @@ public class DockerSwarmInfoController extends BaseDockerSwarmInfoController {
allTag = allTag.stream().filter(StrUtil::isNotEmpty).collect(Collectors.toSet());
String newTags = CollUtil.join(allTag, StrUtil.COLON, StrUtil.COLON, StrUtil.COLON);
//
Entity where = Entity.create().set("id", dockerInfoModel.getId());
Entity update = Entity.create().set("tags", newTags);
dockerInfoService.update(update, where);
DockerInfoModel update = new DockerInfoModel();
update.setId(dockerInfoModel.getId());
update.setTags(newTags);
dockerInfoService.updateById(update);
}
}
//

View File

@ -37,7 +37,6 @@ import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;
import cn.hutool.extra.servlet.ServletUtil;
import io.jpom.common.BaseServerController;
import io.jpom.common.ServerConst;
import io.jpom.model.BaseUserModifyDbModel;
import io.jpom.model.user.UserModel;
import lombok.extern.slf4j.Slf4j;
@ -46,7 +45,6 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.util.Assert;
import top.jpom.db.DbExtConfig;
import top.jpom.h2db.BaseDbCommonService;
import top.jpom.h2db.TableName;
import top.jpom.model.BaseDbModel;
import top.jpom.model.PageResultDto;
@ -80,21 +78,9 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
new Order("modifyTimeMillis", Direction.DESC)
};
public BaseDbService() {
super(ServerConst.ID_STR);
}
@Override
protected String covetTableName(Class<T> tClass) {
TableName annotation = tClass.getAnnotation(TableName.class);
Assert.notNull(annotation, "请配置 table Name");
return annotation.value();
}
@Override
public void insert(T t) {
this.fillInsert(t);
super.insert(t);
super.insertDb(t);
this.executeClear();
}
@ -110,11 +96,10 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
}
}
@Override
public void insert(Collection<T> t) {
// def create time
t.forEach(this::fillInsert);
super.insert(t);
super.insertDb(t);
this.executeClear();
}
@ -167,19 +152,50 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
//
Entity entity = this.dataBeanToEntity(info);
//
entity.remove(StrUtil.format("`{}`", ServerConst.ID_STR));
entity.remove(StrUtil.format("`{}`", ID_STR));
//
Entity where = new Entity();
where.set(ServerConst.ID_STR, id);
where.set(ID_STR, id);
if (whereConsumer != null) {
whereConsumer.accept(where);
}
int update = super.update(entity, where);
int update = super.updateDb(entity, where);
// backtrack
info.setCreateTimeMillis(createTimeMillis);
return update;
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public T getByKey(String keyValue) {
return this.getByKey(keyValue, true);
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public List<T> getByKey(Collection<String> keyValue) {
return this.getByKey(keyValue, true, null);
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public T getByKey(String keyValue, boolean fill) {
return this.getByKey(keyValue, fill, null);
}
/**
* update by id with data
*
@ -190,10 +206,168 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
return this.updateById(info, null);
}
@Override
public int update(Entity entity, Entity where) {
entity.remove("createUser");
return super.update(entity, where);
return super.updateDb(entity, where);
}
/**
* 根据主键生成
*
* @param keyValue 主键值
* @return 影响行数
*/
public int delByKey(String keyValue) {
return this.delByKey(keyValue, null);
}
/**
* 根据主键生成
*
* @param keyValue 主键值
* @param consumer 回调
* @return 影响行数
*/
public int delByKey(Object keyValue, Consumer<Entity> consumer) {
Entity where = new Entity(tableName);
if (keyValue != null) {
where.set(ID_STR, keyValue);
}
if (consumer != null) {
consumer.accept(where);
}
Assert.state(where.size() > 0, "没有添加任何参数:-1");
return del(where);
}
/**
* 判断是否存在
*
* @param data 实体
* @return true 存在
*/
public boolean exists(T data) {
Entity entity = this.dataBeanToEntity(data);
return this.exists(entity);
}
/**
* 判断是否存在
*
* @param where 条件
* @return true 存在
*/
public boolean exists(Entity where) {
long count = this.count(where);
return count > 0;
}
/**
* 查询一个
*
* @param where 条件
* @return Entity
*/
public Entity query(Entity where) {
List<Entity> entities = this.queryList(where);
return CollUtil.getFirst(entities);
}
/**
* 查询 list
*
* @param where 条件
* @return data
*/
public List<T> listByEntity(Entity where) {
List<Entity> entity = this.queryList(where);
return this.entityToBeanList(entity);
}
/**
* 查询列表
*
* @param data 数据
* @param count 查询数量
* @param orders 排序
* @return List
*/
public List<T> queryList(T data, int count, Order... orders) {
Entity where = this.dataBeanToEntity(data);
Page page = new Page(1, count);
page.addOrder(orders);
PageResultDto<T> tPageResultDto = this.listPage(where, page);
return tPageResultDto.getResult();
}
/**
* 分页查询
*
* @param where 条件
* @param page 分页
* @return 结果
*/
public PageResultDto<T> listPage(Entity where, Page page) {
return this.listPage(where, page, true);
}
/**
* 分页查询
*
* @param where 条件
* @param page 分页
* @return 结果
*/
public List<T> listPageOnlyResult(Entity where, Page page) {
PageResultDto<T> pageResultDto = this.listPage(where, page);
return pageResultDto.getResult();
}
/**
* sql 查询 list
*
* @param sql sql 语句
* @param params 参数
* @return list
*/
public List<T> queryList(String sql, Object... params) {
List<Entity> query = this.query(sql, params);
return this.entityToBeanList(query);
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public List<T> listByBean(T data) {
return this.listByBean(data, true);
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public List<T> listByBean(T data, boolean fill) {
Entity where = this.dataBeanToEntity(data);
List<Entity> entitys = this.queryList(where);
return this.entityToBeanList(entitys, fill);
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public T queryByBean(T data) {
Entity where = this.dataBeanToEntity(data);
Entity entity = this.query(where);
return this.entityToBean(entity, true);
}
public List<T> list() {
@ -397,7 +571,7 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
return null;
}
Entity entity = Entity.create();
entity.set(ServerConst.ID_STR, ids);
entity.set(ID_STR, ids);
if (consumer != null) {
consumer.accept(entity);
}
@ -482,7 +656,7 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
page.addOrder(new Order(timeColumn, Direction.DESC));
PageResultDto<T> pageResult;
try {
pageResult = super.listPage(entity, page);
pageResult = this.listPage(entity, page);
} catch (java.lang.IllegalStateException illegalStateException) {
return 0L;
} catch (Exception e) {
@ -522,13 +696,13 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
while (true) {
Page page = new Page(1, 50);
page.addOrder(new Order(timeClo, Direction.DESC));
PageResultDto<T> pageResult = super.listPage(entity, page);
PageResultDto<T> pageResult = this.listPage(entity, page);
if (pageResult.isEmpty()) {
return;
}
// pageResult.each(consumer);
List<String> ids = pageResult.getResult().stream().filter(predicate).map(BaseDbModel::getId).collect(Collectors.toList());
super.delByKey(ids, null);
this.delByKey(ids, null);
}
});
}
@ -541,6 +715,6 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
* @return data
*/
public T getData(String nodeId, String dataId) {
return super.getByKey(dataId);
return this.getByKey(dataId);
}
}

View File

@ -34,7 +34,6 @@ import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.sql.Condition;
import cn.hutool.db.sql.Order;
import io.jpom.system.JpomRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
@ -63,40 +62,30 @@ public abstract class BaseDbCommonService<T> {
PageUtil.setFirstPageNo(1);
}
/**
* String const
*/
public static final String ID_STR = "id";
/**
* 表名
*/
protected final String tableName;
protected final Class<T> tClass;
/**
* 主键
*/
protected final String key;
@SuppressWarnings("unchecked")
public BaseDbCommonService(String key) {
public BaseDbCommonService() {
this.tClass = (Class<T>) TypeUtil.getTypeArgument(this.getClass());
this.tableName = this.covetTableName(this.tClass);
this.key = key;
TableName annotation = tClass.getAnnotation(TableName.class);
Assert.notNull(annotation, "请配置 table Name");
this.tableName = annotation.value();
}
/**
* 转换表名
*
* @param tClass
* @return 转换后的表名
*/
protected abstract String covetTableName(Class<T> tClass);
public String getTableName() {
return tableName;
}
protected String getKey() {
return key;
}
private DataSource getDataSource() {
DSFactory dsFactory = StorageServiceFactory.get().getDsFactory();
return dsFactory.getDataSource();
@ -107,7 +96,7 @@ public abstract class BaseDbCommonService<T> {
*
* @param t 数据
*/
public void insert(T t) {
protected void insertDb(T t) {
Db db = Db.use(this.getDataSource());
try {
Entity entity = this.dataBeanToEntity(t);
@ -122,7 +111,7 @@ public abstract class BaseDbCommonService<T> {
*
* @param t 数据
*/
public void insert(Collection<T> t) {
protected void insertDb(Collection<T> t) {
if (CollUtil.isEmpty(t)) {
return;
}
@ -157,7 +146,7 @@ public abstract class BaseDbCommonService<T> {
* @param where 条件
* @return 影响行数
*/
public int update(Entity entity, Entity where) {
protected int updateDb(Entity entity, Entity where) {
Db db = Db.use(this.getDataSource());
if (where.isEmpty()) {
throw new JpomRuntimeException("没有更新条件");
@ -171,36 +160,6 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public T getByKey(String keyValue) {
return this.getByKey(keyValue, true);
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public List<T> getByKey(Collection<String> keyValue) {
return this.getByKey(keyValue, true, null);
}
/**
* 根据主键查询实体
*
* @param keyValue 主键值
* @return 数据
*/
public T getByKey(String keyValue, boolean fill) {
return this.getByKey(keyValue, fill, null);
}
/**
* 根据主键查询实体
*
@ -214,7 +173,7 @@ public abstract class BaseDbCommonService<T> {
return null;
}
Entity where = new Entity(tableName);
where.set(key, keyValue);
where.set(ID_STR, keyValue);
Entity entity;
try {
Db db = Db.use(this.getDataSource());
@ -225,11 +184,7 @@ public abstract class BaseDbCommonService<T> {
} catch (Exception e) {
throw warpException(e);
}
T entityToBean = this.entityToBean(entity, this.tClass);
if (fill) {
this.fillSelectResult(entityToBean);
}
return entityToBean;
return this.entityToBean(entity, fill);
}
/**
@ -245,7 +200,7 @@ public abstract class BaseDbCommonService<T> {
return null;
}
Entity where = new Entity(tableName);
where.set(key, keyValue);
where.set(ID_STR, keyValue);
List<Entity> entities;
try {
Db db = Db.use(this.getDataSource());
@ -256,82 +211,7 @@ public abstract class BaseDbCommonService<T> {
} catch (Exception e) {
throw warpException(e);
}
return entities.stream().map(entity -> {
//
T toBean = this.entityToBean(entity, this.tClass);
if (fill) {
this.fillSelectResult(toBean);
}
return toBean;
}).collect(Collectors.toList());
}
/**
* entity 实体
*
* @param entity Entity
* @param rClass 实体类
* @param <R> 乏型
* @return data
*/
protected <R> R entityToBean(Entity entity, Class<R> rClass) {
if (entity == null) {
return null;
}
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreError(true);
copyOptions.setIgnoreCase(true);
return BeanUtil.toBean(entity, rClass, copyOptions);
}
/**
* entity 实体
*
* @param entity Entity
* @return data
*/
public T entityToBean(Entity entity) {
if (entity == null) {
return null;
}
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreError(true);
copyOptions.setIgnoreCase(true);
T toBean = BeanUtil.toBean(entity, this.tClass, copyOptions);
this.fillSelectResult(toBean);
return toBean;
}
/**
* 根据主键生成
*
* @param keyValue 主键值
* @return 影响行数
*/
public int delByKey(String keyValue) {
return this.delByKey(keyValue, null);
}
/**
* 根据主键生成
*
* @param keyValue 主键值
* @param consumer 回调
* @return 影响行数
*/
public int delByKey(Object keyValue, Consumer<Entity> consumer) {
// if (ObjectUtil.isEmpty(keyValue)) {
// return 0;
// }
Entity where = new Entity(tableName);
if (keyValue != null) {
where.set(key, keyValue);
}
if (consumer != null) {
consumer.accept(where);
}
Assert.state(where.size() > 0, "没有添加任何参数:-1");
return del(where);
return this.entityToBeanList(entities, fill);
}
/**
@ -353,28 +233,6 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* 判断是否存在
*
* @param data 实体
* @return true 存在
*/
public boolean exists(T data) {
Entity entity = this.dataBeanToEntity(data);
return this.exists(entity);
}
/**
* 判断是否存在
*
* @param where 条件
* @return true 存在
*/
public boolean exists(Entity where) {
long count = this.count(where);
return count > 0;
}
/**
* 查询记录条数
*
@ -405,27 +263,6 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* 查询一个
*
* @param where 条件
* @return Entity
*/
public Entity query(Entity where) {
List<Entity> entities = this.queryList(where);
return CollUtil.getFirst(entities);
}
/**
* 查询 list
*
* @param where 条件
* @return data
*/
public List<T> listByEntity(Entity where) {
List<Entity> entity = this.queryList(where);
return this.entityToBeanList(entity);
}
/**
* 查询列表
@ -460,30 +297,37 @@ public abstract class BaseDbCommonService<T> {
}
/**
* 查询列表
* entity 实体
*
* @param data 数据
* @param count 查询数量
* @param orders 排序
* @return List
* @param entity Entity
* @param fill 是否填充
* @return data
*/
public List<T> queryList(T data, int count, Order... orders) {
Entity where = this.dataBeanToEntity(data);
Page page = new Page(1, count);
page.addOrder(orders);
PageResultDto<T> tPageResultDto = this.listPage(where, page);
return tPageResultDto.getResult();
protected T entityToBean(Entity entity, boolean fill) {
if (entity == null) {
return null;
}
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreError(true);
copyOptions.setIgnoreCase(true);
T toBean = BeanUtil.toBean(entity, this.tClass, copyOptions);
if (fill) {
this.fillSelectResult(toBean);
}
return toBean;
}
/**
* 分页查询
*
* @param where 条件
* @param page 分页
* @return 结果
*/
public PageResultDto<T> listPage(Entity where, Page page) {
return this.listPage(where, page, true);
public List<T> entityToBeanList(List<Entity> entitys) {
return this.entityToBeanList(entitys, true);
}
public List<T> entityToBeanList(List<Entity> entitys, boolean fill) {
if (entitys == null) {
return null;
}
return entitys.stream()
.map((entity -> this.entityToBean(entity, fill)))
.collect(Collectors.toList());
}
/**
@ -505,13 +349,7 @@ public abstract class BaseDbCommonService<T> {
throw warpException(e);
}
//
List<T> list = pageResult.stream().map(entity -> {
T entityToBean = this.entityToBean(entity, this.tClass);
if (fill) {
this.fillSelectResult(entityToBean);
}
return entityToBean;
}).collect(Collectors.toList());
List<T> list = this.entityToBeanList(pageResult, fill);
PageResultDto<T> pageResultDto = new PageResultDto(pageResult);
pageResultDto.setResult(list);
if (pageResultDto.isEmpty() && pageResultDto.getPage() > 1) {
@ -520,17 +358,6 @@ public abstract class BaseDbCommonService<T> {
return pageResultDto;
}
/**
* 分页查询
*
* @param where 条件
* @param page 分页
* @return 结果
*/
public List<T> listPageOnlyResult(Entity where, Page page) {
PageResultDto<T> pageResultDto = this.listPage(where, page);
return pageResultDto.getResult();
}
/**
* sql 查询
@ -562,80 +389,6 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* sql 查询 list
*
* @param sql sql 语句
* @param params 参数
* @return list
*/
public List<T> queryList(String sql, Object... params) {
List<Entity> query = this.query(sql, params);
return this.entityToBeanList(query);
// if (query != null) {
// return query.stream().map((entity -> {
// T entityToBean = this.entityToBean(entity, this.tClass);
// this.fillSelectResult(entityToBean);
// return entityToBean;
// })).collect(Collectors.toList());
// }
// return null;
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public List<T> listByBean(T data) {
return this.listByBean(data, true);
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public List<T> listByBean(T data, boolean fill) {
Entity where = this.dataBeanToEntity(data);
List<Entity> entitys = this.queryList(where);
return this.entityToBeanList(entitys, fill);
}
public List<T> entityToBeanList(List<Entity> entitys) {
return this.entityToBeanList(entitys, true);
}
public List<T> entityToBeanList(List<Entity> entitys, boolean fill) {
if (entitys == null) {
return null;
}
return entitys.stream().map((entity -> {
T entityToBean = this.entityToBean(entity, this.tClass);
if (fill) {
this.fillSelectResult(entityToBean);
}
return entityToBean;
})).collect(Collectors.toList());
}
/**
* 查询实体对象
*
* @param data 实体
* @return data
*/
public T queryByBean(T data) {
Entity where = this.dataBeanToEntity(data);
Entity entity = this.query(where);
T entityToBean = this.entityToBean(entity, this.tClass);
this.fillSelectResult(entityToBean);
return entityToBean;
}
/**
* 查询结果 填充
*

View File

@ -68,13 +68,6 @@ public class PageResultDto<T> implements Serializable {
this.setTotal(pageResult.getTotal());
}
public PageResultDto(PageResultDto<?> pageResult) {
this.setPage(pageResult.getPage());
this.setPageSize(pageResult.getPageSize());
this.setTotalPage(pageResult.getTotalPage());
this.setTotal(pageResult.getTotal());
}
public PageResultDto(int page, int pageSize, int total) {
this.setPage(page);
this.setPageSize(pageSize);