MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5673字,纯文字阅读大概需要9分钟。
内容图文
![MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils](/upload/InfoBanner/zyjiaocheng/1261/7dfa73596cd1481ba0b8a0a3173059af.jpg)
这里在网上找了一份ReflectionUtils
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
package com.litian.jdbc; /** * @author : Li Tian * @contact: litian_cup@163.com * @software: IntelliJ IDEA * @file: ReflectionUtils.java * @time: 2020/3/26 18:57 * @desc: |JDBC 查询得到属性字段 反射机制返回到 JavaBean中相同类属性名的对象中 */ import java.lang.reflect.*; publicclass ReflectionUtils { /** * 使 filed 变为可访问 * * @param field */publicstaticvoid makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } } /** * 直接设置对象的属性,忽略 private/protected 修饰符, 也不经过 setter * * @param object * @param fieldName 属性名称 * @param value */publicstaticvoid setFieldValue(Object object, String fieldName, Object value) { Field field = getDeclaredField(object, fieldName); if (field == null) thrownew IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); // 让 private 元素变得可以访问,field.setAccessible(); makeAccessible(field); try { field.set(object, value); } catch (IllegalArgumentException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 循环向上转型, 获取对象的 DeclaredField * * @param object * @param filedName * @return*/publicstatic Field getDeclaredField(Object object, String filedName) { // 一步步的循环得到 获取声明对象的祖宗类for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { return superClass.getDeclaredField(filedName); } catch (NoSuchFieldException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } returnnull; } /** * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter * * @param object * @param fieldName * @return*/publicstatic Object getFieldValue(Object object, String fieldName) { Field field = getDeclaredField(object, fieldName); if (field == null) thrownew IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field); Object result = null; try { result = field.get(object); } catch (IllegalArgumentException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } /** * 循环向上转型, 获取对象的 DeclaredMethod * * @param object * @param methodName * @param parameterTypes: 指定特定成员方法有重载可能性,必须指定特定的类型变量 * @return*/publicstatic Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) { for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { //superClass.getMethod(methodName, parameterTypes);return superClass.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { //Method 不在当前类定义, 继续向上转型 } //.. } returnnull; } /** * 直接调用对象方法, 而忽略修饰符(private, protected) * * @param object * @param methodName * @param parameterTypes * @param parameters * @return * @throws InvocationTargetException * @throws IllegalArgumentException */publicstatic Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws InvocationTargetException { Method method = getDeclaredMethod(object, methodName, parameterTypes); if (method == null) thrownew IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]"); method.setAccessible(true); // 使用 method.invoke()方法进行运行try { method.invoke(object, parameters); } catch (IllegalAccessException | IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return method; } /** * 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型 * 如: public EmployeeDao extends BaseDao<Employee, String> * * @param clazz * @return*/publicstatic Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass(); // 判定s是否是ParameterType相对应的if (!(genType instanceof ParameterizedType)) return Object.class; // 强制转换 获取超类泛型参数实际类型,返回genType 是类的接口,基本类型或者void Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) return Object.class; if (!(params[index] instanceof Class)) return Object.class; return (Class) params[index]; } /** * 通过反射,获得Class定义中声明的父类的泛型参数的类型. * eg. * public UserDao extends HibernateDao<User> * * @param clazz The class to introspect * @return the first generic declaration, or Object.class if cannot be determined */ @SuppressWarnings("unchecked") publicstatic <T> Class<T> getSuperClassGenricType(final Class clazz) { return getSuperClassGenricType(clazz, 0); } }
JAVA类属性
在JAVAEE中,JAVA类的属性通过getter,setter来定义:get(set)方法:去除get(或set)后,首字母小写即为该类的属性。
而之前的属性,即成员变量称之为字段。
操作java类的属性有一个工具包:beanutils(需要结合logging来用)
BeanUtils.setProperty()
BeanUtils.getProperty()
一般情况下,字段名和属性名都一致
通过该工具包可以将之前的ReflectionUtil替换为:BeanUtils.setProperty(entity, propertyName, value);
BeanUtils测试代码
package com.litian.jdbc; import org.apache.commons.beanutils.BeanUtils; import java.lang.reflect.InvocationTargetException; /** * @author : Li Tian * @contact: litian_cup@163.com * @software: IntelliJ IDEA * @file: BeanUtilsTest.java * @time: 2020/3/27 15:37 * @desc: |测试工具包beanutils */ public class BeanUtilsTest { public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { // 测试赋值操作 Object obj = new User(); System.out.println(obj); BeanUtils.setProperty(obj, "username", "二哈"); System.out.println(obj); // 测试获取操作 Object val = BeanUtils.getProperty(obj, "username"); System.out.println(val); } }
————————————————
版权声明:本文为CSDN博主「李英俊小朋友」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21579045/article/details/105386353
原文:https://www.cnblogs.com/qiu-hua/p/13199632.html
内容总结
以上是互联网集市为您收集整理的MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils全部内容,希望文章能够帮你解决MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。