VUE+ElementUI+springboot項目中 數據字典表全面解析與使用場景 PO轉VO時進行轉義 令前端下拉列表動態生成 前端VUE表格列的動態轉義


1 產生數據字典表的原因

現在有這么一張表,其中的大部分字段都是固定值,比如訂單分類、類型、動作、狀態、業務類型

如果都以字符串的形式存入數據庫中,就存在數據冗余的情況,而且如果希望修改某一字段的字符串(比如從“來自網絡的訂單”改為“網絡訂單”),那就不是修改一條,而是需要修改許多條

訂 單 號 
訂 單 分 類 
訂 單 類 型 
訂 單 
訂 單 狀 態 
業 務 類 型 
下 單 日 
收 貨 人 資 料 
訂 單 商 品 信

 

因此通常以數字的格式存入表中,此時原表的數據如圖

order id 
order status 
order number order class 
2021092212 
2021092323 
2021092324 
order type 
order action 
2 
2 
business type 
9 
9 
3 
Order date consignee 
1 2021-09 22 22: 
2 2021-09 23 21: 
2 2021-09-23 21: 
Remark 
2 WEI 
2 
3

 

而每個字段的數字與字符串的對應關系單獨建一張表進行管理,示意圖如下

訂 單 一 
訂 單 狀 態 
業 昔 口 
務 涌 、 
類 業 業 
型 
1 出 庫 待 審 
1 訂 
2 換 
2 出 庫 審 
3 已 出 
4 已 發 
5 已 到 
6 已 簽 刂 
7 已 拒 刂 
8 已 付 款 
9 退 貨 待 審 
配 送 、 
10 退 貨 審 
11 退 
1 斟 
付 方 、 
1 貨 到 付 發 七 
凵 士 了

 

但是如果字段很多,比如上面有5個字段,那么進行轉義時就要連接6張表進行查詢,而這並不是為了業務進行連表,只是單純需要轉義,這就非常麻煩了

每個字段分別建一個表也很繁瑣,不方便管理,此時就可以考慮把這些字段全部統合在一張表里——數據字典表

數據字典表 能起到減少代碼量以及靈活修改固定值的作用

 

2 數據字典表建表代碼

2.1 一張表

如圖,就是數據字典表,每個字段根據type進行區分,同一type的數據再根據type_value進行區分,並與type_text構成對應關系用於轉義

id 
type type value 
type text 
1 訂 購 
2 
3 支 付 
1 出 庫 待 審 核 
2 出 庫 正 在 審 核 
3 已 出 庫 
4 已 發 貨 
5 已 到 貨 
6 已 簽 收 
7 已 拒 收 
8 已 付 款 
9 退 貨 待 審 核 
10 退 貨 審 核 
11 退 貨 丿 、 庫 
2 號 上 業 務 
1 B2C

 

數據字典表建表語句一覽

sales dictionary' 
DROP TABLE IF EXISTS 
- CREATE TABLE sales dictionary ' ( 
id' int (11) NOT NULL AUTO INCREMENT, 
int (11) DEFAULT NULL COMMENT 1. 2 
type 
int (11) DEFAULT NULL COMMENT '17' , 
type value 
varchar (2SS) DEFAULT NULL COMMENT 
type text 
PRIMARY KEY ('id') 
10 DEFAULT CHARSET=utf8,• 
ENGINE=1nnoDB AUTO INCREMENT

