第一次写,很紧张,值得纪念的日子 2016-04-01(竟然是愚人节,我是个小白)
首先,这个是在项目里的一个功能,因为之前项目里都是写死的值,每个人写的时候都写一堆一堆的代码,一堆一堆的if,else.所以就自己写了个公共的方式,今天开始用,写不太好,望大神们给点指导建议。
话不多说,上正文。
需求背景:
在系统中需要做用户与统一用户系统同步的功能,对于待更新和删除操作的用户,不能直接同步,需要由业务人员操作系统同步用户和组织。最最主要的是,同步时可能不同步所有字段,感觉这个需求停尴尬的,但是业务上需要这样,所以默认了。
解决方案:
页面在选中待更新项目后,弹出dialog,选择要更新的字段。传到后台,向正式表中更新选中的字段。
公共类:
public class DesignUtils<D> {
private final static String SET_HEAD = "set";
private final static String GET_HEAD = "get";
public D get(D newDomain,D originDomain, String ids[]) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//创建class对象
Class<? extends Object> clazzNew = newDomain.getClass();
// 字段数组
Field[] fields = clazzNew.getDeclaredFields();
for(int i = 0; i < ids.length; i++){
for(int j = 0; j < fields.length; j++){
if(fields[j].getName().equals(ids[i])){
getMethod(clazzNew, SET_HEAD, fields[j]).invoke(newDomain, new Object[]{getMethod(clazzNew, GET_HEAD, fields[j]).invoke(originDomain, new Object[]{})});
}
}
}
return newDomain;
}
/**
* 获取实体对应的get和set方法
* @param clazzOrigin
* @param methodHead
* @param fieldName
* @return
*/
public Method getMethod(Class<? extends Object> clazz,String methodHead, Field field){
if(field.getName() == null || "".equals(field.getName())){
return null;
}
Method method = null;
try {
if(methodHead.equals(GET_HEAD)){
method = clazz.getMethod(getMethodName(methodHead, field.getName()), new Class[]{});
}else if(methodHead.equals(SET_HEAD)){
method = clazz.getMethod(getMethodName(methodHead, field.getName()), new Class[]{field.getType()});
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return method;
}
/**
* 获取get或者set方法名
* @param methodHead
* @param fieldName
* @return
*/
public String getMethodName(String methodHead, String fieldName){
return methodHead + getFirstLetterStr(fieldName);
}
/**
* 将字符串首字母转换成大写
* @param str
* @return
*/
public String getFirstLetterStr(String str){
return getFirstLetter(str) + str.substring(1);
}
/**
* 获取字符串第一个字母,并且转换成大写
* @param str
* @return
*/
public String getFirstLetter(String str){
return str.substring(0, 1).toUpperCase();
}
}
user工具类(感觉可有可无):
import java.lang.reflect.InvocationTargetException;
public class UserDesign extends DesignUtils<User>{
public User getNewUser(User newUser, User originUser, String[] ids) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
return get(newUser, originUser, ids);
}
}
depart工具类(同上):
import java.lang.reflect.InvocationTargetException;
public class DepartDesign extends DesignUtils<Depart>{
public Depart getNewDepart(Depart newDepart, Depart originDepart, String[] ids) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
return get(newDepart, originDepart, ids);
}
}
user实体:
public class User {
private String userId;
private String userName;
private String userAge;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
}
public class Depart {
private String departId;
private String departName;
private String departParent;
public String getDepartId() {
return departId;
}
public void setDepartId(String departId) {
this.departId = departId;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public String getDepartParent() {
return departParent;
}
public void setDepartParent(String departParent) {
this.departParent = departParent;
}
}
Test类:
import java.lang.reflect.InvocationTargetException;
public class Test extends DesignUtils<User>{
public static void main(String[] args) {
/**
* 用户类 User
*/
UserDesign ud = new UserDesign();
//DesignUtils<User> user = new DesignUtils<User>();
//根据前台选定的用户信息(临时表)
User originUser = new User();
originUser.setUserId("1001");;
originUser.setUserName("张三");
originUser.setUserAge("20");
//通过前台传递过来(需要向更新的字段)
String[] idsUser = {"userName"};
//需要插入正是表的实体类
User newUser = new User();
try {
//user.get(newUser, originUser, idsUser);
//user.getNewUser(newUser, originUser, idsUser);
ud.get(newUser, originUser, idsUser);
System.out.println(newUser.getUserId());
System.out.println(newUser.getUserName());
System.out.println(newUser.getUserAge());
//TODO
//对正是表进行操作
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//=============================================
/**
* 部门Depart 与人员同理,只是方式不同
*/
//DepartDesign dd = new DepartDesign();
DesignUtils<Depart> departUtil = new DesignUtils<Depart>();
Depart originDepart = new Depart();
originDepart.setDepartId("000201");
originDepart.setDepartName("部门一");
originDepart.setDepartParent("000000");
Depart newDepart = new Depart();
String[] idsDepart = {"departId", "departName"};
try {
//dd.getNewDepart(newDepart, originDepart, idsDepart);
//dd.get(newDepart, originDepart, idsDepart);
departUtil.get(newDepart, originDepart, idsDepart);
System.out.println(newDepart.getDepartId());
System.out.println(newDepart.getDepartName());
System.out.println(newDepart.getDepartParent());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}