java中给实体对象属性的空值赋默认值

网友投稿 2441 2022-08-24


java中给实体对象属性的空值赋默认值

目录给实体对象属性的空值赋默认值给实体类赋默认值通用方法

给实体对象属性的空值赋默认值

private final String defaultStr = "";

private final Date defaultDate = new Date();

private final BigDecimal defaultDecimal = new BigDecimal(0);

private final Timestamp defaultTimestamp=new Timestamp(new Date().getTime());

//赋默认值

public void setDefaultValue(Object object) {

try {

Class clazz = object.getClass();

Field[] fields = clazz.getDeclaredFields();

String primaryKey = EntityUtil.getPrimaryKey(currentSession(), object.getClass());

for(int i=0;i

Field field = fields[i];

String fieldName = field.getName();

Class fieldClass=field.getType();

field.setAccessible(true); //设置访问权限

if(!fieldName.equals(primaryKey) && isFieldValueNull(fieldName,object)){

if (fieldClass == Integer.class ) {

field.set(object, defaultDecimal.intValue()http://);

}else if (fieldClass == Long.class) {

field.set(object, defaultDecimal.longValue());

}else if (fieldClass == Float.class) {

field.set(object, defaultDecimal.doubleValue());

}else if (fieldClass == BigDecimal.class) {

field.set(object, defaultDecimal);

} else if (fieldClass == Date.class) {

field.set(object, defaultDate);

} else if (fieldClass == String.class){

field.set(object, defaultStr); // 设置值

} else if (fieldClass == Timestamp.class){

field.set(object, defaultTimestamp);

}

}else if(fieldName.equals(primaryKey) && isStringFieldValueNull(fieldName,object,fieldClass)){//mysql,需要对对主键做特殊处理

field.set(object, null);

}

}

} catch (Exception e) {

e.printStackTrace();

System.out.println(e.getMessage());

}

}

//判断字段是否为空

privateLdxRfo boolean isFieldValueNull(String fieldName, Object object) throws ClassNotFoundException {

boolean isNUll=false;

try {

String firstLetter = fieldName.substring(0, 1).toUpperCase();

String getter = "get" + firstLetter + fieldName.substring(1);

Method method = object.getClass().getMethod(getter, new Class[] {});

Object value = method.invoke(object, new Object[] {});

if(value==null){

isNUll=true;

}

return isNUll;

} catch (Exception e) {

return isNUll;

}

}

//判断主键是否为空值

private boolean isStringFieldValueNull(String fieldName, Object object, Class fieldClass) throws ClassNotFoundException {

boolean isNUll=false;

try {

String firstLetter = fieldName.substring(0, 1).toUpperCase();

String getter = "get" + firstLetter + fieldName.substring(1);

Method method = object.getClass().getMethod(getter, new Class[] {});

Object value = method.invoke(object, new Object[] {});

if(value==null ){

isNUll=true;

}else{

if (fieldClass == String.class && StringUtils.isBlank((String)value)) {

isNUll=true;

}

}

return isNUll;

} catch (Exception e) {

return isNUll;

}

}

给实体类赋默认值通用方法

package com.clamc.common.util;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.math.BigDecimal;

import java.sql.Date;

import java.sql.Timestamp;

import java.util.ArrayList;

import java.util.List;

/**

* yangzhiwei

* 使用反射给实体类k赋值(默认值)

* insert update会报null异常,为空时不能插入和更新

*/

public class ObjInvoke {

public static Object getObjDefault(Object obj) {

// 得到类对象

Class objCla = obj.getClass();

Field[] fs = objCla.getDeclaredFields();

for (int i = 0; i < fs.length; i++) {

Field f = fs[i];

// 设置些属性是可以访问的

boolean isStatic = Modifier.isStatic(f.getModifiers());

if(isStatic){

continue;

}

// 设置些属性是可以访问的

f.setAccessible(true);

try {

// 得到此属性的值

Object val = f.get(obj);

// 得到此属性的类型

String type = f.getType().toString();

if (type.endsWith("String") && val == null) {

// 给属性设值

f.set(obj, "");

} else if ((type.endsWith("int") || type.endsWith("Integer") || type.endsWith("double")) && val == null) {

f.set(obj, 0);

}else if ((type.endsWith("long")|| type.endsWith("Long") )&& val == null){

f.set(obj, 0L);

} else if (type.endsWith("Date") && val == null) {

f.set(obj, Date.valueOf("1970-01-01"));

}else if(type.endsWith("Timestamp") && val == null){

f.set(obj, Timestamp.valueOf("1970-01-01 00:00:00"));

} else if (type.endsWith("BigDecimal") && val == null) {

f.set(obj, new BigDecimal(0));

}

} catch (Exception e) {

e.printStackTrace();

}

}

return obj;

}

public static List getObjDefaultList(List objList) {

Listhttp:// list=new ArrayList();

for(Object t:objList){

list.add(getObjDefault(t));

}

return list;

}

}

Field field = fields[i];

String fieldName = field.getName();

Class fieldClass=field.getType();

field.setAccessible(true); //设置访问权限

if(!fieldName.equals(primaryKey) && isFieldValueNull(fieldName,object)){

if (fieldClass == Integer.class ) {

field.set(object, defaultDecimal.intValue()http://);

}else if (fieldClass == Long.class) {

field.set(object, defaultDecimal.longValue());

}else if (fieldClass == Float.class) {

field.set(object, defaultDecimal.doubleValue());

}else if (fieldClass == BigDecimal.class) {

field.set(object, defaultDecimal);

} else if (fieldClass == Date.class) {

field.set(object, defaultDate);

} else if (fieldClass == String.class){

field.set(object, defaultStr); // 设置值

} else if (fieldClass == Timestamp.class){

field.set(object, defaultTimestamp);

}

}else if(fieldName.equals(primaryKey) && isStringFieldValueNull(fieldName,object,fieldClass)){//mysql,需要对对主键做特殊处理

field.set(object, null);

}

}

} catch (Exception e) {

e.printStackTrace();

System.out.println(e.getMessage());

}

}