CREATE TABLE `sales_dictionary` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` int(11) DEFAULT NULL COMMENT '類型 1.訂單動作 2 訂單狀態 3 業務類型 4 訂單分類 5 訂單類型 6 配送方式 7 支付方式',
  `type_value` int(11) DEFAULT NULL COMMENT '值',
  `type_text` varchar(255) DEFAULT NULL COMMENT '文本',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

2.2 兩張表

上面是簡單的建表,將類型、keyvalue統合在一張表,方便直接查詢使用,但很明顯,時間久了誰記得type中的數字對應什么,如果直接改用字符串,又會陷入“改一個字符串就要修改多條記錄”的情況

因此實際上應該拆為兩張表。

兩張表的建表方式以及查詢方式具體見SpringBoot+Redis實現數據字典_蹊源的奇思妙想的博客-CSDN博客_springboot緩存數據字典

下面是摘錄:

1 
2 
3 
4 
5 
6 
7 
8 
9 
le 
11 
12 
13 
14 
15 
16 
17 
18 
19 
SYS_DICT 
SET FOREIGN KEY CHECKS=a; 
- Table structure for SYS DICT 
DROP TABLE IF EXISTS 'SYS DICT' 
CREATE TABLE 'SYS DICT' ( 
-code • varchar(36) NOT NULL COMMENT 
•type_code' varchar(36) DEFAULT NULL COWENT 
•name• varchar(5€) DEFAULT NULL commENT '*IA' 
•value' int(2€) DEFAULT NULL COMMENT 
'fixed' int(2) DEFAULT NULL C0'1MENT 'default äÉfiiä%1', 
•creater• varchar(28) DEFAULT NULL commENT '*A' 
create _ time' datetime DEFAULT NULL COWIENT , 
•updater' varchar(28) DEFAULT NULL COMMENT 
update _ time' datetime DEFAULT NULL COWIENT , 
('code') 
PRIMARY KEY C code' ) , 
KEY sys_type• ('type_code• ) , 
CONSTRAINT sys_type' 
FOREIGN KEY ('type_code• ) REFERENCES 
CHARSET=utf8; 
ENGINE=1nnoDB DEFAULT 
'SYS DICT TYPE

1 
2 
3 
4 
5 
6 
7 
8 
9 
le 
11 
12 
13 
14 
15 
16 
17 
18 
19 
SET FOREIGN KEY CHECKS=a; 
- Table structure for SYS DICT TYPE 
DROP TABLE IF EXISTS 'SYS DICT TYPE' 
CREATE TABLE 'SYS DICT _ TYPE' ( 
-code • varchar(36) NOT NULL, 
•name• varchar(5€) DEFAULT NULL commENT 
•value' varchar(se) DEFAULT NULL COWIENT 
•creater• varchar(28) DEFAULT NULL commENT '*A' 
create _ time' datetime DEFAULT NULL COWIENT , 
•updater' varchar(28) DEFAULT NULL COMMENT 
•updater_time• datetime DEFAULT NULL COMMENT , 
PRIMARY KEY ( • code' ) , 
UNIQUE KEY key_value• ('value' ) 
ENGINE=1nnoDB DEFAULT CHARSET=utf8;

 

3 查詢數據庫的數據字典表並緩存到集合中

3.1 步驟一 建一個與字典對應的實體類

如圖,為單張數據字典表對應的實體類,姑且可以理解為PO,如果是兩張數據字典表,那么就是兩表聯查的PO

- @Data 
-@Equa1sAndHashCode(ca11Super = false) 
public class SalesDictionary implements Serializable {l 
private static final long seriaLVersionUID = IL; 
@Tab1eId(va1ue = " id" , 
private Integer id; 
type 
IdType . AUTO) 
* 2 3 4 iTæ5ö*_ 5 iTæ3& 6 7 
private Integer type; 
@Tab1eFie1d("Type_value") 
private Integer typeVa1ue; 
@Tab1eFie1d("Type_text " ) 
private String typeText;

 

3.2 步驟二 寫一個mapper 查詢數據字典表

3.2.1 單張數據字典表

在后端寫一個mapper,查詢數據庫的字典表

public interface SalesDictionaryMapper extends BaseMapper<Sa1esDictionary> { 
List<Sa1esDictionary> findA11();

 

(select 
select * from sales_dictionary; 
< / select>

 

3.2.2 兩張數據字典表

相比起一張表,兩張表管理更為清晰,只是在后端mapper查詢數據庫表時,需要兩張表進行聯查

參考SpringBoot+Redis實現數據字典_蹊源的奇思妙想的博客-CSDN博客_springboot緩存數據字典

getall . 
<select id="getall" resultType="com.luo.dao.entity.SysDictPojo"> 
1 
select 
2 
3 
t1. name, 
t1. value, 
4 
t2. value typeCodeVa1ue 
5 
from SYS DICT tl 
6 
7 
8 
left join SYS DICT TYPE t2 on t2. code 
. type_code

 

3.3 步驟三 編寫詞典全局緩存方法

3.3.1 存放位置

直接復制下面的代碼,粘貼到IDEA

com 
javasm 
V common 
cache 
constants 
dictionary 
InitContext

 

3.3.2 代碼

注意根據步驟一和二 修改一下聲明的mapper接口以及集合中的元素類型

@Component
public class InitContext {

    /**
     * 詞典全局緩存對象
     * key : type 表示哪個類型
     * value中的hashMap中的key : 表示value value表示text文本
     */
    public static final Map<Integer,LinkedHashMap<Integer,String>> dic = new HashMap<>();
    //ProjectApplication是當前springboot模塊的啟動類,Logger類是一個日志對象 作用是在緩存好詞典后,在控制台提示一下
    Logger logger = LoggerFactory.getLogger(ProjectApplication.class);
    @Resource
    private SalesDictionaryMapper salesDictionaryMapper;

    /**
     * 容器創建自動執行
     */
    @PostConstruct
    public void init(){
        initDictionary();
    }
    
    /**
     * 初始化詞典
     */
    public void initDictionary(){
        List<SalesDictionary> all = salesDictionaryMapper.findAll();
        all.forEach(sysDictionary -> {
            LinkedHashMap<Integer, String> kv = dic.get(sysDictionary.getType());
            if(kv == null){
                kv = new LinkedHashMap<>();
            }
            kv.put(sysDictionary.getTypeValue(),sysDictionary.getTypeText());
            dic.put(sysDictionary.getType(),kv);
        });
        //緩存完畢,logger對象在控制台打印下面的語句進行說明,可以不要
        logger.debug("詞典初始化完成");
    }
}

3.3.3 幾個知識點

3.3.3.1 Logger,是一個日志對象

java日志LoggerFactory.getLogger最全講解使用方法_滑稽的鼠標的博客-CSDN博客_loggerfactory.getlogger

下圖划線代碼就是在springboot的啟動類下創建一個日志對象,其實不是必須的

@Component 
public class InitContext { 
* key : type 
* : *iÄvaLue 
new HashMap<>(); 
public static final Map<lnteger, LinkedHashMap<Integer, dic 
Logger logger = LoggerFactory. getLogger(ProjectApp1ication . class) ; 
@Resource 
private SalesDictionaryMapper sales ictlonarymapper;

 

這個對象要做的就是在springboot啟動完畢並且詞典緩存后,在控制台提示一下,這不是必須要做的

 

3.3.3.2 @PostConstruct 這個注解注釋的方法會在整個類被注入spring容器后自動執行

springboot 啟動時加載數據(字典)到內存 - 簡書 (jianshu.com)

主要看文章中對 @PostConstruct”的介紹 以及 存在“BiMap”這種東西,能把mapvalue也唯一化,不過我這里沒有用

 

下面是部分摘錄

@PostConstruct

spring中Constructor、@Autowired、@PostConstruct的順序(網上解釋比較清楚的版本)

 

要將對象p注入到對象a,那么首先就必須得生成對象p與對象a,才能執行注入。所以,如果一個類A中有個成員變量p被@Autowired注解,那么@Autowired注入是發生在A的構造方法執行完之后的。

 

如果想在生成對象時候完成某些初始化操作,而偏偏這些初始化操作又依賴於依賴注入,那么就無法在構造函數中實現。為此,可以使用@PostConstruct注解一個方法來完成初始化,@PostConstruct注解的方法將會在依賴注入完成后被自動調用。

 

Constructor >> @Autowired >> @PostConstruct

 

3.3.3.3 字典的數據類型 Map<Integer,LinkedHashMap<Integer,String>> dic

這是一個嵌套map,外層mapkey對應typevalue是一個LinkedHashMapLinkedHashMapkey對應type_valuevalue對應type_text

實際使用時,可以根據自己的字典表格式變化,比如只建立一張數據字典表的情況下,type不用數字,而是用字符串,那么此時外層mapkey就要用String

 

4 VUE+springboot項目中使用數據字典表

4.1 緩存完畢的字典表樣式

數據字典表存到項目的map后,數據結構如圖

logger . 
System. out. println( - 
¯ .xxer . hikari . HikariDataSource 
s . SalesDictionaryMapper . findA11 
> " +dic . toString()); 
s . SalesDictionaryMapper . findA11 
Hi kariP001-1 
— = > Preparing: 
Parameters: 
Total : 
Start 
select 
25 
System. out. println(dic . get(l) . tostring()) ; 
s . SalesDictionaryMapper . findA11 
System. out. println (dic . get (1) . get (1) 
J _.vasm . ProjectApp1ication 
3*ff},

 

4.2 使用方式 直接使用

緩存數據字典表后,可以直接在controller類、serviceimpl類或者transfer類中使用“dic"IDEA會提示需要導入,直接導入即可

son (dic . get (8) . get (salesReturn . getReason 
Create local variable 'dic 
Create field 'die in 'SalesReturnTransfer' 
Create parameter 'die 
Import static constant 'com.javasm.common.dictionary.InitContext.dic' 
Oat 
Qualify static constant access 'com.javasm.common.dictionary.InitContext.dic'

 

- import static com.javasm.common . dictionary. InitContext . dic;

 

4.3 應用場景一 POVO時進行轉義

POVO時,在transfer類中使用,第一個get要人為指定,第二個get直接將PO的屬性值傳入,這樣VO就能獲得對應的字符串

//EF*ällfJÄä 
if (salesReturn . getReason() 
salesReturnVO. setReason (dic . get (8) . get (salesReturn . getReason 
26 
pe$Ä

 

可以把數據庫的字典表的注釋放在實體類上 方便查看

沖 類 型 1. 訂 單 動 作 2 訂 單 狀 態 3 業 務 類 型 4 訂 單 分 類 5 訂 單 類 型 5 配 送 方 式 7 支 付 方 式 8 客 戶 拒 收 原 因 
pr 、 Ivate Integer 、 type;

 

4.4 應用場景二 令前端下拉列表動態生成

4.4.1 效果圖

前端基於VUE+ElementUI,要實現的效果如圖

訂 望 編 號 
下 望 日 
訂 望 類 型 
開 始 凸 
毛 話 訂 望 
電 話 iJ 單 
訂 望 
0 查 河 
前 端 的 下 拉 列 表 的 | abe 廂 va 》 ue 動 態 生 成 
號 訂 單 編 號 
訂 單 類 型

 

4.4.2 后端

之前考慮的做法是每個字段單獨建表,后端分別查詢,然后傳給前端,一旦字段多了,那也太麻煩了

現在有了數據字典表,直接把數據字典表傳給前端即可

" dic " ) 
public AxiosResu1t<Map<Integer, LinkedHashMap<Integer, dic(){ 
return AxiosResu1t.success(dic);

 

封裝類如圖,返回成功的方法的statusmessage已經用枚舉類預設好

public enum EnumStatus { 
OK(20000, " ) , 
ERROR(40000, 
NO LOGIN(33333, "*F"), 
"R F-€öffifi"), 
".%$iEi$8Eifi"), 
PASSWOED ERROR(22224, 
FORM VALICATOR ERROR(33233, 
"iLi!H"), 
* preturn 
public class AxiosResu1t<T> { 
private int status; 
private String message; 
private T data; 
public static < T > AxiosResu1t<T> success() { 
return OK, data: null);

 

postman請求結果一覽

“ St t05 “ : 22222J 
作 成 功 
"data" : { 
. 出 庫 待 審 核 
. 出 庫 審 核 
. 已 出 庫 
. 已 發 貨 
. 已 到 貨 
. 已 拒 收 
. 退 貨 待 審 核 
" 退 貨 審 核 
" 退 貨 入 庫

 

4.4.3 前端

4.4.3.1 請求方法

前端基於VUE+ElementUI

前端向后端的controller發送請求,其中instance是自定義封裝好的類

wtÄEaxios 
axios . create({ 
nconst instance 
baseURL: 
' http://10ca1host:8088/', 
timeout: 3000 
- export function { 
return instance . get( 
sales/dic•

 

4.4.3.2 methods

methods的代碼如下,獲取到數據后,存到VUE的屬性下

async queryDic(){ 
await queryA11Dic(); 
let result 
2 3 4 5 6 gæi*fijt 7 
result 7 ] ; 
this . 
res u 1 t C 5 ] ; 
this . 
res u 1 t C 3 ] ; 
this. 
. orderActionList= result [1]; 
this 
. orderStatusList= resultC2]; 
this 
orderTypeList: ] / 
pay Typel_ist: C], / /-*-fffij-tt 
business Typel_ist: c], 
orderActionList: C ] , / 
orderStatusList:

 

4.4.3.3 前端下拉列表代碼

ElementUI實現的下拉列表代碼

<div class:" search-form" > 
<el-form : inline="true" 
class=" demo-form-inline" size=" mini " > 
<el-form-item 
<el-input . orderNumber" 
< / el -form-item> 
<el-form-item " > 
el-select . orderType" placeholder= " > 
<el-option v-for=" (key, value) in orderTypeList" :label="key" : 
< / el - select > 
< / el -form-item> 
<el-form-item 
el-select v-mode1="searchParams . pay Type" placeholder= 
<el-option v-for=" (key, value) in pay Typel_ist" :label="key" : 
< / el - select > 
< / el -form- item> 
<el-form-item " > 
el-select v-mode1="searchParams . business Type" placeholder= " > 
<el-option v-for=" (key, value) in business Typel_ist" :label="key" : 
< /Åi select> 
< / el -form-item> 
<el-form-item 
el-select v-mode1="searchParams . orderAction" placeholder= > 
<el-option v-for=" (key, value) in orderActionList" :label="key" : 
< /ÅI -select > 
</el-form- item>

 

4.4.4 前端的知識點說明

后端沒啥要說的,倒是前端讓我吃盡了苦頭,下面是我遇到的問題和解決辦法

 

4.4.4.1 console.log顯示object

ajax請求后的返回結果顯示[object Object]的原因_IT_townlet的博客-CSDN博客

console.log時不要加字符串

在這里插入圖片描述

 

4.4.4.2 后端返回的數據是Promise對象

打印后端傳進來的數據時返回數據是Promise對象_Olliverzhang的博客-CSDN博客

要給方法加同步鎖,即asyncawait

async queryDic(){ 
await queryA11Dic(); 
let result 
result 7 ] ; 
this . 
res u 1 t C 5 ] ; 
this . 
res u 1 t C 3 ] ; 
this . 
. orderActionList= result [1]; 
this 
. orderStatusList= resultC2]; 
this

 

4.4.4.3 json取值方式 json對象[]

JSON取值(key是中文或者數字)方式詳解_Care_about的博客-CSDN博客

json的取值方式有好幾種,對於后端傳遞的這種嵌套map,需要手動指定一下外層mapkey

async queryDic(){ 
await queryA11Dic(); 
let result 
2 3 4 5 6 gæi*fijt 7 
. resultC7]; 
this 
res u 1 t C 5 ] ; 
this. 
res u 1 t C 3 ] ; 
this . 
. orderActionList= result [1]; 
this 
. orderStatusList= resultC2]; 
this

 

4.4.4.4 ElementUI的下拉列表動態生成的方式

筆記: SpringBoot + VUE實現數據字典展示功能_CJG753951的博客-CSDN博客

下圖是文章摘錄,其中的取值方式啟發了我

1 
2 
3 
4 
5 
6 
7 
8 
9 
11 
12 
13 
14 
15 
16 
17 
18 
19 
-table-column 
v-for=" (k, index) in data " 
align="center" 
: prop= " k" 
: key—I' index" 
show-overflow-tooltip > 
(template slot-scope="scope"> 
(span v-if=" ! dictionary[k] 
(span v-else> 
(div v-for=" (v, i) in dictionaryCk)" 
(span v . rowCk] - 
v . dict typeCode " span 
< / span> 
</template> 
</el -table-col umn>

 

動態生成的下拉列表,可以在遍歷json對象時,將獲得的元素拆開使用

<el-form-item " > 
el-select . orderType" placeholder= " > 
<el-option v-for=" (key, value) in orderTypeList" :label="key" : 
< / el - selecg> 
</el-form- item>

 

可以對比寫死的情況,如圖

<el-form-item " > 
@-select . reason' 
<el-option v-for="type in reasonList" :label="type. reasonName" : value="type. reasonld" 
< /e1- select > 
< / el - form- item> 
reasonList:C 
{reasonld: "1" , 
{reasonld: "2" 
reasonName : 
reasonName :

 

4.5 應用場景三 前端VUE表格列的動態轉義

筆記: SpringBoot + VUE實現數據字典展示功能_CJG753951的博客-CSDN博客

Vue 利用后端的數據字典和Map對象實現表格列字段動態轉義的處理方案 - 阿拉伯1999 - 博客園 (cnblogs.com)

這個場景與應用場景一相悖,畢竟如果POVO時就把數字轉為字符串了,前端就沒必要轉了,我通常會依據應用場景一去實現轉義,所以這個我就不研究了,僅放在這里

 

5 數據字典表緩存到本地內存還是redis

5.1 結論 依據公司技術選型而定

在實際使用時,需要在整個項目啟動時就執行查詢語句,把數據字典表整體保存到一個map中,這個map再存到redis或者本地內存中,然后在項目的各個類中隨調隨用

而到底是直接用staticmap存到內存中還是直接把map存到redis中,就看以后的技術選型,從速度上看前者肯定更快,因為用的就是本地的內存

 

5.2 本文緩存到本地

本篇文章是將字典表存在本地內存中,所以使用了static修飾方法

 

5.3 緩存到redis的方式

如果要探究存到redis中,參見SpringBoot+Redis實現數據字典_蹊源的奇思妙想的博客-CSDN博客_springboot緩存數據字典

文章的部分知識點的個人理解如下

 

5.3.1 springUtils工具類

文章中的RedisDistUtil類用於獲取被spring管理的service接口對象,然后調用這個對象的方法實現轉義

RedisDisWtil: spring Pojo iBÆ service , 
1 
2 
3 
4 
5 
6 
7 
8 
9 
le 
11 
12 
13 
14 
15 
public class RedisDistUti1 { 
private static ApplicationContext context; 
@param distname 
@param value 
* @return 
* @throws Exception 
spring Bean . 
(service}EQ) (transformstr) 
public static String transformStr(String distname, int value) { 
ApplicationContext context = Springutil. getApp1icationContext(); 
ISysDictService iSysDictService —context . get8ean(ISysDictService.c1ass) 
return iSysDictService. transformstr(distname, value);

 

為了能從spring容器中取出service接口對象,借助了springUtils工具類,這是一個自定義的工具類,用於從spring容器中取出對象

①本篇文章的數據字典表被定義為static 靜態的對象,被存在內存中,所以可以直接使用,不需要springUtils這種類

 

②如果不使用這個工具類,而是在當前類聲明要使用的對象並利用@Autowired等注解注入spring容器,那么還得先把最外層這個類(RedisDistUtil)注入spring容器,然而注入spring容器的類不是越多越好

 

③顯然,這個工具類要求springboot項目啟動,否則spring容器中是空的,方法getBean取不到東西就報空指針異常

Exception in thread "main" java. lang. NullPointerException 
at com.javasm.common . util . SpringContainerLlti1s . getBean(SprineContainerlJti1s. -java: 24) 
at com.javasm.common . excel. ExcelReaderDemo. main(Exce1ReaderDemo . iava: 24)

 

5.3.2 RedisTemplate

文章中提到spring封裝了一個對象叫RedisTemplate 基於這個對象提供的方法 我們能操控redis儲存的數據

. ( ) ; / /Î*fËhEEh 
.2PEFzzLiEZ ( ) ; / 
.2PEFzzSet ( ) ; / 
.2PEFzzZSeZ ( ) ; /

 

6 數據字典表和常量類對比

我沒有使用數據字典表時,直接在項目建了一個常量類,如圖

public final class SalesConstants { 
private SalesConstants(){} 
Java sm 
common 
async 
cache 
constants 
SalesConstants 
dictiona ry 
InitCantext 
exception 
reflect 
config 
controller 
aftersale 
•O AdminController 
AreaController 
18 
11 
12 
13 
14 
15 
16 
17 
18 
19 
28 
21 
22 
23 
24 
25 
26 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
int ORDER acrrotv ORDER = 1; 
int ORDER ACTION REDEEM = 2; 
int ORDER acrrotv PAY = 3; 
String CN 
ORDER acrrotv ORDER 
String CN 
ORDER acrrotv REDEEM ; 
String CN 
ORDER acrrotv PAY ; 
int 
int 
int 
int 
int 
int 
int 
int 
int 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
STA rus 
STA rus 
STA rus 
STA rus 
STA rus 
STA rus 
STA rus 
STA rus 
STA rus 
PENDING REVIEW = 1; 
UNDER REVIEW = 2; 
OUT OF STOCK = 3; 
SHIPPED = 4; 
ARRIVED = 5; 
RECEIVED = 6; 
RETURN = 7; 
PAY = 8; 
RETURN PENDING REVIEW

 

借助這個常量類也能實現轉義以及管理,但真的太麻煩了

比如轉義,代碼量可見一斑,而且每當需要新增一條“訂單動作”,我就需要到各個transform中為switch-case增加一條

if (salesOrderConnection . getOrderAction() ! —null) 
switch (salesOrderConnection . getOrderAction( 
case SalesConstants.ORDER ACTION ORDER: 
salesOrderConnectionVO. setOrderAction(Sa1esConstants.CN ORDER ACTION ORDER) ; 
break; 
case SalesConstants.ORDER ACTION REDEEM: 
salesOrderConnectionVO. setOrderAction(Sa1esConstants.CN ORDER ACTION REDEEM) ; 
break; 
case SalesConstants.ORDER ACTION PAY: 
salesOrderConnectionVO. setOrderAction(Sa1esConstants.CN ORDER ACTION PAY); 
break; 
if (salesOrderConnection . getOrderAction() ! —null) 
salesOrderConnectionVO. setOrderAction ( dic . get (1) . get (salesOrderConnection . getOrderAction

 

再就是管理時,每新增一條,我就得准備一個常量名,寫起來特麻煩

public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
public 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
static 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
final 
int 
int 
int 
int 
int 
int 
int 
int 
int 
int 
int 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
ORDER 
PENDING REVIEW = 1; 
UNDER REVIEW = 2; 
OUT OF STOCK = 3; 
SHIPPED = 4; 
ARRIVED = 5; 
RECEIVED = 6; 
RETURN = 7; 
PAY = 8; 
RETURN PENDING REVIEW = 9; 
RETURN UNDER REVIEW 
= 11; 
10; 
String 
String 
String 
String 
String 
String 
String 
String 
String 
String 
String 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
s TA rus 
PENDING REVIEW 
UNDER REVIEW 
OUT OF STOCK 
SHIPPED " " ; 
ARRIVED " " ; 
RECEIVED " ; 
RETURN " ; 
PAY 
RETURN PENDING REVIEW 
RETURN UNDER REVIEW 
s TOCK IN

 

現在固定值寫在一張數據庫表中 相比常量類就方便維護了

 

並且前端下拉菜單不再需要寫死 直接去讀字典表類的map即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM