欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

1.需求說(shuō)明:

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

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

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

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è)枚舉類(lèi)接口,以規(guī)范枚舉類(lèi)

package com.visy.enums2dict.interfaces;

public interface EnumInterface {

? ? String getCode();

? ? String getName();

? ? String getRemark();

? ? /**
? ? ?* 通過(guò)代碼獲取名稱(chēng)
? ? ?*/
? ? 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ù)的核心類(lèi)

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 {

? ? //獲取指定包下的所有類(lèi)路徑
? ? private static List<String> getClassesByPackage(String packagePath) {
? ? ? ? //獲取包的文件路徑
? ? ? ? String basePath = ClassLoader.getSystemResource("").getPath();
? ? ? ? String filePath = basePath + packagePath.replace(".", "/");

? ? ? ? //獲取包下所有類(lèi)路徑
? ? ? ? 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));
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? //從文件路徑提取類(lèi)路徑
? ? 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;
? ? }

? ? //獲取指定枚舉類(lèi)的全部數(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è)枚舉類(lèi)(需實(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","碼盤(pán)完成"),
? ? 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=碼盤(pán)完成,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)文章

  • 如何寫(xiě)好一個(gè)Spring組件的實(shí)現(xiàn)步驟

    如何寫(xiě)好一個(gè)Spring組件的實(shí)現(xiàn)步驟

    這篇文章主要介紹了如何寫(xiě)好一個(gè)Spring組件的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java獲取漢字對(duì)應(yīng)的拼音(全拼或首字母)

    Java獲取漢字對(duì)應(yīng)的拼音(全拼或首字母)

    這篇文章主要介紹了Java如何獲取漢字對(duì)應(yīng)的拼音(全拼或首字母),文中實(shí)現(xiàn)的方法是引用了pinyin4j-2.5.0.jar,然后給出了完整的示例代碼,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • java實(shí)現(xiàn)優(yōu)酷視頻地址解析示例代碼分享

    java實(shí)現(xiàn)優(yōu)酷視頻地址解析示例代碼分享

    最近做了一個(gè)在線(xiàn)視頻的下載器,需要解析youku的視頻,獲得真正的視頻地址,現(xiàn)在把解析過(guò)程記錄下來(lái)以供參考
    2014-01-01
  • MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用

    MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用

    動(dòng)態(tài)?SQL?是?MyBatis?的強(qiáng)大特性之一。如果你使用過(guò)?JDBC?或其它類(lèi)似的框架,你應(yīng)該能理解根據(jù)不同條件拼接?SQL?語(yǔ)句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(dòng)態(tài)?SQL,可以徹底擺脫這種痛苦
    2022-04-04
  • Java文件管理操作的知識(shí)點(diǎn)整理

    Java文件管理操作的知識(shí)點(diǎn)整理

    這篇文章主要為大家詳細(xì)介紹了Java中文件管理操作的一些知識(shí)點(diǎn)和實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-09-09
  • Java計(jì)算兩個(gè)時(shí)間段的差的實(shí)例詳解

    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)求的幾種方式(附源碼)

    本文主要介紹了java構(gòu)造http請(qǐng)求的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Spring Security登陸流程講解

    Spring Security登陸流程講解

    本文主要介紹了Spring Security登陸流程講解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • SpringBoot開(kāi)發(fā)技巧之如何處理跨域請(qǐng)求CORS

    SpringBoot開(kāi)發(fā)技巧之如何處理跨域請(qǐng)求CORS

    CORS(Cross-Origin Resource Sharing)"跨域資源共享",是一個(gè)W3C標(biāo)準(zhǔn),它允許瀏覽器向跨域服務(wù)器發(fā)送Ajax請(qǐng)求,打破了Ajax只能訪(fǎng)問(wèn)本站內(nèi)的資源限制
    2021-10-10
  • java枚舉是如何保證線(xiàn)程安全的

    java枚舉是如何保證線(xiàn)程安全的

    這篇文章主要介紹了java枚舉是如何保證線(xiàn)程安全的。Java SE5提供了一種新的類(lèi)型-Java的枚舉類(lèi)型,關(guān)鍵字enum可以將一組具名的值的有限集合創(chuàng)建為一種新的類(lèi)型,而這些具名的值可以作為常規(guī)的程序組件使用,這是一種非常有用的功能。,需要的朋友可以參考下
    2019-06-06

最新評(píng)論