📄 staticmethod.java
字号:
package com.cnpoint.myspaces.common.util;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class StaticMethod {
static String CHARSET_DB = "utf-8";
static String CHARSET_PAGE = "utf-8";
public static String dbNull2String(String s)
{
String reStr;
reStr = "";
reStr = s != null ? s : "";
reStr = reStr.trim();
if(CHARSET_PAGE.equals(CHARSET_DB))
return reStr;
try {
reStr = new String(reStr.getBytes(CHARSET_DB), CHARSET_PAGE);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reStr;
}
public static String nullObject2String(Object s)
{
String str = "";
try{
str = s.toString();
}catch(Exception e){
str="";
}
return str;
}
public static void populate(Object bean, Map properties){
if(bean == null || properties == null)
return;
for(Iterator names = properties.keySet().iterator(); names.hasNext();)
{
String name = (String)names.next();
if(name != null)
{
Object value = properties.get(name);
try {
BeanUtils.setProperty(bean, name, StaticMethod.dbNull2String(StaticMethod.nullObject2String(value)));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 名称:copyPropertys
* 功能:将o2的属性值拷贝到01中
* 说明:实现从o2到o1的属性拷贝,字段名相同可以不区分大小写,但要具有相同的类型
* 输入参数:Object o1,Object o2
* 返回参数:viod
*/
public static void copyPropertys(Object o1, Object o2){
if (o1 == null)
throw new IllegalArgumentException("No destination bean specified");
if (o2 == null)
throw new IllegalArgumentException("No origin bean specified");
//获得o1的属性列表
Field[] fieldsO1 = o1.getClass().getDeclaredFields();
//获得02的属性列表
Field[] fieldsO2 = o2.getClass().getDeclaredFields();
try {
for (int i = 0; i < fieldsO2.length; i++) {
for (int j = 0; j < fieldsO1.length; j++) {
//循环判断o1的属性和02的属性名称是否一致
if (fieldsO2[i].getName().equalsIgnoreCase(fieldsO1[j].getName())) {
//调用o1的setName方法设置属性值
BeanUtils.setProperty(o1, fieldsO1[j].getName(),
BeanUtils.getProperty(o2, fieldsO2[i].getName()));
break;
}
}
}
} catch (Exception ex) {
try {
throw ex;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -