Forráskód Böngészése

增加部分说明

zhouhao 8 éve
szülő
commit
43d9f9cedb

+ 9 - 0
hsweb-commons/README.md

@@ -0,0 +1,9 @@
+# 通用功能模块
+实现通用CRUD功能,增删改查,直接继承之.
+# 目录介绍
+1. [hsweb-commons-controller](hsweb-commons-controller):通用springmvc控制器
+1. [hsweb-commons-dao](hsweb-commons-dao):通用dao实现
+1. [hsweb-commons-entity](hsweb-commons-entity):通用实体类
+1. [hsweb-commons-service](hsweb-commons-service):通用服务类
+1. [hsweb-commons-utils](hsweb-commons-utils):工具类
+

+ 49 - 0
hsweb-commons/hsweb-commons-entity/README.md

@@ -0,0 +1,49 @@
+# 通用实体类模块
+集成系统通用的实体类,如 树形结构实体,排序实体,创建信息实体
+
+# 常用实体类
+
+| 类名       | 说明          | 
+| ------------- |:-------------:| 
+| [`Entity`](src/main/java/org/hswebframework/web/commons/entity/Entity.java)    | 实体类的总接口,用来标识为一个实体类 | 
+| [`GenericEntity`](src/main/java/org/hswebframework/web/commons/entity/GenericEntity.java)    | 提供基本属性的实体类 | 
+| [`RecordCreationEntity`](src/main/java/org/hswebframework/web/commons/entity/RecordCreationEntity.java)    | 可记录创建信息的实体类 | 
+| [`TreeSortSupportEntity`](src/main/java/org/hswebframework/web/commons/entity/TreeSortSupportEntity.java)    | 可排序树形结构实体类 | 
+
+# 实体类工厂
+作用: 为了增加拓展性,各个地方依赖的实体均为接口,实体实例应该调用[EntityFactory](src/main/java/org/hswebframework/web/commons/entity/factory/EntityFactory.java)
+进行实例化。如: `UserEntity user=entityFactory.newInstance(UserEntity.class);`
+
+目标: controller,service 不再依赖具体实体实现类。实现类由 dao和springMvc进行提供
+
+默认工厂实现: [MapperEntityFactory](src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java)
+该工厂可注册接口和实现类的映射关系,以及提供默认的实现类创建。
+默认的实现类创建逻辑为。`Class.forName("Simple"+interfaceName);`
+如:`UserEntity user=entityFactory.newInstance(UserEntity.class)` 
+如果未注册`UserEntity`对应的实现类,则将尝试创建`UserEntity`同包下的`SimpleUserEntity`类实例
+
+注册接口和实现类映射关系:
+
+方式1: 调用 mapperEntityFactory进行注册
+
+```java
+    @javax.annotation.Resource
+    private MapperEntityFactory mapperEntityFactory;
+
+    @javax.annotation.PostConstruct
+    public void init(){
+        mapperEntityFactory.addMapping(UserEntity.class,new Mapper(CustomUserEntity.class,CustomUserEntity::new));
+    }
+
+```
+
+方式2: application.yml 配置文件描述
+
+```yaml
+entity:
+      mappings:
+          -  source-base-package: org.hswebframework.web.entity.authorization
+             target-base-package: com.company.authorization
+             mapping:
+                UserEntity: CustomUserEntity
+```

+ 3 - 1
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/RecordCreationEntity.java

