最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫

 更新時間:2022年08月10日 11:14:14   作者:風(fēng)喃海北  
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

1.需求說明:

我們在開發(fā)中常常會用到數(shù)據(jù)字典,后端程序中也會經(jīng)常用到(一般是用枚舉類來存儲),然而我們數(shù)據(jù)庫中也會維護(hù)一個數(shù)據(jù)字典的數(shù)據(jù),便于前端做數(shù)據(jù)顯示時的處理,有一個問題就是,如果字典項發(fā)生變化后,我們需要修改枚舉類和數(shù)據(jù)庫的字典數(shù)據(jù),要修改兩次,還要面臨二者不一致的風(fēng)險。

所以這里的一個決絕方案就是自動讀取枚舉類的數(shù)據(jù)并更新到數(shù)據(jù)庫,本文只講枚舉類數(shù)據(jù)的提取。

2.首先創(chuàng)建一個描述枚舉類型的注解:

package com.visy.enums2dict.annotations;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnumDesc {
? ? String value();
}

3.創(chuàng)建一個枚舉類接口,以規(guī)范枚舉類

package com.visy.enums2dict.interfaces;

public interface EnumInterface {

? ? String getCode();

? ? String getName();

? ? String getRemark();

? ? /**
? ? ?* 通過代碼獲取名稱
? ? ?*/
? ? String getNameByCode(String code);
}

4.創(chuàng)建保存枚舉數(shù)據(jù)的實體

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)備兩個枚舉類(需實現(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.測試:

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java:程序包com.xxx.xxx不存在報錯萬能解決辦法

    java:程序包com.xxx.xxx不存在報錯萬能解決辦法

    這篇文章主要給大家介紹了關(guān)于java:程序包com.xxx.xxx不存在報錯萬能解決辦法,這個問題曾逼瘋初學(xué)者的我,不過弄清楚原理后就很簡單了,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • java如何獲取視頻文件的編解碼器代碼示例

    java如何獲取視頻文件的編解碼器代碼示例

    這篇文章主要給大家介紹了關(guān)于java如何獲取視頻文件的編解碼器的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-05-05
  • 淺析java創(chuàng)建文件和目錄

    淺析java創(chuàng)建文件和目錄

    這篇文章主要介紹了淺析java創(chuàng)建文件和目錄的關(guān)鍵技術(shù)點以及演示示例,是篇非常不錯的文章,有需要的朋友可以參考下
    2014-09-09
  • idea2017建立jsp工程及tomcat配置教程

    idea2017建立jsp工程及tomcat配置教程

    本文通過圖文并茂的形式給大家介紹了idea2017建立jsp工程及tomcat等配置的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • java volatile關(guān)鍵字作用及使用場景詳解

    java volatile關(guān)鍵字作用及使用場景詳解

    在本文里我們給大家分享的是關(guān)于java volatile關(guān)鍵字作用及使用場景的相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • SpringBoot框架集成ElasticSearch實現(xiàn)過程示例詳解

    SpringBoot框架集成ElasticSearch實現(xiàn)過程示例詳解

    這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • Java基礎(chǔ)之Maven詳解

    Java基礎(chǔ)之Maven詳解

    這篇文章主要介紹了Java基礎(chǔ)之Maven詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java將對象保存到文件中/從文件中讀取對象的方法

    Java將對象保存到文件中/從文件中讀取對象的方法

    下面小編就為大家?guī)硪黄狫ava將對象保存到文件中/從文件中讀取對象的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • java中String StringBuffer和StringBuilder的區(qū)別詳解

    java中String StringBuffer和StringBuilder的區(qū)別詳解

    大家好,本篇文章主要講的是java中String StringBuffer和StringBuilder的區(qū)別詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • 基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過程

    基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過程

    這篇文章主要介紹了基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過程,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09

最新評論

芮城县| 龙南县| 呼伦贝尔市| 南汇区| 新和县| 黔南| 淳安县| 乐清市| 福建省| 伊春市| 游戏| 克什克腾旗| 新泰市| 铅山县| 昌邑市| 肇东市| 清水河县| 大宁县| 施秉县| 治县。| 乡宁县| 桃源县| 大丰市| 和平区| 韶山市| 屏边| 南雄市| 徐汇区| 襄樊市| 阿拉尔市| 平塘县| 姜堰市| 灵山县| 临潭县| 武鸣县| 华宁县| 孝义市| 三明市| 平果县| 海口市| 芒康县|