当前位置: 首页>編程日記>正文

java获取字典表数据,利用注解完成字典表数据对应

java获取字典表数据,利用注解完成字典表数据对应

设计思路:

1、首先将字典表的数据以Map的形式进行初始化,key格式为type+"@"+code,value值为正常回显的值,如"ta_sex"+"@"+"1", "男"

2、建立DictAcc注解,code代表是数据库字段还是需要回显的标识,type存储类型如ta_sex

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface DictACC {

public String code();

public String type();

}

3.建立一个类BV,code表示数据库存储的名字,name表示需要回显的名字

public class BV {

public final static String code="code";

public final static String name="name";

}

4.在pojo中字段sex,再建立sex_n表示要回显的字段(_n必须对应)

public class User {

@DictACC(code = BV.code, type = "ta_orgType")

public String orgType="1";

@DictACC(code = BV.name, type = "ta_orgType")

public String orgType_n;

@DictACC(code = BV.code, type = "ta_sex")

public String sex="1";

@DictACC(code = BV.name, type = "ta_sex")

public String sex_n;

public String getOrgType() {

return orgType;

}

public void setOrgType(String orgType) {

this.orgType = orgType;

}

public String getOrgType_n() {

return orgType_n;

}

public void setOrgType_n(String orgType_n) {

this.orgType_n = orgType_n;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getSex_n() {

return sex_n;

}

public void setSex_n(String sex_n) {

this.sex_n = sex_n;

}

}

5.代码如下:map相当与数据库中取出来的数据库字典表

public class MainTest {

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {

HashMap map=new HashMap();

map.put("ta_orgType"+"@"+"1", "部门1");

map.put("ta_orgType"+"@"+"2", "部门2");

map.put("ta_orgType"+"@"+"3", "部门3");

map.put("ta_sex"+"@"+"1", "男");

map.put("ta_sex"+"@"+"2", "女");

User user=new User();

User user1=new User();

List userList=new ArrayList();

userList.add(user);

userList.add(user1);

Doaction.fill(userList,map);

for (int i = 0; i < userList.size(); i++) {

System.out.println(userList.get(0).getOrgType_n());

}

}

}

6.利用反射完成对sex_n等赋值

public class Doaction {

public static void fill(List pojoList, HashMap dictmap)

throws IllegalArgumentException, IllegalAccessException {

Map> map = getDictMap(pojoList.get(0));

Map codeMap = map.get("codeMap");

Map nameMap = map.get("nameMap");

for (T pojo:pojoList) {

fillname(pojo, codeMap, nameMap, dictmap);

}

}

/**

* 获取带DictACC字典注解的字段,并拼接成Map

* @param t

* @return

*/

public static Map> getDictMap(T t) {

//字典code值组成的Map

Map codeMap = new HashMap();

//需要从code值转成value组成的Map

Map nameMap = new HashMap();

List fieldlist =new ArrayList();

//获取父类的所有field

fieldlist.addAll(Arrays.asList(t.getClass().getSuperclass().getDeclaredFields()));

//获取本类的所有field

fieldlist.addAll(Arrays.asList(t.getClass().getDeclaredFields()));

for (Field field : fieldlist) {

if (field.getAnnotation(DictACC.class) != null) {

DictACC ann = field.getAnnotation(DictACC.class);

if (ann.code().equals(BV.code)) {

codeMap.put(ann.type() + "@" + field.getName(), field);

} else if (ann.code().equals(BV.name)) {

nameMap.put(ann.type() + "@" + field.getName(), field);

}

}

}

Map> map = new HashMap>();

map.put("codeMap", codeMap);

map.put("nameMap", nameMap);

return map;

}

public static void fillname(T t, Map codeMap,

Map nameMap, HashMap dictmap)

throws IllegalArgumentException, IllegalAccessException {

for (Entry entry : nameMap.entrySet()) {

//获取codekey

String codeKey = entry.getKey().substring(0,

entry.getKey().length() - 2);

if (codeMap.get(codeKey) != null) {

Field field = codeMap.get(codeKey);

//获取code的值

String value = String.valueOf(field.get(t));

String targetKey = codeKey.split("@")[0] + "@" + value;

if (dictmap.containsKey(targetKey)) {

//从字典表获取那么中应该存的值

String targetValue = dictmap.get(targetKey);

Field targetField = entry.getValue();

targetField.setAccessible(true);

//给name赋值

targetField.set(t, targetValue);

}

}

}

}

}


https://www.fengoutiyan.com/post/15714.html

相关文章:

  • java 注解开发
  • java基本类型强制转换
  • java获取数据库表字段
  • java获取注解的值
  • java获取表单提交的数据
  • java参数注解
  • java自定义注解调用方法
  • java获取字段名称
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機