@@ -1,9 +1,11 @@
 package org.hswebframework.web.commons.entity;
 
 /**
- * TODO 完成注释
+ * 记录创建信息的实体类,包括创建人和创建时间。
+ * 此实体类与行级权限控制相关联:只能操作自己创建的数据
  *
  * @author zhouhao
+ * @since 3.0
  */
 public interface RecordCreationEntity extends Entity {
     String getCreatorId();

+ 0 - 38
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/SharedEntity.java

@@ -1,38 +0,0 @@
-/*
- *
- *  * Copyright 2016 http://www.hswebframework.org
- *  *
- *  * Licensed under the Apache License, Version 2.0 (the "License");
- *  * you may not use this file except in compliance with the License.
- *  * You may obtain a copy of the License at
- *  *
- *  *     http://www.apache.org/licenses/LICENSE-2.0
- *  *
- *  * Unless required by applicable law or agreed to in writing, software
- *  * distributed under the License is distributed on an "AS IS" BASIS,
- *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  * See the License for the specific language governing permissions and
- *  * limitations under the License.
- *
- */
-
-package org.hswebframework.web.commons.entity;
-
-/**
- * TODO 完成注释
- *
- * @author zhouhao
- */
-public interface SharedEntity extends Entity {
-
-    String creatorId = "creatorId";
-    String shareCode = "shareCode";
-
-    String getCreatorId();
-
-    SharedEntity setCreatorId(String creatorId);
-
-    String getShareCode();
-
-    SharedEntity setShareCode(String shareCode);
-}

+ 0 - 57
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/SimpleSharedEntity.java

@@ -1,57 +0,0 @@
-/*
- *
- *  * Copyright 2016 http://www.hswebframework.org
- *  *
- *  * Licensed under the Apache License, Version 2.0 (the "License");
- *  * you may not use this file except in compliance with the License.
- *  * You may obtain a copy of the License at
- *  *
- *  *     http://www.apache.org/licenses/LICENSE-2.0
- *  *
- *  * Unless required by applicable law or agreed to in writing, software
- *  * distributed under the License is distributed on an "AS IS" BASIS,
- *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  * See the License for the specific language governing permissions and
- *  * limitations under the License.
- *
- */
-
-package org.hswebframework.web.commons.entity;
-
-
-import org.hibernate.validator.constraints.NotBlank;
-
-/**
- * TODO 完成注释
- *
- * @author zhouhao
- */
-public abstract class SimpleSharedEntity<PK> extends SimpleGenericEntity<PK> implements SharedEntity {
-    @NotBlank
-    private String creatorId;
-
-    @NotBlank
-    private String shareCode;
-
-    @Override
-    public String getCreatorId() {
-        return creatorId;
-    }
-
-    @Override
-    public SimpleSharedEntity<PK> setCreatorId(String creatorId) {
-        this.creatorId = creatorId;
-        return this;
-    }
-
-    @Override
-    public String getShareCode() {
-        return shareCode;
-    }
-
-    @Override
-    public SimpleSharedEntity<PK> setShareCode(String shareCode) {
-        this.shareCode = shareCode;
-        return this;
-    }
-}

+ 14 - 1
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java

@@ -35,6 +35,10 @@ import java.util.function.Supplier;
 public class MapperEntityFactory implements EntityFactory {
     private Map<Class, Mapper> realTypeMapper = new HashMap<>();
 
+    public <T extends Entity> MapperEntityFactory(Map<Class<T>, Mapper> realTypeMapper) {
+        this.realTypeMapper.putAll(realTypeMapper);
+    }
+
     public <T extends Entity> MapperEntityFactory addMapping(Class<T> target, Mapper<T> mapper) {
         realTypeMapper.put(target, mapper);
         return this;
@@ -43,6 +47,7 @@ public class MapperEntityFactory implements EntityFactory {
     @Override
     @SuppressWarnings("unchecked")
     public <T extends Entity> T newInstance(Class<T> beanClass) {
+        if (beanClass == null) return null;
         Mapper<T> mapper = realTypeMapper.get(beanClass);
         if (mapper != null) return mapper.getInstanceGetter().get();
         synchronized (beanClass) {
@@ -96,7 +101,15 @@ public class MapperEntityFactory implements EntityFactory {
         }
     }
 
-    class DefaultInstanceGetter<T extends Entity> implements Supplier<T> {
+    public static <T extends Entity> Mapper<T> defaultMapper(Class<T> target) {
+        return new Mapper<>(target, defaultInstanceGetter(target));
+    }
+
+    public static <T extends Entity> Supplier<T> defaultInstanceGetter(Class<T> clazz) {
+        return new DefaultInstanceGetter<>(clazz);
+    }
+
+    static class DefaultInstanceGetter<T extends Entity> implements Supplier<T> {
         Class<T> type;
 
         public DefaultInstanceGetter(Class<T> type) {