# 拓展实体演示
想拓展系统自带功能的实体?如添加字段.
测试: 运行`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"
};
}
}
```
5. 使用jpa注解方式拓展字段
依赖jpa-api:
```xml
org.hibernate.javax.persistence
hibernate-jpa-2.0-api
1.0.1.Final
```
在拓展的实体类中使用jpa注解:
```java
@Data
@Table //此处设置表名是无效的,仅作为一个解析标识
public class CustomUserEntity extends SimpleBindRoleUserEntity {
@Column(name = "nick_name")
private String nickName;
}
```
注意: 暂时只支持简单的属性。不支持表关联