package com.dao.chu.po;
/**
*
* <p>Title: Customer</p>
* <p>Description:客户信息实体 </p>
*/
public class Customer {
private int id;
@IsRequired
private String name; // 姓名
@IsRequired
private String idnum; // 身份证号码
@IsRequired
private String phone; // 手机号
private String sex; // 性别
private int age; // 年龄
private String address; // 地址
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", idnum=" + idnum
+ ", phone=" + phone + ", sex=" + sex + ", age=" + age
+ ", address=" + address + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdnum() {
return idnum;
}
public void setIdnum(String idnum) {
this.idnum = idnum;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.dao.chu.po;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* <p>Title: IsRequired</p>
* <p>Description: 字段是否必填 </p>
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
public @interface IsRequired
{
/**
*
* <p>Title: isRequired</p>
* <p>Description:true:必填 false:非必填 </p>
* @return
*/
boolean isRequired() default true;
}
package com.dao.chu.po;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import javax.jws.WebResult;
import com.sun.xml.internal.ws.util.StringUtils;
/**
*
* <p>Title: PoUtils</p>
* <p>Description:Po操作工具类 </p>
*/
@SuppressWarnings("unused")
public class PoUtils
{
/**
* <p>Title: getProperties</p>
* <p>Description: 获取javabean属性通用方法 </p>
* @param t
* @param beanName
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws IntrospectionException
*/
private static <T> Object getProperties(T t, String beanName)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException
{
Object nameValue = new PropertyDescriptor(beanName, t.getClass()).getReadMethod().invoke(t);
return nameValue;
}
/**
* <p>Title: IsFieldBlank</p>
* <p>Description:判断前台传过来的必填字段是否为空 ,不正确则将相应字段返回 </p>
* @param t
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws IntrospectionException
*/
public static <T> RespBody IsFieldBlank(T t)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException
{
RespBody respBody = new RespBody();
StringBuffer sb = new StringBuffer();
Field[] declaredFields = t.getClass().getDeclaredFields();
for (Field field : declaredFields)
{
field.setAccessible(true);
String name = field.getName();
boolean fieldHasAnno = field.isAnnotationPresent(IsRequired.class);
if (fieldHasAnno)
{
IsRequired annotation = field.getAnnotation(IsRequired.class);
boolean required = annotation.isRequired();
if (required)
{
Object value = getProperties(t, name);
if (null == value)
{
sb.append(name + ",");
}
}
}
}
if (null==sb.toString()||"".equals(sb.toString()))
{
respBody.isSuccess();
}
respBody.setSuccess(false);
respBody.setMsg(sb.toString().substring(0,sb.toString().lastIndexOf(",")) + " is required");
return respBody;
}
}
package com.dao.chu.po;
/**
*
* <p>Title: RespBody</p>
* <p>Description: 响应实体类</p>
*/
public class RespBody
{
private boolean isSuccess = true;
private String msg;
private Object data;
public boolean isSuccess()
{
return isSuccess;
}
public void setSuccess(boolean isSuccess)
{
this.isSuccess = isSuccess;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
public Object getData()
{
return data;
}
public void setData(Object data)
{
this.data = data;
}
public RespBody(boolean isSuccess, String msg, Object data)
{
super();
this.isSuccess = isSuccess;
this.msg = msg;
this.data = data;
}
public RespBody(boolean isSuccess, String msg)
{
super();
this.isSuccess = isSuccess;
this.msg = msg;
}
public RespBody()
{
}
@Override
public String toString()
{
return "ReturnBody [isSuccess=" + isSuccess + ", msg=" + msg + ", data=" + data + "]";
}
}
package com.dao.chu.po;
/**
*
* <p>Title: IsRequiredTest</p>
* <p>Description: 必填成员变量测试类</p>
*/
public class IsRequiredTest {
public static void main(String[] args) {
Customer customer = new Customer();
try {
//=========第一次不赋值==========
RespBody respBody = PoUtils.IsFieldBlank(customer);
//不通过则返回提示信息
if (!respBody.isSuccess()) {
System.out.println("1."+respBody.getMsg());
}
//=========第二次给姓名赋值==========
customer.setName("张三");
respBody = PoUtils.IsFieldBlank(customer);
//不通过则返回提示信息
if (!respBody.isSuccess()) {
System.out.println("2."+respBody.getMsg());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有