在大型业务系统中, 常常需要从数据库中查询某个实体对象。 在进行处理之前, 必须先检测该实体是否存在,以增强系统的健壮性。 不过, 检测代码充斥在主业务流程中又会大大降低业务逻辑的清晰性, 最好集中起来进行管理。 因此,编写了一个类来做这个事情, 只需要一行调用就可以完成相应实体对象的检测。
十余年的鄂伦春网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整鄂伦春建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“鄂伦春网站设计”,“鄂伦春网站推广”以来,每个客户项目都认真落实执行。
美中不足的一点是, 打 log 的位置发生变化了。由于打 log 的主要目标是为了便于调试和定位错误位置, 为了补偿, 一种办法是调用方法时根据具体语境传入自定义的错误消息, 根据错误消息可以定位错误位置; 另一种办法是, 如果不希望每次调用都使用不同的自定义错误消息, 使用 log.error("error", bizEx) 打印异常栈信息, 也可以获取到抛出错误的位置。
NOTE: 为了遵循公司代码的保密性, 隐去导入的包名, 各位读者可以借鉴其思想, 运用到自己的项目中去。
使用方法:
import static util.CheckEntityExistManager.checkEntity;import util.CheckEntityExistManager.EntityChecker;public class EntityExistCheckTester { private static final Log log = LogFactory.getLog(EntityExistCheckTester.class); public static void main(String[] args) throws BizException { demo(); demo2(); } public static ResultSet demo() { try { // Not Need type casting DeviceDO device = checkEntity(EntityChecker.DISK_ID_CHECK, "d-xxx"); System.out.println(device); VmDO vm = checkEntity(EntityChecker.VM_NAME_CHECK, "vm-name","user-defined msg: vm not exist when creating"); System.out.println(vm); return new ResultSet(IErrorCode.SUCCESSFUL); } catch (BizException bizEx) { log.error("error", bizEx); return new ResultSet(bizEx.getErrorCode()); } catch (Exception ex) { log.error(ex); return new ResultSet(IErrorCode.FAILED); } } public static ResultSet demo2() { try { ZoneDO zone = checkEntity(EntityChecker.ZONE_ID_CHECK, "z-yy"); System.out.println(zone); return new ResultSet(IErrorCode.SUCCESSFUL); } catch (BizException bizEx) { log.error("error", bizEx); return new ResultSet(bizEx.getErrorCode()); } catch (Exception ex) { log.error(ex); return new ResultSet(IErrorCode.FAILED); } } }
实体存在性集中检测类: CheckEntityExistManager:
public class CheckEntityExistManager { private static final Log log = LogFactory.getLog(CheckEntityExistManager.class); private static CheckEntityExistManager checkEntityMgr = getInstance(); private CheckEntityExistManager() { } private static CheckEntityExistManager getInstance() { if (checkEntityMgr == null) { checkEntityMgr = new CheckEntityExistManager(); } return checkEntityMgr; } interface ICheckEntityExist{ T checkEntityExist(String entityValue, String msg) throws BizException; } private static ICheckEntityExist diskIdChecker = checkEntityMgr.new CheckDiskIdExist(); private static ICheckEntityExist vmNameChecker = checkEntityMgr.new CheckVmExist(); private static ICheckEntityExist zoneChecker = checkEntityMgr.new CheckZoneExist(); public enum EntityChecker { DISK_ID_CHECK(diskIdChecker, DeviceErrorCode.DISK_NOT_EXIST), VM_NAME_CHECK(vmNameChecker, VmErrorCode.ERROR_VM_NOT_EXIST), ZONE_ID_CHECK(zoneChecker, ZoneErrorCode.ZONE_NOT_EXIST); EntityChecker(ICheckEntityExist> entityChecker, IErrorCode errorCode) { this.entityChecker = entityChecker; this.errorCode = errorCode; } private static Map errorMap = new HashMap (); static { for (EntityChecker entityChecker: EntityChecker.values()) { ICheckEntityExist> checker = entityChecker.getEntityChecker(); errorMap.put(checker.getClass().getSimpleName(), entityChecker.getErrorCode()); } } private static IErrorCode getErrorCode(String checkClassSimpleName) { return errorMap.get(checkClassSimpleName); } private ICheckEntityExist> entityChecker; private IErrorCode errorCode; public ICheckEntityExist> getEntityChecker() { return entityChecker; } public void setEntityChecker(ICheckEntityExist> entityChecker) { this.entityChecker = entityChecker; } public IErrorCode getErrorCode() { return errorCode; } public void setErrorCode(IErrorCode errorCode) { this.errorCode = errorCode; } } @Autowired private DeviceService deviceService; @Autowired private VmService vmService; @Autowired private ZoneService zoneService; class CheckDiskIdExist implements ICheckEntityExist { @Override public DeviceDO checkEntityExist(String diskId, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckDiskIdExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //DeviceDO dev = deviceService.getDevice(diskId); DeviceDO dev = new DeviceDO(); return CheckEntityExistManager.throwexOrReturn(dev, message, errorCode); } } class CheckVmExist implements ICheckEntityExist { @Override public VmDO checkEntityExist(String vmName, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckVmExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //VmDO vm = vmService.queryVmByName(vmName); VmDO vm = null; return CheckEntityExistManager.throwexOrReturn(vm, message, errorCode); } } class CheckZoneExist implements ICheckEntityExist { @Override public ZoneDO checkEntityExist(String zoneId, String msg) throws BizException { IErrorCode errorCode = EntityChecker.getErrorCode(CheckZoneExist.class.getSimpleName()); String message = (msg == null ? errorCode.getMessage() : msg); //ZoneDO zone = zoneService.queryZoneById(zoneId); ZoneDO zone = null; return CheckEntityExistManager.throwexOrReturn(zone, message, errorCode); } } public static T throwexOrReturn(T entity, String message, IErrorCode errorCode) throws BizException { if (entity == null) { log.error(message); throw new BizException(errorCode); } return entity; } public static T checkEntity(EntityChecker entityChecker, String entityValue, String customMsg) throws BizException { ICheckEntityExist checker = (ICheckEntityExist ) entityChecker.getEntityChecker(); return checker.checkEntityExist(entityValue, customMsg); } public static T checkEntity(EntityChecker entityChecker, String entityValue) throws BizException { ICheckEntityExist checker = (ICheckEntityExist ) entityChecker.getEntityChecker(); return checker.checkEntityExist(entityValue, null); } }
其他精彩文章文章
更多关于android开发文章