有时候写代码,经常赋值,做JavaBean转换;字段不一致的没法复制字段,要手工写set方法;自己做了一个生成set赋值的工具。
效果:
CartParam cartparam= new CartParam();
cartparam.setCartId(11111L);
cartparam.setExpandAttr("11111");
cartparam.setGoodsId(11111L);
cartparam.setIds("11111");
cartparam.setInviteMemberId(11111L);
cartparam.setIsSelected(11111);
cartparam.setMemberId(11111L);
cartparam.setOrderType(11111);
cartparam.setSkuId(11111L);
cartparam.setTotal(11111);
cartparam.setType(11111);
CartParam cartparam= new CartParam();
cartparam.getCartId();
cartparam.getClass();
cartparam.getExpandAttr();
cartparam.getGoodsId();
cartparam.getIds();
cartparam.getInviteMemberId();
cartparam.getIsSelected();
cartparam.getMemberId();
cartparam.getOrderType();
cartparam.getSkuId();
cartparam.getTotal();
cartparam.getType();
工具类:
package test;
import com.zscat.mallplus.vo.CartParam;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* <p>Title: 生成常用set赋值语句:set、get方法</p>
* <p>Description: </p>
* @author 清风笑Samson
@author 张代浩
* @version 1.0
*/
public class MyBeanUtils
extends PropertyUtilsBean {
public static void main(String[] args) {
CartParam bean = new CartParam();
HashMap<String,Object> map = new HashMap<String,Object>();
String resultSetStr = MyBeanUtils.copyBean2SetString(bean);
String resultGetStr = MyBeanUtils.copyBean2GetString(bean);
System.out.println(resultSetStr);
System.out.println(resultGetStr);
}
private static void convert(Object dest, Object orig) throws
IllegalAccessException, InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException
("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty origDescriptors[] =
( (DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (DynaBean) orig).get(name);
try {
getInstance().setSimpleProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else if (orig instanceof Map) {
Iterator names = ( (Map) orig).keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (Map) orig).get(name);
try {
getInstance().setSimpleProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else
/* if (orig is a standard JavaBean) */
{
PropertyDescriptor origDescriptors[] =
PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) &&
PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(orig, name);
getInstance().setSimpleProperty(dest, name, value);
}
catch (IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
}
/**
* 对象拷贝
* 数据对象空值不拷贝到目标对象
*
* @throws NoSuchMethodException
* copy
*/
public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
{
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name) &&PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if(value!=null){
getInstance().setSimpleProperty(tobean, name, value);
}
}
catch (IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
/**
* 把orig和dest相同属性的value复制到dest中
* @param dest
* @param orig
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyBean2Bean(Object dest, Object orig) throws Exception {
convert(dest, orig);
}
public static String copyBean2SetString(Object bean){
System.out.println(bean.getClass().getSimpleName());
String className = bean.getClass().getSimpleName();
System.out.println(bean.getClass().getSimpleName().toLowerCase()); //小写
String beanName = bean.getClass().getSimpleName().toLowerCase();
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
StringBuffer sb = new StringBuffer();
sb.append(className +" "+beanName +"= new "+className+"();\n");
for (int i =0;i<pds.length;i++)
{
try {
PropertyDescriptor pd = pds[i];
String propname = pd.getName();
if (pd.getPropertyType() ==String.class){
sb.append(beanName+".set"+captureName(propname)+"(\"11111\");\n");
}
else if (pd.getPropertyType() ==Integer.class){
sb.append(beanName+".set"+captureName(propname)+"(11111);\n");
}
else if (pd.getPropertyType() ==Long.class){
sb.append(beanName+".set"+captureName(propname)+"(11111L);\n");
}
else if (pd.getPropertyType() ==Boolean.class){
sb.append(beanName+".set"+captureName(propname)+"(true);\n");
}
else if (pd.getPropertyType() == Date.class){
sb.append(beanName+".set"+captureName(propname)+"(Calendar.getInstance().getTime());\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static String copyBean2GetString(Object bean){
System.out.println(bean.getClass().getSimpleName());
String className = bean.getClass().getSimpleName();
System.out.println(bean.getClass().getSimpleName().toLowerCase()); //小写
String beanName = bean.getClass().getSimpleName().toLowerCase();
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
StringBuffer sb = new StringBuffer();
sb.append(className +" "+beanName +"= new "+className+"();\n");
for (int i =0;i<pds.length;i++)
{
try {
PropertyDescriptor pd = pds[i];
String propname = pd.getName();
sb.append(beanName+".get"+captureName(propname)+"();\n");
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
//首字母大写
public static String captureName(String name) {
// name = name.substring(0, 1).toUpperCase() + name.substring(1);
// return name;
char[] cs=name.toCharArray();
cs[0]-=32;
return String.valueOf(cs);
}
public static void copyBean2Map(Map map, Object bean){
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
for (int i =0;i<pds.length;i++)
{
PropertyDescriptor pd = pds[i];
String propname = pd.getName();
try {
Object propvalue = PropertyUtils.getSimpleProperty(bean,propname);
map.put(propname, propvalue);
} catch (IllegalAccessException e) {
//e.printStackTrace();
} catch (InvocationTargetException e) {
//e.printStackTrace();
} catch (NoSuchMethodException e) {
//e.printStackTrace();
}
}
}
/**
* 将Map内的key与Bean中属性相同的内容复制到BEAN中
* @param bean Object
* @param properties Map
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyMap2Bean(Object bean, Map properties) throws
IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ( (bean == null) || (properties == null)) {
return;
}
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
// Identify the property name and value(s) to be assigned
if (name == null) {
continue;
}
Object value = properties.get(name);
try {
Class clazz = PropertyUtils.getPropertyType(bean, name);
if (null == clazz) {
continue;
}
String className = clazz.getName();
if (className.equalsIgnoreCase("java.sql.Timestamp")) {
if (value == null || value.equals("")) {
continue;
}
}
getInstance().setSimpleProperty(bean, name, value);
}
catch (NoSuchMethodException e) {
continue;
}
}
}
/**
* 自动转Map key值大写
* 将Map内的key与Bean中属性相同的内容复制到BEAN中
* @param bean Object
* @param properties Map
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyMap2Bean_Nobig(Object bean, Map properties) throws
IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ( (bean == null) || (properties == null)) {
return;
}
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
// Identify the property name and value(s) to be assigned
if (name == null) {
continue;
}
Object value = properties.get(name);
// 命名应该大小写应该敏感(否则取不到对象的属性)
//name = name.toLowerCase();
try {
if (value == null) { // 不光Date类型,好多类型在null时会出错
continue; // 如果为null不用设 (对象如果有特殊初始值也可以保留?)
}
Class clazz = PropertyUtils.getPropertyType(bean, name);
if (null == clazz) { // 在bean中这个属性不存在
continue;
}
String className = clazz.getName();
// 临时对策(如果不处理默认的类型转换时会出错)
if (className.equalsIgnoreCase("java.util.Date")) {
value = new Date(((java.sql.Timestamp)value).getTime());// wait to do:貌似有时区问题, 待进一步确认
}
// if (className.equalsIgnoreCase("java.sql.Timestamp")) {
// if (value == null || value.equals("")) {
// continue;
// }
// }
getInstance().setSimpleProperty(bean, name, value);
}
catch (NoSuchMethodException e) {
continue;
}
}
}
/**
* Map内的key与Bean中属性相同的内容复制到BEAN中
* 对于存在空值的取默认值
* @param bean Object
* @param properties Map
* @param defaultValue String
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyMap2Bean(Object bean, Map properties, String defaultValue) throws
IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ( (bean == null) || (properties == null)) {
return;
}
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
// Identify the property name and value(s) to be assigned
if (name == null) {
continue;
}
Object value = properties.get(name);
try {
Class clazz = PropertyUtils.getPropertyType(bean, name);
if (null == clazz) {
continue;
}
String className = clazz.getName();
if (className.equalsIgnoreCase("java.sql.Timestamp")) {
if (value == null || value.equals("")) {
continue;
}
}
if (className.equalsIgnoreCase("java.lang.String")) {
if (value == null) {
value = defaultValue;
}
}
getInstance().setSimpleProperty(bean, name, value);
}
catch (NoSuchMethodException e) {
continue;
}
}
}
public MyBeanUtils() {
super();
}
}