本文实例为大家分享了Java同步枚举类数据到数据库的具体实现代码,供大家参考,具体内容如下
1.需求说明:
我们在开发中常常会用到数据字典,后端程序中也会经常用到(一般是用枚举类来存储),然而我们数据库中也会维护一个数据字典的数据,便于前端做数据显示时的处理,有一个问题就是,如果字典项发生变化后,我们需要修改枚举类和数据库的字典数据,要修改两次,还要面临二者不一致的风险。
所以这里的一个决绝方案就是自动读取枚举类的数据并更新到数据库,本文只讲枚举类数据的提取。
2.首先创建一个描述枚举类型的注解:
package com.visy.enums2dict.annotations;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnumDesc {
String value();
}
3.创建一个枚举类接口,以规范枚举类
package com.visy.enums2dict.interfaces;
public interface EnumInterface {
String getCode();
String getName();
String getRemark();
String getNameByCode(String code);
}
4.创建保存枚举数据的实体
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.提取枚举数据的核心类
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;
}
//获取指定枚举类的全部数据
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;
}
//获取指定包下所有枚举配置数据
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.准备两个枚举类(需实现2中的接口和1的注解)
package com.visy.enums2dict.enums;
import com.visy.enums2dict.annotations.EnumDesc;
import com.visy.enums2dict.interfaces.EnumInterface;
@EnumDesc("入库单状态")
public enum InbStatus implements EnumInterface {
CREATE("100","新建"),
PALLET_FINISH("260","码盘完成"),
PART_FINISH("300", "部分完成"),
FULL_FINISH("310","全部完成"),
CLOSE("950", "关闭"),
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("出库单订单状态")
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.输出结果:
typeCode=InbStatus,typeName=入库单状态,code=100,name=新建,remark=null
typeCode=InbStatus,typeName=入库单状态,code=260,name=码盘完成,remark=null
typeCode=InbStatus,typeName=入库单状态,code=300,name=部分完成,remark=null
typeCode=InbStatus,typeName=入库单状态,code=310,name=全部完成,remark=null
typeCode=InbStatus,typeName=入库单状态,code=950,name=关闭,remark=null
typeCode=InbStatus,typeName=入库单状态,code=999,name=取消,remark=null
typeCode=OubStatus,typeName=出库单订单状态,code=300,name=全部分配,remark=null
typeCode=OubStatus,typeName=出库单订单状态,code=320,name=部分拣货,remark=null
然后,你就可以将这些数据同步到数据库啦
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。