diff --git a/hsweb-examples/hsweb-examples-custom-entity/README.md b/hsweb-examples/hsweb-examples-custom-entity/README.md new file mode 100644 index 000000000..10adc7459 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/README.md @@ -0,0 +1,212 @@ +# 拓展实体演示 +想拓展系统自带功能的实体?如添加字段. + +测试: 运行`org.hswebframework.web.example.custom.Application` + +提交数据: +```bash +curl -l -H "Content-type: application/json" \ +-X POST -d '{"name":"旧的属性","nameEn":"拓展的属性"}' \ +http://localhost:8081/organizational +``` +成功返回: +```json +{"result":"fd13ec65130d5ed66491a1e0453a3172","status":200,"timestamp":1497678000068} +``` + +获取数据: +```bash +curl http://localhost:8081/organizational/fd13ec65130d5ed66491a1e0453a3172 +``` +可以看到数据已经有新的字段 +# 实体类 +1. 编写实体类,继承需要拓展的实体,如: +```java +package org.hswebframework.web.example.custom.entity; + +import org.hswebframework.web.entity.organizational.SimpleOrganizationalEntity; + +public class CustomOrganizationalEntity extends SimpleOrganizationalEntity { + + /**********拓展字段**********/ + private String leader; + + private String nameEn; + + private String otherProperty; + + public String getLeader() { + return leader; + } + + public void setLeader(String leader) { + this.leader = leader; + } + + public String getNameEn() { + return nameEn; + } + + public void setNameEn(String nameEn) { + this.nameEn = nameEn; + } + + public String getOtherProperty() { + return otherProperty; + } + + public void setOtherProperty(String otherProperty) { + this.otherProperty = otherProperty; + } +} +``` + +2. 提供给hsweb +将新的实体类提供给hsweb有3种方式,第一种:jdk的serviceLoader;第二种:application.yml配置; +第三种:java类方式配置,选择其中一种即可. + +serviceLoader + +创建文件:`META-INF/services/org.hswebframework.web.entity.organizational.OrganizationalEntity` +内容: +```text +org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity +``` + +application.yml +```yaml +hsweb: + entity: + mappings: + - source-base-package: org.hswebframework.web.entity.organizational + target-base-package: org.hswebframework.web.example.custom.entity + mapping: + OrganizationalEntity: CustomOrganizationalEntity +``` + +java类 +```java + package org.hswebframework.web.example.custom.config; + + import org.hswebframework.web.commons.entity.factory.MapperEntityFactory; + import org.hswebframework.web.entity.organizational.OrganizationalEntity; + import org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity; + import org.hswebframework.web.starter.entity.EntityMappingCustomer; + import org.springframework.stereotype.Component; + + /** + * 自定义实体关系 + * + * @author zhouhao + * @since 3.0 + */ + @Component + public class CustomEntityMappingCustomer implements EntityMappingCustomer { + @Override + public void customize(MapperEntityFactory entityFactory) { + //OrganizationalEntity使用CustomOrganizationalEntity实现 + entityFactory.addMapping(OrganizationalEntity.class, + MapperEntityFactory.defaultMapper(CustomOrganizationalEntity.class)); + } + } + +``` + +3. 编写新的mybatis mapper配置 + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + delete from s_organization where u_id =#{id} + + + + + + + + + + + + + + + + +``` + +4. 覆盖mybatis mapper xml配置 +覆盖方式有2种: application.yml或者java类配置.选择其一即可. + +application.yml + +```yaml +mybatis: + mapper-location-excludes: classpath*:org/hswebframework/**/OrganizationalMapper.xml + mapper-locations: classpath*:custom/mappers/OrganizationalMapper.xml +``` + +java class +```java +package org.hswebframework.web.example.custom.config; + +import org.hswebframework.web.dao.mybatis.MybatisMapperCustomer; +import org.springframework.stereotype.Component; + +@Component +public class CustomMybatisMapperCustomer implements MybatisMapperCustomer { + @Override + public String[] getExcludes() { + return new String[]{ + "classpath*:org/hswebframework/**/OrganizationalMapper.xml" + }; + } + + @Override + public String[] getIncludes() { + return new String[]{ + "classpath*:custom/mappers/OrganizationalMapper.xml" + }; + } +} +``` diff --git a/hsweb-examples/hsweb-examples-custom-entity/pom.xml b/hsweb-examples/hsweb-examples-custom-entity/pom.xml new file mode 100644 index 000000000..9f1fca280 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/pom.xml @@ -0,0 +1,69 @@ + + + + hsweb-examples + org.hswebframework.web + 3.0-SNAPSHOT + + 4.0.0 + + hsweb-examples-custom-entity + + + + + src/main/resources + true + + + + + + org.codehaus.groovy + groovy-all + + + com.h2database + h2 + + + com.alibaba + druid + 1.0.26 + + + ch.qos.logback + logback-classic + + + org.slf4j + slf4j-api + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.springframework.boot + spring-boot-starter-logging + + + + org.hswebframework.web + hsweb-spring-boot-starter + ${project.version} + + + + + org.hswebframework.web + hsweb-system-organizational-starter + ${project.version} + + + + \ No newline at end of file diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/Application.java b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/Application.java new file mode 100644 index 000000000..c12764945 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/Application.java @@ -0,0 +1,18 @@ +package org.hswebframework.web.example.custom; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +/** + * @author zhouhao + */ +@SpringBootApplication +@Configuration +@EnableAspectJAutoProxy +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class); + } +} diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomEntityMappingCustomer.java b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomEntityMappingCustomer.java new file mode 100644 index 000000000..57f0d51dc --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomEntityMappingCustomer.java @@ -0,0 +1,22 @@ +package org.hswebframework.web.example.custom.config; + +import org.hswebframework.web.commons.entity.factory.MapperEntityFactory; +import org.hswebframework.web.entity.organizational.OrganizationalEntity; +import org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity; +import org.hswebframework.web.starter.entity.EntityMappingCustomer; +import org.springframework.stereotype.Component; + +/** + * 自定义实体关系 + * + * @author zhouhao + * @since 3.0 + */ +@Component +public class CustomEntityMappingCustomer implements EntityMappingCustomer { + @Override + public void customize(MapperEntityFactory entityFactory) { + entityFactory.addMapping(OrganizationalEntity.class, + MapperEntityFactory.defaultMapper(CustomOrganizationalEntity.class)); + } +} diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomMybatisMapperCustomer.java b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomMybatisMapperCustomer.java new file mode 100644 index 000000000..61fac94c3 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/config/CustomMybatisMapperCustomer.java @@ -0,0 +1,27 @@ +package org.hswebframework.web.example.custom.config; + +import org.hswebframework.web.dao.mybatis.MybatisMapperCustomer; +import org.springframework.stereotype.Component; + + +/** + * 自定义mybatis xml + * + * @author zhouhao + */ +@Component +public class CustomMybatisMapperCustomer implements MybatisMapperCustomer { + @Override + public String[] getExcludes() { + return new String[]{ + "classpath*:org/hswebframework/**/OrganizationalMapper.xml" + }; + } + + @Override + public String[] getIncludes() { + return new String[]{ + "classpath*:custom/mappers/OrganizationalMapper.xml" + }; + } +} diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/entity/CustomOrganizationalEntity.java b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/entity/CustomOrganizationalEntity.java new file mode 100644 index 000000000..b22837358 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/java/org/hswebframework/web/example/custom/entity/CustomOrganizationalEntity.java @@ -0,0 +1,40 @@ +package org.hswebframework.web.example.custom.entity; + +import org.hswebframework.web.entity.organizational.SimpleOrganizationalEntity; + +/** + * 自定义实体 + * + * @author zhouhao + */ +public class CustomOrganizationalEntity extends SimpleOrganizationalEntity { + private String leader; + + private String nameEn; + + private String otherProperty; + + public String getLeader() { + return leader; + } + + public void setLeader(String leader) { + this.leader = leader; + } + + public String getNameEn() { + return nameEn; + } + + public void setNameEn(String nameEn) { + this.nameEn = nameEn; + } + + public String getOtherProperty() { + return otherProperty; + } + + public void setOtherProperty(String otherProperty) { + this.otherProperty = otherProperty; + } +} diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/META-INF/services/org.hswebframework.web.entity.organizational.OrganizationalEntity b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/META-INF/services/org.hswebframework.web.entity.organizational.OrganizationalEntity new file mode 100644 index 000000000..8d4314c06 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/META-INF/services/org.hswebframework.web.entity.organizational.OrganizationalEntity @@ -0,0 +1 @@ +org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity \ No newline at end of file diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/application.yml b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/application.yml new file mode 100644 index 000000000..a12966988 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/application.yml @@ -0,0 +1,24 @@ +spring: + aop: + auto: true + proxy-target-class: true + datasource: + url : jdbc:h2:mem:example + username : sa + password : + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name : org.h2.Driver + cache: + type: simple +hsweb: + app: + name: hsweb示例 + version: 3.0.0 +# entity: +# mappings: +# - source-base-package: org.hswebframework.web.entity.organizational +# target-base-package: org.hswebframework.web.example.custom.entity +# mapping: +# OrganizationalEntity: CustomOrganizationalEntity +server: + port: 8081 \ No newline at end of file diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/custom/mappers/OrganizationalMapper.xml b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/custom/mappers/OrganizationalMapper.xml new file mode 100644 index 000000000..cbab9d699 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/custom/mappers/OrganizationalMapper.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + delete from s_organization where u_id =#{id} + + + + + + + + + + + + + + + + diff --git a/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/hsweb-starter.js b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/hsweb-starter.js new file mode 100644 index 000000000..ba3a97791 --- /dev/null +++ b/hsweb-examples/hsweb-examples-custom-entity/src/main/resources/hsweb-starter.js @@ -0,0 +1,59 @@ +/* + * 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. + * + */ +//组件信息 +var info = { + groupId: "${project.groupId}", + artifactId: "${project.artifactId}", + version: "${project.version}", + website: "https://github.com/hs-web/hsweb-framework/tree/master/hsweb-system/hsweb-system-organizational", + author: "admin@hsweb.me", + comment: "组织架构" +}; + +//版本更新信息 +var versions = [ + // { + // version: "3.0.0", + // upgrade: function (context) { + // java.lang.System.out.println("更新到3.0.2了"); + // } + // } +]; +var JDBCType = java.sql.JDBCType; +function install(context) { + var database = context.database; + database.createOrAlter("s_organization") + .addColumn().name("u_id").alias("id").comment("ID").jdbcType(java.sql.JDBCType.VARCHAR).length(32).primaryKey().commit() + .addColumn().name("name_en").alias("nameEn").comment("名称(英文)").jdbcType(java.sql.JDBCType.VARCHAR).length(32).commit() + .addColumn().name("leader").alias("leader").comment("机构负责人").jdbcType(java.sql.JDBCType.VARCHAR).length(256).commit() + .addColumn().name("other_property").alias("otherProperty").comment("其他属性").jdbcType(java.sql.JDBCType.VARCHAR).length(32).commit() + .comment("组织机构表").commit(); +} + +//设置依赖 +dependency.setup(info) + .onInstall(install) + .onUpgrade(function (context) { //更新时执行 + var upgrader = context.upgrader; + upgrader.filter(versions) + .upgrade(function (newVer) { + newVer.upgrade(context); + }); + }) + .onUninstall(function (context) { //卸载时执行 + + }); \ No newline at end of file