新增自定义实体演示

This commit is contained in:
zhouhao
2017-06-17 13:51:18 +08:00
parent a9ceb594b5
commit 2941bfcb58
10 changed files with 546 additions and 0 deletions

View File

@@ -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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hswebframework.web.dao.organizational.OrganizationalDao">
<resultMap id="OrganizationalResultMap" type="org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity">
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
<result property="fullName" column="full_name" javaType="String" jdbcType="VARCHAR"/>
<result property="code" column="code" javaType="String" jdbcType="VARCHAR"/>
<result property="optionalRoles" column="optional_roles" javaType="java.util.List" jdbcType="CLOB"/>
<result property="parentId" column="parent_id" javaType="String" jdbcType="VARCHAR"/>
<result property="path" column="path" javaType="String" jdbcType="VARCHAR"/>
<result property="sortIndex" column="sort_index" javaType="Long" jdbcType="DECIMAL"/>
<result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
<result property="level" column="level" javaType="Integer" jdbcType="DECIMAL"/>
<!--拓展的属性-->
<result property="nameEn" column="name_en" javaType="String" jdbcType="VARCHAR"/>
<result property="leader" column="leader" javaType="String" jdbcType="VARCHAR"/>
<result property="otherProperty" column="other_property" javaType="String" jdbcType="VARCHAR"/>
</resultMap>
<!--用于动态生成sql所需的配置-->
<sql id="config">
<bind name="resultMapId" value="'OrganizationalResultMap'"/>
<bind name="tableName" value="'s_organization'"/>
</sql>
<insert id="insert" parameterType="org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity">
<include refid="config"/>
<include refid="BasicMapper.buildInsertSql"/>
</insert>
<delete id="deleteByPk" parameterType="String">
delete from s_organization where u_id =#{id}
</delete>
<delete id="delete" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildDeleteSql"/>
</delete>
<update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildUpdateSql"/>
</update>
<select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="OrganizationalResultMap">
<include refid="config"/>
<include refid="BasicMapper.buildSelectSql"/>
</select>
<select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
<include refid="config"/>
<include refid="BasicMapper.buildTotalSql"/>
</select>
</mapper>
```
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"
};
}
}
```

View File

@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hsweb-examples</artifactId>
<groupId>org.hswebframework.web</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hsweb-examples-custom-entity</artifactId>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<!--组织架构-->
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-system-organizational-starter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -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);
}
}

View File

@@ -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));
}
}

View File

@@ -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"
};
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1 @@
org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity

View File

@@ -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

View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ 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.
~
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hswebframework.web.dao.organizational.OrganizationalDao">
<resultMap id="OrganizationalResultMap" type="org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity">
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
<result property="fullName" column="full_name" javaType="String" jdbcType="VARCHAR"/>
<result property="code" column="code" javaType="String" jdbcType="VARCHAR"/>
<result property="optionalRoles" column="optional_roles" javaType="java.util.List" jdbcType="CLOB"/>
<result property="parentId" column="parent_id" javaType="String" jdbcType="VARCHAR"/>
<result property="path" column="path" javaType="String" jdbcType="VARCHAR"/>
<result property="sortIndex" column="sort_index" javaType="Long" jdbcType="DECIMAL"/>
<result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
<result property="level" column="level" javaType="Integer" jdbcType="DECIMAL"/>
<!--拓展的属性-->
<result property="nameEn" column="name_en" javaType="String" jdbcType="VARCHAR"/>
<result property="leader" column="leader" javaType="String" jdbcType="VARCHAR"/>
<result property="otherProperty" column="other_property" javaType="String" jdbcType="VARCHAR"/>
</resultMap>
<!--用于动态生成sql所需的配置-->
<sql id="config">
<bind name="resultMapId" value="'OrganizationalResultMap'"/>
<bind name="tableName" value="'s_organization'"/>
</sql>
<insert id="insert" parameterType="org.hswebframework.web.example.custom.entity.CustomOrganizationalEntity">
<include refid="config"/>
<include refid="BasicMapper.buildInsertSql"/>
</insert>
<delete id="deleteByPk" parameterType="String">
delete from s_organization where u_id =#{id}
</delete>
<delete id="delete" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildDeleteSql"/>
</delete>
<update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildUpdateSql"/>
</update>
<select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="OrganizationalResultMap">
<include refid="config"/>
<include refid="BasicMapper.buildSelectSql"/>
</select>
<select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
<include refid="config"/>
<include refid="BasicMapper.buildTotalSql"/>
</select>
</mapper>

View File

@@ -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) { //卸载时执行
});