BasicService
setDispFields
设置字段显示值。效果为把某个字段的code转化成name、把状态的-1/1转化成无效/有效等等。具体逻辑大致如下所示:
1 2 3 4 5 6 7 8 9 10 11
| public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) { String temp; if (mapRow.get(TmbMsgPushTaskEntity.FieldRegister) != null) { temp = ConvertUtil.convertToString(mapRow.get(TmbMsgPushTaskEntity.FieldRegister)); mapRow.put("disp" + TmbMsgPushTaskEntity.FieldRegister, this.getNameFromSysCode(temp)); } if (mapRow.get(TmbMsgPushTaskEntity.FieldPushsts) != null) { temp = ConvertUtil.convertToString(mapRow.get(TmbMsgPushTaskEntity.FieldPushsts)); mapRow.put("disp" + TmbMsgPushTaskEntity.FieldPushsts, datacodeCache.getValueDynamic("Pushsts", temp, CboItemDetailsEntity.FieldCode, CboItemDetailsEntity.FieldName)); } }
|
可以看到在此过程中,如果该字段的的值对应关系已经存储在了CboItemDetails
表中(还有CboItems表),那么直接调用datacodeCache.getValueDynamic
方法即可,反之需要自己重写字段转化具体实现方法(如getNameFromSysCode
)
update
有protected void updateBefore(map)
、protected void updateAfter(map)
和public int update(map)
三个方法。三个方法在同一事务中。
1 2 3 4 5 6 7 8
| @Override @Transactional(rollbackFor = Exception.class) public int update(Map<String, Object> map) { updateBefore(map); int i = dao.update(map); updateAfter(map); return i; }
|
可以重写这三个方法来自定义更新前、更新、更新后的具体操作,这三个方法的入参是同一个map。
只需要注意重新更新操作时,需要调用父类方法或者手动调用before方法和after方法,否则不会执行对应逻辑。
updateBatch
、updateBatchMap
方法也有更新前、更新后的处理方法;
updateEntity
是将实体类转化成map在调用update方法;
- 领域更新
updateField
和条件更新updateByWhere
则没有更新前、更新后的处理逻辑。
delete
有一个条件删除方法deleteByWhere
方法,与更新方法类似,也有删除前和删除后的处理逻辑
1 2 3 4 5 6 7 8 9
| @Override @Transactional(rollbackFor = Exception.class) public int deleteByWhere(String whereSql) { int resultCount = 0; deleteBefore(whereSql); resultCount = dao.deleteByWhere(whereSql); deleteAfter(whereSql); return resultCount; }
|
- deleteById:根据id删除
- deleteByIds:根据ids批量删除
insert
同update方法,有插入前和插入后的处理逻辑
1 2 3 4 5 6 7 8
| @Override @Transactional(rollbackFor = Exception.class) public T insert(BasicEntity entity) { beforeInsert(entity); entity = dao.insert(entity); afterInsert(entity); return (T) entity; }
|
批量插入insertBatch
方法类似。
需要注意的是,在底层TapMyBatisDao
的beforeInsert
的实现中,默认将enabled置为1,导致新增对象的enabled总为1.
query
有以下几个方法:
- queryById(String id):根据id查询
- queryByIds(String ids):根据ids批量查询
- queryByWhere(String whereSql):条件查询
- queryForPage(Map paraMap):分页查询
- queryObjectByWhere(String whereSql):条件查询单个对象(
list.get(0)
)
- queryMapById(String id):根据id查询,返回值由entity变为map
- queryMapByIds(String ids):根据ids批量查询,返回值由entity变为map
- queryMapByWhere(String whereSql):条件查询,返回值由entity变为map
- queryMapForPage(Map paraMap):分页查询,返回值由entity变为map
- queryMapFieldsByWhere(String whereSql, List filedList):条件领域查询
拓展方法
以上的CRUD方法还大都拥有其对应的拓展方法
- 根据xml中的selectName执行对应方法:
update(Map<String, Object> paraMap, String selectName)
- 直接执行sql语句的方法:
updateExecute(String updateSql)
TapMyBatisDao
insert
平台定义了唯一性校验功能,配置后,在插入操作前,会进行唯一性校验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Override protected void beforeInsert(BasicEntity entity) { try { super.beforeInsert(entity); TapEntity tapEntity = (TapEntity) entity; tapEntity.setEnabled(1); if (this.getSessionUserBean() != null) { tapEntity.setCreateuser(this.getSessionUserBean().getUser().getId()); tapEntity.setModifieduser(this.getSessionUserBean().getUser().getId()); tapEntity.setCreateorgid(this.getSessionUserBean().getOrg().getId()); } ICboUniqueControlDao cboUniqueControlDao = (ICboUniqueControlDao) SpringUtil.getBean("cboUniqueControlDao"); String tableName = ((TapEntity) this.getEntityClass().newInstance()).getTableName(); List<CboUniqueControlEntity> listUniqueControl = cboUniqueControlDao.queryByWhere(" controltable = '" + tableName + "' "); this.checkFieldUnique(entity.convertToMap(), listUniqueControl); addTableLog(CboTableLogOperateTypeEnum.Insert, entity.convertToMap()); } catch (Exception ex) { throw new DaoException(ex.getMessage()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| private void checkFieldUnique(Map<String, Object> map, List<CboUniqueControlEntity> listUniqueControl) { if (listUniqueControl == null || listUniqueControl.size() <= 0) { return; } for (CboUniqueControlEntity uniqueControl : listUniqueControl) { String[] arraySplitfieldName = uniqueControl.getControlfield().split(";"); boolean isNotExists = false; for (String splitfieldName : arraySplitfieldName) { if (!map.keySet().contains(splitfieldName)) { isNotExists = true; break; } } if (isNotExists) { continue; } StringBufferProxy sql = new StringBufferProxy(); sql.appendSingle(" 1=1 "); for (String splitfieldName : arraySplitfieldName) { sql.appendSingle(" and {0}='{1}' ", splitfieldName, ConvertUtil.convertToString(map.get(splitfieldName))); } sql.appendSingle(" and id != '{0}' ", ConvertUtil.convertToString(map.get("id"))); if (!StringUtil.isEmpty(uniqueControl.getCondition())) { sql.appendSingle(uniqueControl.getCondition()); } int count = this.getCount(sql.toString()); if (count > 0) { throw new DaoException(uniqueControl.getTipmsg()); } } }
|
在这里进行配置,可以指定数据库和字段名
