BaseServiceImpl增加beforeCreate用于自定义填充字段

This commit is contained in:
mazhicheng 2020-06-28 22:34:16 +08:00
parent 55f469b74a
commit 7f9299e6b0
4 changed files with 67 additions and 22 deletions

View File

@ -65,7 +65,15 @@ public class Cons {
/** /**
* 创建时间字段 * 创建时间字段
*/ */
createTime createTime,
/**
* 更新时间
*/
updateTime,
/**
* 创建人
*/
createBy
} }
} }

View File

@ -50,7 +50,7 @@ public class BaseController {
if(entityOrDto instanceof HttpServletRequest){ if(entityOrDto instanceof HttpServletRequest){
throw new Exception("参数错误buildQueryWrapper()参数为Entity/DTO对象"); throw new Exception("参数错误buildQueryWrapper()参数为Entity/DTO对象");
} }
return QueryBuilder.toQueryWrapper(entityOrDto, extractParams()); return QueryBuilder.toQueryWrapper(entityOrDto, extractQueryParams());
} }
/*** /***
@ -62,7 +62,7 @@ public class BaseController {
if(entityOrDto instanceof HttpServletRequest){ if(entityOrDto instanceof HttpServletRequest){
throw new Exception("参数错误buildQueryWrapper()参数为Entity/DTO对象"); throw new Exception("参数错误buildQueryWrapper()参数为Entity/DTO对象");
} }
return QueryBuilder.toLambdaQueryWrapper(entityOrDto, extractParams()); return QueryBuilder.toLambdaQueryWrapper(entityOrDto, extractQueryParams());
} }
/*** /***
@ -124,7 +124,7 @@ public class BaseController {
* 提取请求参数名集合 * 提取请求参数名集合
* @return * @return
*/ */
private Set<String> extractParams(){ protected Set<String> extractQueryParams(){
Map<String, Object> paramValueMap = convertParams2Map(); Map<String, Object> paramValueMap = convertParams2Map();
if(V.notEmpty(paramValueMap)){ if(V.notEmpty(paramValueMap)){
return paramValueMap.keySet(); return paramValueMap.keySet();
@ -136,7 +136,7 @@ public class BaseController {
* 将请求参数值转换为Map * 将请求参数值转换为Map
* @return * @return
*/ */
public Map<String, Object> convertParams2Map(){ protected Map<String, Object> convertParams2Map(){
Map<String, Object> result = new HashMap<>(8); Map<String, Object> result = new HashMap<>(8);
if(request == null){ if(request == null){
return result; return result;
@ -196,7 +196,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public Long getLong(String param){ protected Long getLong(String param){
return S.toLong(request.getParameter(param)); return S.toLong(request.getParameter(param));
} }
@ -206,7 +206,7 @@ public class BaseController {
* @param defaultValue * @param defaultValue
* @return * @return
*/ */
public long getLong(String param, Long defaultValue){ protected long getLong(String param, Long defaultValue){
return S.toLong(request.getParameter(param), defaultValue); return S.toLong(request.getParameter(param), defaultValue);
} }
@ -215,7 +215,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public Integer getInteger(String param){ protected Integer getInteger(String param){
return S.toInt(request.getParameter(param)); return S.toInt(request.getParameter(param));
} }
@ -225,7 +225,7 @@ public class BaseController {
* @param defaultValue * @param defaultValue
* @return * @return
*/ */
public int getInt(String param, Integer defaultValue){ protected int getInt(String param, Integer defaultValue){
return S.toInt(request.getParameter(param), defaultValue); return S.toInt(request.getParameter(param), defaultValue);
} }
@ -234,7 +234,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public boolean getBoolean(String param){ protected boolean getBoolean(String param){
return S.toBoolean(request.getParameter(param)); return S.toBoolean(request.getParameter(param));
} }
@ -244,7 +244,7 @@ public class BaseController {
* @param defaultBoolean * @param defaultBoolean
* @return * @return
*/ */
public boolean getBoolean(String param, boolean defaultBoolean){ protected boolean getBoolean(String param, boolean defaultBoolean){
return S.toBoolean(request.getParameter(param), defaultBoolean); return S.toBoolean(request.getParameter(param), defaultBoolean);
} }
@ -253,7 +253,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public Double getDouble(String param){ protected Double getDouble(String param){
if(V.notEmpty(request.getParameter(param))){ if(V.notEmpty(request.getParameter(param))){
return Double.parseDouble(request.getParameter(param)); return Double.parseDouble(request.getParameter(param));
} }
@ -266,7 +266,7 @@ public class BaseController {
* @param defaultValue * @param defaultValue
* @return * @return
*/ */
public Double getDouble(String param, Double defaultValue){ protected Double getDouble(String param, Double defaultValue){
if(V.notEmpty(request.getParameter(param))){ if(V.notEmpty(request.getParameter(param))){
return Double.parseDouble(request.getParameter(param)); return Double.parseDouble(request.getParameter(param));
} }
@ -278,7 +278,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public String getString(String param){ protected String getString(String param){
if(V.notEmpty(request.getParameter(param))){ if(V.notEmpty(request.getParameter(param))){
return request.getParameter(param); return request.getParameter(param);
} }
@ -291,7 +291,7 @@ public class BaseController {
* @param defaultValue * @param defaultValue
* @return * @return
*/ */
public String getString(String param, String defaultValue){ protected String getString(String param, String defaultValue){
if(V.notEmpty(request.getParameter(param))){ if(V.notEmpty(request.getParameter(param))){
return request.getParameter(param); return request.getParameter(param);
} }
@ -303,7 +303,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public String[] getStringArray(String param){ protected String[] getStringArray(String param){
if(request.getParameterValues(param) != null){ if(request.getParameterValues(param) != null){
return request.getParameterValues(param); return request.getParameterValues(param);
} }
@ -315,7 +315,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public List<String> getStringList(String param){ protected List<String> getStringList(String param){
String[] strArray = getStringArray(param); String[] strArray = getStringArray(param);
if(V.isEmpty(strArray)){ if(V.isEmpty(strArray)){
return null; return null;
@ -328,7 +328,7 @@ public class BaseController {
* @param param * @param param
* @return * @return
*/ */
public List<Long> getLongList(String param){ protected List<Long> getLongList(String param){
String[] strArray = getStringArray(param); String[] strArray = getStringArray(param);
if(V.isEmpty(strArray)){ if(V.isEmpty(strArray)){
return null; return null;

View File

@ -78,9 +78,21 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
warning("createEntity", "参数entity为null"); warning("createEntity", "参数entity为null");
return false; return false;
} }
return save(entity);
}
@Override
public boolean save(T entity) {
beforeCreateEntity(entity);
return super.save(entity); return super.save(entity);
} }
/**
* 用于创建之前的自动填充等场景调用
*/
protected void beforeCreateEntity(T entity){
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public <RE, R> boolean createEntityAndRelatedEntities(T entity, List<RE> relatedEntities, ISetter<RE, R> relatedEntitySetter) { public <RE, R> boolean createEntityAndRelatedEntities(T entity, List<RE> relatedEntities, ISetter<RE, R> relatedEntitySetter) {
@ -130,7 +142,26 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
} }
else{ else{
// 批量插入 // 批量插入
return super.saveBatch(entityList, BaseConfig.getBatchSize()); return saveBatch(entityList, BaseConfig.getBatchSize());
}
}
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize){
// 批量插入
beforeCreateEntities(entityList);
return super.saveBatch(entityList, batchSize);
}
/**
* 用于创建之前的自动填充等场景调用
*/
protected void beforeCreateEntities(Collection<T> entityList){
if(V.isEmpty(entityList)){
return;
}
for(T entity : entityList){
beforeCreateEntity(entity);
} }
} }
@ -649,7 +680,7 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
* @param message * @param message
*/ */
private void warning(String method, String message){ private void warning(String method, String message){
log.warn(this.getClass().getName() + ".{} 调用错误: {}, 请检查!", method, message); log.warn(this.getClass().getSimpleName() + ".{} 调用错误: {}, 请检查!", method, message);
} }
} }

View File

@ -161,8 +161,14 @@ public class BeanUtils {
* @return * @return
*/ */
public static Object getProperty(Object obj, String field){ public static Object getProperty(Object obj, String field){
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(obj); try {
return wrapper.getPropertyValue(field); BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(obj);
return wrapper.getPropertyValue(field);
}
catch (Exception e) {
log.warn("获取对象属性值出错返回null", e);
}
return null;
} }
/*** /***