本文主要是介绍Java使用反射对于自定义SQL进行赋值教训总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、首先建立一张实体表->映射到db库里面的表
演示首先使用实体类:
public class Employee {/**** 列名称*/private String column;/**** 列属性值*/private String property;public String getColumn() {return column;}public void setColumn(String column) {this.column = column;}public String getProperty() {return property;}public void setProperty(String property) {this.property = property;}@Overridepublic String toString() {return "Employee{" +"column='" + column + '\'' +", property='" + property + '\'' +'}';}
}
2、首先就是使用反射进行对类的属性进行赋值:
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {test2();}public static void test() throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {String selectSQL= " SELECT count(id) as sum from employee where #start_date < date_add(create_time,interval 30 HOUR) ";String currentDate = DateUtils.getCurrDate();String yesterDay="'"+DateUtils.countDate(currentDate, -1)+"'";String name="";if(selectSQL.contains("#start_date")){System.out.println("yesterDay :"+yesterDay);name=selectSQL.replace("#start_date",yesterDay);System.out.println("selectSQL :"+name);}Employee employee=new Employee();employee.setColumn("列值1111111");employee.setProperty("属性值");Class clazz=employee.getClass();//获取Class对象方式1Field[] fields=clazz.getDeclaredFields();//获取变量System.out.println("getDeclaredFields:"+fields.length);for(Field field:fields){field.setAccessible(true);if(field.getName().equals("column")){field.set(employee,"测试数据");//设置字段的值}if(field.getName().equals("property")){field.set(employee,"属性值");//设置字段的值}// System.out.println("field.getAnnotations:"+field.getAnnotations().length+"\tfield.getName:"+field.getName()+"\tfield.get:"+field.get(employee));//获取实例属性名和值}System.out.println(employee.getColumn());System.out.println(employee.toString());// String clazzName=clazz.getName();//获取类名,含包名
// String clazzSimpleName=clazz.getSimpleName();//获取类名,不含包名
// System.out.println("getName:"+clazzName+"\tgetSimpleName:"+clazzSimpleName);
//
// int mod=clazz.getModifiers();//获取类修饰符
// System.out.println("Modifier.isPublic:"+Modifier.isPublic(mod));//判断类修饰符
// System.out.println("Modifier.isProtected:"+Modifier.isProtected(mod));//判断类修饰符
//
// Package p=clazz.getPackage();//获取包
// System.out.println("getPackage:"+p);
//
// Class superClass=clazz.getSuperclass();//获取父类
// System.out.println("getSuperclass:"+superClass);
//
// Class[] interfaces=clazz.getInterfaces();//获取实现接口
// System.out.println("getInterfaces:"+interfaces.length);
//
// Constructor[] cons=clazz.getConstructors();//构造方法
// System.out.println("getConstructors:"+cons.length);
//
// Method[] methods=clazz.getMethods();//获取所有方法
// System.out.println("getMethods:"+methods.length);
// for(Method method:methods){
// System.out.println("method.getName:"+method);
// }// Method method=clazz.getMethod("getColumn", null);//获取指定方法
// System.out.println("getMethod(,):"+method);
// Object methodVlaue=method.invoke(employee, null);//调用方法
// System.out.println("method.invoke(,):"+methodVlaue);// Method method3=clazz.getMethod("setProp3",Double.class);//获取指定方法
// System.out.println("getMethod(,):"+method3);
// Object methodVlaue3=method3.invoke(employee, 2.0);//调用setter方法,该方法没有返回值,所以methodVlaue3为null;此处注意参数2.0 ,不能用null
// System.out.println("method.invoke(,):"+methodVlaue3);// Field[] fields=clazz.getDeclaredFields();//获取变量
// System.out.println("getDeclaredFields:"+fields.length);
// for(Field field:fields){
// field.setAccessible(true);
// field.set(employee,"测试数据");//设置字段的值
// System.out.println("field.getAnnotations:"+field.getAnnotations().length+"\tfield.getName:"+field.getName()+"\tfield.get:"+field.get(employee));//获取实例属性名和值
//
// }// Annotation[] annos=clazz.getAnnotations();//获取类注解
// System.out.println("getAnnotations:"+annos.length);}
显然,首先确保private属性能读进去,
然后就是判断属性名称限定条件,是否需要HashMap转换,
最后进行赋值,是可以实现输出内容,显然数值赋值成功完成。
这篇关于Java使用反射对于自定义SQL进行赋值教训总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!