优化:基于阿里巴巴 Java 开发手册(黄山版)优化方法排序及访问权限修饰符

1.编程规约>OOP规约>第20条:
【推荐】当一个类有多个构造方法,或者多个同名方法,这些方法应该按顺序放置在一起,便于阅读,
此条规则优先于下一条。
2.编程规约>OOP规约>第21条:
【推荐】类内方法定义的顺序依次是:公有方法或保护方法 > 私有方法 > getter / setter 方法。
说明:公有方法是类的调用者和维护者最关心的方法,首屏展示最好;保护方法虽然只是子类关心,也可能是“模板设
计模式”下的核心方法;而私有方法外部一般不需要特别关心,是一个黑盒实现;因为承载的信息价值较低,所有
Service 和 DAO 的 getter / setter 方法放在类体最后。
3.编程规约>OOP规约>第26条:
【推荐】类成员与方法访问控制从严:
  1)如果不允许外部直接通过 new 来创建对象,那么构造方法必须是 private。
  2)工具类不允许有 public 或 default 构造方法。
  3)类非 static 成员变量并且与子类共享,必须是 protected。
  4)类非 static 成员变量并且仅在本类使用,必须是 private。
  5)类 static 成员变量如果仅在本类使用,必须是 private。
  6)若是 static 成员变量,考虑是否为 final。
  7)类成员方法只供类内部调用,必须是 private。
  8)类成员方法只对继承类公开,那么限制为 protected。
  说明:任何类、方法、参数、变量,严控访问范围。过于宽泛的访问范围,不利于模块解耦。思考:如果是一个
  private 的方法,想删除就删除,可是一个 public 的 service 成员方法或成员变量,删除一下,不得手心冒点汗吗?
  变量像自己的小孩,尽量在自己的视线内,变量作用域太大,无限制的到处跑,那么你会担心的。
This commit is contained in:
2023-03-04 10:41:43 +08:00
parent 6da85463c9
commit 4779d77265
18 changed files with 174 additions and 201 deletions

View File

@@ -87,9 +87,18 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
@Autowired
protected M baseMapper;
protected Class<T> entityClass = currentEntityClass();
protected Class<V> voClass = currentVoClass();
protected Class<D> detailVoClass = currentDetailVoClass();
private final Class<T> entityClass;
private final Class<V> voClass;
private final Class<D> detailVoClass;
private final String entityIdName;
public BaseServiceImpl() {
this.entityClass = (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);
this.voClass = (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2);
this.detailVoClass =
(Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3);
this.entityIdName = this.currentEntityIdName();
}
@Override
public PageDataVO<V> page(Q query, PageQuery pageQuery) {
@@ -141,51 +150,6 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
return list;
}
@Override
public D get(Long id) {
T entity = this.getById(id);
D detailVO = BeanUtil.copyProperties(entity, detailVoClass);
this.fillDetail(detailVO);
return detailVO;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long add(C request) {
if (request == null) {
return 0L;
}
// 保存信息
T entity = BeanUtil.copyProperties(request, entityClass);
baseMapper.insert(entity);
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Object idValue = tableInfo.getPropertyValue(entity, this.currentEntityIdName());
return Convert.toLong(idValue);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(C request) {
String idName = this.currentEntityIdName();
Object idValue = ReflectUtil.getFieldValue(request, idName);
T entity = this.getById(idValue);
BeanUtil.copyProperties(request, entity, CopyOptions.create().ignoreNullValue());
baseMapper.updateById(entity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(List<Long> ids) {
baseMapper.deleteBatchIds(ids);
}
@Override
public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
List<D> list = this.list(query, sortQuery, detailVoClass);
list.forEach(this::fillDetail);
ExcelUtils.export(list, "导出数据", detailVoClass, response);
}
/**
* 查询列表
*
@@ -208,6 +172,50 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
return BeanUtil.copyToList(entityList, targetClass);
}
@Override
public D get(Long id) {
T entity = this.getById(id);
D detailVO = BeanUtil.copyProperties(entity, detailVoClass);
this.fillDetail(detailVO);
return detailVO;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long add(C request) {
if (request == null) {
return 0L;
}
// 保存信息
T entity = BeanUtil.copyProperties(request, entityClass);
baseMapper.insert(entity);
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Object idValue = tableInfo.getPropertyValue(entity, entityIdName);
return Convert.toLong(idValue);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(C request) {
Object idValue = ReflectUtil.getFieldValue(request, entityIdName);
T entity = this.getById(idValue);
BeanUtil.copyProperties(request, entity, CopyOptions.create().ignoreNullValue());
baseMapper.updateById(entity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(List<Long> ids) {
baseMapper.deleteBatchIds(ids);
}
@Override
public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
List<D> list = this.list(query, sortQuery, detailVoClass);
list.forEach(this::fillDetail);
ExcelUtils.export(list, "导出数据", detailVoClass, response);
}
/**
* 根据 ID 查询
*
@@ -259,46 +267,6 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
}
}
/**
* 获取实体类 ID 名称
*
* @return 实体类 ID 名称
*/
protected String currentEntityIdName() {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
return keyProperty;
}
/**
* 获取实体类 Class 对象
*
* @return 实体类 Class 对象
*/
protected Class<T> currentEntityClass() {
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);
}
/**
* 获取列表信息类 Class 对象
*
* @return 列表信息类 Class 对象
*/
protected Class<V> currentVoClass() {
return (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2);
}
/**
* 获取详情信息类 Class 对象
*
* @return 详情信息类 Class 对象
*/
protected Class<D> currentDetailVoClass() {
return (Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3);
}
/**
* 链式查询
*
@@ -345,4 +313,17 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
protected LambdaUpdateChainWrapper<T> lambdaUpdate() {
return ChainWrappers.lambdaUpdateChain(baseMapper);
}
/**
* 获取实体类 ID 名称
*
* @return 实体类 ID 名称
*/
private String currentEntityIdName() {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
return keyProperty;
}
}