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

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

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

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

1.需求說(shuō)明:

我們?cè)陂_(kāi)發(fā)中常常會(huì)用到數(shù)據(jù)字典,后端程序中也會(huì)經(jīng)常用到(一般是用枚舉類來(lái)存儲(chǔ)),然而我們數(shù)據(jù)庫(kù)中也會(huì)維護(hù)一個(gè)數(shù)據(jù)字典的數(shù)據(jù),便于前端做數(shù)據(jù)顯示時(shí)的處理,有一個(gè)問(wèn)題就是,如果字典項(xiàng)發(fā)生變化后,我們需要修改枚舉類和數(shù)據(jù)庫(kù)的字典數(shù)據(jù),要修改兩次,還要面臨二者不一致的風(fēng)險(xiǎn)。

所以這里的一個(gè)決絕方案就是自動(dòng)讀取枚舉類的數(shù)據(jù)并更新到數(shù)據(jù)庫(kù),本文只講枚舉類數(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();

? ? /**
? ? ?* 通過(guò)代碼獲取名稱
? ? ?*/
? ? 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("入庫(kù)單狀態(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("出庫(kù)單訂單狀態(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=入庫(kù)單狀態(tài),code=100,name=新建,remark=null
typeCode=InbStatus,typeName=入庫(kù)單狀態(tài),code=260,name=碼盤完成,remark=null
typeCode=InbStatus,typeName=入庫(kù)單狀態(tài),code=300,name=部分完成,remark=null
typeCode=InbStatus,typeName=入庫(kù)單狀態(tài),code=310,name=全部完成,remark=null
typeCode=InbStatus,typeName=入庫(kù)單狀態(tài),code=950,name=關(guān)閉,remark=null
typeCode=InbStatus,typeName=入庫(kù)單狀態(tài),code=999,name=取消,remark=null
typeCode=OubStatus,typeName=出庫(kù)單訂單狀態(tài),code=300,name=全部分配,remark=null
typeCode=OubStatus,typeName=出庫(kù)單訂單狀態(tài),code=320,name=部分揀貨,remark=null

然后,你就可以將這些數(shù)據(jù)同步到數(shù)據(jù)庫(kù)啦

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬(wàn)能解決辦法

    java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬(wàn)能解決辦法

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

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

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

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

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

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

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

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

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

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

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

    Java基礎(chǔ)之Maven詳解

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

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

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

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

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

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

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

最新評(píng)論

乾安县| 开远市| 上蔡县| 定西市| 太仆寺旗| 房产| 临邑县| 三都| 南康市| 新乡市| 大足县| 勃利县| 文山县| 德钦县| 博罗县| 剑川县| 洛南县| 济宁市| 明水县| 高邑县| 保定市| 慈溪市| 托克托县| 通河县| 大足县| 高唐县| 曲沃县| 吉隆县| 建瓯市| 稷山县| 长治县| 江北区| 长寿区| 密云县| 德钦县| 万安县| 日喀则市| 胶南市| 山阳县| 芜湖市| 水富县|