拓展实体类

说明

hsweb提供的业务功能只提供了基础的,必要的属性。在某些场景下,可能需要拓展某个实体的字段,比如要给组织架构功能添加更多的属性信息。

添加字段

编写实体类,继承需要拓展的实体。
com.myproject.entity.CustomOrganizationalEntity
1
package com.myproject.entity;
2
3
import org.hswebframework.web.entity.organizational.SimpleOrganizationalEntity;
4
@Table //加上jpa注解,无需再修改 mybatis mapper.xml
5
public class CustomOrganizationalEntity extends SimpleOrganizationalEntity {
6
7
/**********新增加的字段**********/
8
9
@Column(name="leader")
10
private String leader;
11
12
@Column(name="name_en")
13
private String nameEn;
14
15
@Column(name="other_property")
16
private String otherProperty;
17
18
public String getLeader() {
19
return leader;
20
}
21
22
public void setLeader(String leader) {
23
this.leader = leader;
24
}
25
26
public String getNameEn() {
27
return nameEn;
28
}
29
30
public void setNameEn(String nameEn) {
31
this.nameEn = nameEn;
32
}
33
34
public String getOtherProperty() {
35
return otherProperty;
36
}
37
38
public void setOtherProperty(String otherProperty) {
39
this.otherProperty = otherProperty;
40
}
41
}
Copied!

覆盖默认实体

在创建了新的实体类后,需要告诉hsweb,不再使用默认的实体类。
目前提供了3种覆盖方式(Java自带ServiceLoader,application.yml配置,Java类配置),任选其一即可

ServiceLoader方式

resources下创建文件:
META-INF/services/org.hswebframework.web.entity.organizational.OrganizationalEntity
内容为:com.myproject.entity.CustomOrganizationalEntity

application.yml方式

application.yml
1
hsweb:
2
entity:
3
mappings:
4
- source-base-package: org.hswebframework.web.entity.organizational
5
target-base-package: com.myproject.entity
6
mapping:
7
OrganizationalEntity: CustomOrganizationalEntity
Copied!

Java类配置方式

CustomEntityMappingCustomizer
1
@Component
2
public class CustomEntityMappingCustomizer implements EntityMappingCustomizer {
3
@Override
4
public void customize(MapperEntityFactory entityFactory) {
5
//OrganizationalEntity使用CustomOrganizationalEntity实现
6
entityFactory.addMapping(OrganizationalEntity.class,
7
MapperEntityFactory.defaultMapper(CustomOrganizationalEntity.class));
8
}
9
}
Copied!

修改Mybatis配置

此步骤仅在未使用JPA注解或者需要修改mapper.xml时进行

使用新的mapper.xml

创建新的mapper.xml
com/myproject/mappers/OrganizationalMapper.xml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE mapper
3
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
5
<mapper namespace="org.hswebframework.web.dao.organizational.OrganizationalDao">
6
<resultMap id="OrganizationalResultMap" type="org.hswebframework.web.entity.organizational.OrganizationalEntity">
7
<!--默认的属性-->
8
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
9
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
10
<result property="fullName" column="full_name" javaType="String" jdbcType="VARCHAR"/>
11
<result property="code" column="code" javaType="String" jdbcType="VARCHAR"/>
12
<result property="optionalRoles" column="optional_roles" javaType="java.util.List" jdbcType="CLOB"/>
13
<result property="parentId" column="parent_id" javaType="String" jdbcType="VARCHAR"/>
14
<result property="path" column="path" javaType="String" jdbcType="VARCHAR"/>
15
<result property="sortIndex" column="sort_index" javaType="Long" jdbcType="DECIMAL"/>
16
<result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
17
<result property="level" column="level" javaType="Integer" jdbcType="DECIMAL"/>
18
<!--重点: 拓展的属性-->
19
<result property="nameEn" column="name_en" javaType="String" jdbcType="VARCHAR"/>
20
<result property="leader" column="leader" javaType="String" jdbcType="VARCHAR"/>
21
<result property="otherProperty" column="other_property" javaType="String" jdbcType="VARCHAR"/>
22
23
</resultMap>
24
25
<!--用于动态生成sql所需的配置-->
26
<sql id="config">
27
<bind name="resultMapId" value="'OrganizationalResultMap'"/>
28
<bind name="tableName" value="'s_organization'"/>
29
</sql>
30
<!--修改parameterType为新的实体类型-->
31
<insert id="insert" parameterType="com.myproject.entity.CustomOrganizationalEntity">
32
<include refid="config"/>
33
<include refid="BasicMapper.buildInsertSql"/>
34
</insert>
35
36
<delete id="deleteByPk" parameterType="String">
37
delete from s_organization where u_id =#{id}
38
</delete>
39
40
<delete id="delete" parameterType="org.hswebframework.web.commons.entity.Entity">
41
<include refid="config"/>
42
<include refid="BasicMapper.buildDeleteSql"/>
43
</delete>
44
45
<update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
46
<include refid="config"/>
47
<include refid="BasicMapper.buildUpdateSql"/>
48
</update>
49
50
<select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="OrganizationalResultMap">
51
<include refid="config"/>
52
<include refid="BasicMapper.buildSelectSql"/>
53
</select>
54
55
<select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
56
<include refid="config"/>
57
<include refid="BasicMapper.buildTotalSql"/>
58
</select>
59
</mapper>
Copied!

覆盖默认的mapper.xml

java类配置方式

CustomMybatisMapperCustomizer
1
@Component //提供给spring才会生效
2
public class CustomMybatisMapperCustomizer implements MybatisMapperCustomizer {
3
@Override
4
public String[] getExcludes() {
5
//不加载默认的配置
6
return new String[]{
7
"classpath*:org/hswebframework/**/OrganizationalMapper.xml"
8
};
9
}
10
11
@Override
12
//追加配置
13
public String[] getIncludes() {
14
return new String[]{
15
"classpath*:com/myproject/mappers/OrganizationalMapper.xml"
16
};
17
}
18
}
Copied!

application.yml方式

application.yml
1
mybatis:
2
mapper-location-excludes: classpath*:org/hswebframework/**/OrganizationalMapper.xml #不加载的xml
3
mapper-locations: classpath*:com/myproject/mappers/OrganizationalMapper.xml
Copied!
Last modified 2yr ago