Java實(shí)現(xiàn)同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫
本文實(shí)例為大家分享了Java同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
1.需求說明:
我們?cè)陂_發(fā)中常常會(huì)用到數(shù)據(jù)字典,后端程序中也會(huì)經(jīng)常用到(一般是用枚舉類來存儲(chǔ)),然而我們數(shù)據(jù)庫中也會(huì)維護(hù)一個(gè)數(shù)據(jù)字典的數(shù)據(jù),便于前端做數(shù)據(jù)顯示時(shí)的處理,有一個(gè)問題就是,如果字典項(xiàng)發(fā)生變化后,我們需要修改枚舉類和數(shù)據(jù)庫的字典數(shù)據(jù),要修改兩次,還要面臨二者不一致的風(fēng)險(xiǎn)。
所以這里的一個(gè)決絕方案就是自動(dòng)讀取枚舉類的數(shù)據(jù)并更新到數(shù)據(jù)庫,本文只講枚舉類數(shù)據(jù)的提取。
2.首先創(chuàng)建一個(gè)描述枚舉類型的注解:
package com.visy.enums2dict.annotations;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnumDesc {
? ? String value();
}3.創(chuàng)建一個(gè)枚舉類接口,以規(guī)范枚舉類
package com.visy.enums2dict.interfaces;
public interface EnumInterface {
? ? String getCode();
? ? String getName();
? ? String getRemark();
? ? /**
? ? ?* 通過代碼獲取名稱
? ? ?*/
? ? String getNameByCode(String code);
}4.創(chuàng)建保存枚舉數(shù)據(jù)的實(shí)體
package com.visy.enums2dict.core;
public class DictEntity {
? ? private String typeCode;
? ? private String typeName;
? ? private String code;
? ? private String name;
? ? private String remark;
? ? DictEntity(){}
? ? DictEntity(String code, String name, String remark){
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? ? ? this.remark = remark;
? ? }
? ? DictEntity(String typeCode, String code, String name, String remark){
? ? ? ? this.typeCode = typeCode;
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? ? ? this.remark = remark;
? ? }
? ? DictEntity(String typeCode, String typeName, String code, String name, String remark){
? ? ? ? this.typeCode = typeCode;
? ? ? ? this.typeName = typeName;
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? ? ? this.remark = remark;
? ? }
? ? public void setTypeCode(String typeCode) {
? ? ? ? this.typeCode = typeCode;
? ? }
? ? public void setTypeName(String typeName) {
? ? ? ? this.typeName = typeName;
? ? }
? ? public void setCode(String code) {
? ? ? ? this.code = code;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? public void setRemark(String remark) {
? ? ? ? this.remark = remark;
? ? }
? ? public String getTypeCode() {
? ? ? ? return typeCode;
? ? }
? ? public String getTypeName() {
? ? ? ? return typeName;
? ? }
? ? public String getCode() {
? ? ? ? return code;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public String getRemark() {
? ? ? ? return remark;
? ? }
? ? public String toString(){
? ? ? ? return "typeCode="+this.getTypeCode()+",typeName="+this.getTypeName()+",code="+this.getCode()+",name="+this.getName()+",remark="+this.getRemark();
? ? }
}5.提取枚舉數(shù)據(jù)的核心類
package com.visy.enums2dict.core;
import com.visy.enums2dict.annotations.EnumDesc;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class EnumsToDict {
? ? //獲取指定包下的所有類路徑
? ? private static List<String> getClassesByPackage(String packagePath) {
? ? ? ? //獲取包的文件路徑
? ? ? ? String basePath = ClassLoader.getSystemResource("").getPath();
? ? ? ? String filePath = basePath + packagePath.replace(".", "/");
? ? ? ? //獲取包下所有類路徑
? ? ? ? List<String> classPathList = new ArrayList<String>();
? ? ? ? getClassPaths(filePath, classPathList);
? ? ? ? return classPathList;
? ? }
? ? private static void getClassPaths(String rootPath, List<String> result){
? ? ? ? File rootFile = new File(rootPath);
? ? ? ? File[] children = rootFile.listFiles();
? ? ? ? if(children==null){
? ? ? ? ? ? result.add(classPathPickUp(rootFile.getPath()));
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? for(File child: children){
? ? ? ? ? ? String childPath = child.getPath();
? ? ? ? ? ? if(child.isDirectory()){
? ? ? ? ? ? ? ? getClassPaths(childPath, result);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? result.add(classPathPickUp(childPath));
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //從文件路徑提取類路徑
? ? private static String classPathPickUp(String filePath){
? ? ? ? if(filePath!=null && !"".equals(filePath)){
? ? ? ? ? ? int start = filePath.indexOf("classes");
? ? ? ? ? ? int end = filePath.indexOf(".class");
? ? ? ? ? ? String classPath = filePath.substring(start,end).replace("\\",".");
? ? ? ? ? ? return classPath.replace("classes.","");
? ? ? ? }
? ? ? ? return filePath;
? ? }
? ? //獲取指定枚舉類的全部數(shù)據(jù)
? ? private static List<DictEntity> ?getDataByClass(String classPath){
? ? ? ? List<DictEntity> dictList = new ArrayList<DictEntity>();
? ? ? ? try{
? ? ? ? ? ? Class<?> clazz = Class.forName(classPath);
? ? ? ? ? ? Object[] values = clazz.getEnumConstants();
? ? ? ? ? ? EnumDesc enumDesc = ?clazz.getAnnotation(EnumDesc.class);
? ? ? ? ? ? String typeName = enumDesc!=null ? enumDesc.value() : null;
? ? ? ? ? ? Method m1 = clazz.getDeclaredMethod("getCode");
? ? ? ? ? ? Method m2 = clazz.getDeclaredMethod("getName");
? ? ? ? ? ? Method m3 = clazz.getDeclaredMethod("getRemark");
? ? ? ? ? ? Method.setAccessible(new Method[]{m1,m2,m3},true);
? ? ? ? ? ? for(Object value: values){
? ? ? ? ? ? ? ? String typeCode = value.getClass().getSimpleName();
? ? ? ? ? ? ? ? String code = (String)m1.invoke(value);
? ? ? ? ? ? ? ? String name = (String)m2.invoke(value);
? ? ? ? ? ? ? ? String remark = (String)m3.invoke(value);
? ? ? ? ? ? ? ? DictEntity dict = new DictEntity(typeCode,typeName,code,name,remark);
? ? ? ? ? ? ? ? dictList.add(dict);
? ? ? ? ? ? }
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return dictList;
? ? }
? ? //獲取指定包下所有枚舉配置數(shù)據(jù)
? ? public static List<DictEntity> getDictsOfPackage(String pkgPath) {
? ? ? ? List<String> list = getClassesByPackage(pkgPath);
? ? ? ? List<DictEntity> dictList = new ArrayList<DictEntity>();
? ? ? ? for(String path: list){
? ? ? ? ? ? dictList.addAll(getDataByClass(path));
? ? ? ? }
? ? ? ? return dictList;
? ? }
}6.準(zhǔn)備兩個(gè)枚舉類(需實(shí)現(xiàn)2中的接口和1的注解)
package com.visy.enums2dict.enums;
import com.visy.enums2dict.annotations.EnumDesc;
import com.visy.enums2dict.interfaces.EnumInterface;
@EnumDesc("入庫單狀態(tài)")
public enum InbStatus implements EnumInterface {
? ? CREATE("100","新建"),
? ? PALLET_FINISH("260","碼盤完成"),
? ? PART_FINISH("300", "部分完成"),
? ? FULL_FINISH("310","全部完成"),
? ? CLOSE("950", "關(guān)閉"),
? ? CANCEL("999", "取消");
? ? private String code;
? ? private String name;
? ? private String remark;
? ? InbStatus(String code, String name){
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? }
? ? InbStatus(String code, String name, String remark){
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? ? ? this.remark = remark;
? ? }
? ? public String getCode() {
? ? ? ? return code;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public String getRemark() {
? ? ? ? return remark;
? ? }
? ? public String getNameByCode(String code){
? ? ? ? for(InbStatus status : InbStatus.values()){
? ? ? ? ? ? if(code!=null && code.equals(status.getCode())){
? ? ? ? ? ? ? ? return status.getName();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}package com.visy.enums2dict.enums;
import com.visy.enums2dict.annotations.EnumDesc;
import com.visy.enums2dict.interfaces.EnumInterface;
@EnumDesc("出庫單訂單狀態(tài)")
public enum OubStatus implements EnumInterface {
? ? ALL_ALLOCATE("300","全部分配"),
? ? PART_JH("320","部分揀貨");
? ? private String code;
? ? private String name;
? ? private String remark;
? ? OubStatus(String code, String name){
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? }
? ? OubStatus(String code, String name, String remark){
? ? ? ? this.code = code;
? ? ? ? this.name = name;
? ? ? ? this.remark = remark;
? ? }
? ? public String getCode() {
? ? ? ? return code;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public String getRemark() {
? ? ? ? return remark;
? ? }
? ? public String getNameByCode(String code){
? ? ? ? for(InbStatus status : InbStatus.values()){
? ? ? ? ? ? if(code!=null && code.equals(status.getCode())){
? ? ? ? ? ? ? ? return status.getName();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}7.測(cè)試:
package com.visy.enums2dict.test;
import com.visy.enums2dict.core.DictEntity;
import com.visy.enums2dict.core.EnumsToDict;
import java.util.List;
public class EnumsToDictTest {
? ? public static void main(String[] args) {
? ? ? ? List<DictEntity> dictList = EnumsToDict.getDictsOfPackage("com.visy.enums2dict.enums");
? ? ? ? for(DictEntity dict: dictList){
? ? ? ? ? ? System.out.println(dict.toString());
? ? ? ? }
? ? }
}8.輸出結(jié)果:
typeCode=InbStatus,typeName=入庫單狀態(tài),code=100,name=新建,remark=null
typeCode=InbStatus,typeName=入庫單狀態(tài),code=260,name=碼盤完成,remark=null
typeCode=InbStatus,typeName=入庫單狀態(tài),code=300,name=部分完成,remark=null
typeCode=InbStatus,typeName=入庫單狀態(tài),code=310,name=全部完成,remark=null
typeCode=InbStatus,typeName=入庫單狀態(tài),code=950,name=關(guān)閉,remark=null
typeCode=InbStatus,typeName=入庫單狀態(tài),code=999,name=取消,remark=null
typeCode=OubStatus,typeName=出庫單訂單狀態(tài),code=300,name=全部分配,remark=null
typeCode=OubStatus,typeName=出庫單訂單狀態(tài),code=320,name=部分揀貨,remark=null
然后,你就可以將這些數(shù)據(jù)同步到數(shù)據(jù)庫啦
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟
這篇文章主要介紹了如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java獲取漢字對(duì)應(yīng)的拼音(全拼或首字母)
這篇文章主要介紹了Java如何獲取漢字對(duì)應(yīng)的拼音(全拼或首字母),文中實(shí)現(xiàn)的方法是引用了pinyin4j-2.5.0.jar,然后給出了完整的示例代碼,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
java實(shí)現(xiàn)優(yōu)酷視頻地址解析示例代碼分享
最近做了一個(gè)在線視頻的下載器,需要解析youku的視頻,獲得真正的視頻地址,現(xiàn)在把解析過程記錄下來以供參考2014-01-01
MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用
動(dòng)態(tài)?SQL?是?MyBatis?的強(qiáng)大特性之一。如果你使用過?JDBC?或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接?SQL?語句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(dòng)態(tài)?SQL,可以徹底擺脫這種痛苦2022-04-04
Java計(jì)算兩個(gè)時(shí)間段的差的實(shí)例詳解
在本篇內(nèi)容中,我們給大家整理了關(guān)于Java計(jì)算兩個(gè)時(shí)間段的差的實(shí)例內(nèi)容,并做了詳細(xì)分析,有需要的朋友們學(xué)習(xí)下。2022-11-11
java構(gòu)造http請(qǐng)求的幾種方式(附源碼)
本文主要介紹了java構(gòu)造http請(qǐng)求的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
SpringBoot開發(fā)技巧之如何處理跨域請(qǐng)求CORS
CORS(Cross-Origin Resource Sharing)"跨域資源共享",是一個(gè)W3C標(biāo)準(zhǔn),它允許瀏覽器向跨域服務(wù)器發(fā)送Ajax請(qǐng)求,打破了Ajax只能訪問本站內(nèi)的資源限制2021-10-10

