diff --git a/src/main/java/mpms/service/h2db/BaseDbCommonService.java b/src/main/java/mpms/service/h2db/BaseDbCommonService.java index 4afcbd6c9de64624db35d20d297e9bf41a1307ba..7aaf0d1b2006f1ef4fac1ef0ce5b776a5848ffc6 100644 --- a/src/main/java/mpms/service/h2db/BaseDbCommonService.java +++ b/src/main/java/mpms/service/h2db/BaseDbCommonService.java @@ -181,8 +181,50 @@ public abstract class BaseDbCommonService { } } + /** + * 根据主键查询实体 + * + * @param keyValue 主键值 + * @return 数据 + */ + public T getByKey(String keyValue) { + if (StrUtil.isEmpty(keyValue)) { + return null; + } + if (!DbConfig.getInstance().isInit()) { + // ignore + return null; + } + Entity where = new Entity(tableName); + where.set(key, keyValue); + Db db = Db.use(); + db.setWrapper((Character) null); + Entity entity; + try { + entity = db.get(where); + } catch (SQLException e) { + throw new LinuxRuntimeException("数据库异常", e); + } + return this.entityToBean(entity, this.tClass); + } - + /** + * entity 转 实体 + * + * @param entity Entity + * @param rClass 实体类 + * @param 乏型 + * @return data + */ + private R entityToBean(Entity entity, Class rClass) { + if (entity == null) { + return null; + } + CopyOptions copyOptions = new CopyOptions(); + copyOptions.setIgnoreError(true); + copyOptions.setIgnoreCase(true); + return BeanUtil.toBean(entity, rClass, copyOptions); + } }