设计理念:
1、首先,以Map格式初始化词典表的数据。 key格式为type '@' code,value值为通常的回波值。 例如,' ta_sex' '@' '1','男人'
2、编写DictAcc评论。 代码表示数据库字段还是需要回显的标识,是ta_sex等type存储类型
@target(elementtype.field ) )
@ retention (retention policy.runtime ) )。
公共@接口dictacc {
公共字符串代码(;
公共字符串类型(;
}
3 .创建BV类。 代码表示数据库存储的名称,name表示需要回显的名称
公共类bv {
publicfinalstaticstringcode=' code ';
publicfinalstaticstringname=' name ';
}
pojo创建字段sex,sex_n创建表示回显的字段。 _n必须支持
公共类用户{
@dictacc(code=bv.code,type='ta_orgType ' )
公共字符串org类型='1';
@dictacc(code=bv.name,type='ta_orgType ' )
公共字符串org type _ n;
@ dictacc (代码=bv.code,type='ta_sex ' ) )。
公共字符串sex='1';
@ dictacc (代码=bv.name,type='ta_sex ' ) )。
公共字符串sex _ n;
公共字符串获取类型
返回类型;
}
publicvoidsetorgtype (字符串类型) {
this.orgType=orgType;
}
公共字符串getorgtype _ n
return orgType_n;
}
publicvoidsetorgtype _ n (stringorgtype _ n ) {
this.orgType_n=orgType_n;
}
公共字符串获取sex
返回性;
}
publicvoidsetsex(stringsex ) {
this.sex=sex;
}
公共字符串get sex _ n
return sex_n;
}
publicvoidsetsex_n(stringsex_n ) {
this.sex_n=sex_n;
}
}
5 .代码如下: map相当于从数据库中检索到的数据库词典表
公共类管理{
publicstaticvoidmain (字符串[ ] 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','女');
用户用户=new user (;
用户用户1=new user (;
列表用户列表=new ArrayList (;
Userlist.add(user;
Userlist.add(user1;
doaction.fill(userlist,map );
for(intI=0; i userList.size (; I ) {
system.out.println (userlist.get (0).getOrgType_n );
}
}
}
6 .利用反射完成向sex_n等赋值
公共类待办事项{
publicstaticvoidfill (列表,散列映射目录映射) )。
throws IllegalArgumentException,IllegalAccessException {
map map=getdictmap (POJO list.get (0) );
mapcodemap=map.get (代码图);
映射名称映射=映射. get (名称映射);
for(tpojo:pojolist ) {
fillname(Pojo、codeMap、nameMap、dictmap );
}
}
//*
获取带有DictACC词典注释的字段,并将其连接到Map
* @param t
* @return
*/
publicstaticmapgetdictmap{
//由词典代码值构成的Map
Map codeMap=new HashMap (;
需要从代码值转换为由值构成的映射
Map nameMap=new HashMap (;
列表字段列表=new ArrayList (;
//获取父类的所有字段
Fieldlist.addall(Arrays.aslist ) t.getclass(.getsuperclass ).getDeclaredFields ) );
//获取此类的所有字段
field list.addall (arrays.as list (t.getclass (.getdeclaredfields ) );
现场现场:现场列表(for ) {
if (field.get annotation (dictacc.class )!=空) {
dictaccann=field.get annotation (dictacc.class );
if(ann.code ().equals () bv.code () ) )。
codemap.put(Ann.type () ' @' field.getName )、field );
}elseif(Ann.code ).equals (bv.name ) )。
namemap.put(ann.type () ' @' field.getName )、field );
}
}
}
Map map=new HashMap (;
map.put ('代码映射',代码映射);
map.put('nameMap ',namemap );
返回图;
}
公共服务语音文件名(TT,映射代码映射,
Map nameMap,HashMap dictmap )
throws IllegalArgumentException,IllegalAccessException {
for (输入项: name map.entryset ) ) }
获取代码密钥
String codeKey=entry.getKey ().substring(0) ) 0、
entry.getKey ().length (-2 );
if (代码映射. get (代码密钥)!=空) {
field field=code map.get (代码密钥);
获取代码的值
string value=string.value of (field.get (t );
stringtargetkey=code key.split (' @ ' ) [0] '@' value;
if (dict map.contains key (target key ) ) )。
//从词典表中取得那么应该保存的值
stringtargetvalue=dict map.get (目标密钥);
fieldtargetfield=entry.getvalue (;
targetfield.setaccessible(true;
为name赋值
targetfield.set(t,targetValue;
}
}
}
}
}