//判断字段是否为空

privateLdxRfo boolean isFieldValueNull(String fieldName, Object object) throws ClassNotFoundException {

boolean isNUll=false;

try {

String firstLetter = fieldName.substring(0, 1).toUpperCase();

String getter = "get" + firstLetter + fieldName.substring(1);

Method method = object.getClass().getMethod(getter, new Class[] {});

Object value = method.invoke(object, new Object[] {});

if(value==null){

isNUll=true;

}

return isNUll;

} catch (Exception e) {

return isNUll;

}

}

//判断主键是否为空值

private boolean isStringFieldValueNull(String fieldName, Object object, Class fieldClass) throws ClassNotFoundException {

boolean isNUll=false;

try {

String firstLetter = fieldName.substring(0, 1).toUpperCase();

String getter = "get" + firstLetter + fieldName.substring(1);

Method method = object.getClass().getMethod(getter, new Class[] {});

Object value = method.invoke(object, new Object[] {});

if(value==null ){

isNUll=true;

}else{

if (fieldClass == String.class && StringUtils.isBlank((String)value)) {

isNUll=true;

}

}

return isNUll;

} catch (Exception e) {

return isNUll;

}

}

给实体类赋默认值通用方法

package com.clamc.common.util;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.math.BigDecimal;

import java.sql.Date;

import java.sql.Timestamp;

import java.util.ArrayList;

import java.util.List;

/**

* yangzhiwei

* 使用反射给实体类k赋值(默认值)

* insert update会报null异常,为空时不能插入和更新

*/

public class ObjInvoke {

public static Object getObjDefault(Object obj) {

// 得到类对象

Class objCla = obj.getClass();

Field[] fs = objCla.getDeclaredFields();

for (int i = 0; i < fs.length; i++) {

Field f = fs[i];

// 设置些属性是可以访问的

boolean isStatic = Modifier.isStatic(f.getModifiers());

if(isStatic){

continue;

}

// 设置些属性是可以访问的

f.setAccessible(true);

try {

// 得到此属性的值

Object val = f.get(obj);

// 得到此属性的类型

String type = f.getType().toString();

if (type.endsWith("String") && val == null) {

// 给属性设值

f.set(obj, "");

} else if ((type.endsWith("int") || type.endsWith("Integer") || type.endsWith("double")) && val == null) {

f.set(obj, 0);

}else if ((type.endsWith("long")|| type.endsWith("Long") )&& val == null){

f.set(obj, 0L);

} else if (type.endsWith("Date") && val == null) {

f.set(obj, Date.valueOf("1970-01-01"));

}else if(type.endsWith("Timestamp") && val == null){

f.set(obj, Timestamp.valueOf("1970-01-01 00:00:00"));

} else if (type.endsWith("BigDecimal") && val == null) {

f.set(obj, new BigDecimal(0));

}

} catch (Exception e) {

e.printStackTrace();

}

}

return obj;

}

public static List getObjDefaultList(List objList) {

Listhttp:// list=new ArrayList();

for(Object t:objList){

list.add(getObjDefault(t));

}

return list;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python中Numpy模块的使用(python numpy模块)
下一篇:Python中python-nmap模块的使用(python nmea)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~