首页 > 编程语言 >java通过cglib动态生成实体bean的操作

java通过cglib动态生成实体bean的操作

时间:2022-11-11 15:11:56浏览次数:51  
标签:java String return bean cglib import public name

转载自:https://www.jb51.net/article/205882.htm

 

maven依赖:

1 2 3 4 5 6 7 8 9 10 <dependency>       <groupId>commons-beanutils</groupId>       <artifactId>commons-beanutils</artifactId>       <version>1.9.3</version>     </dependency>     <dependency>       <groupId>cglib</groupId>       <artifactId>cglib-nodep</artifactId>       <version>3.2.4</version>     </dependency>

DynamicBeanEntity.class动态bean类:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 package com.dym.entity; import net.sf.cglib.beans.BeanGenerator; import org.apache.commons.collections.map.MultiValueMap; import java.lang.reflect.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /**  * @author: zhaoxu  * @description:  */ public class DynamicBeanEntity {   Object dynamicBean;   Class clazz;   public DynamicBeanEntity(Map dynAttrMap) {     this.dynamicBean = generateBean(dynAttrMap);     clazz = dynamicBean.getClass();   }   /**    * 获取所有属性值    *    * @return    * @throws IllegalAccessException    */   public Map<String, Object> getValues() throws IllegalAccessException {     Map<String, Object> fieldValuesMap = new HashMap(16);     Field[] fields = clazz.getDeclaredFields();     for (Field field : fields) {       field.setAccessible(true);       Object fieldValue = field.get(dynamicBean);       fieldValuesMap.put(field.getName().split("\\$cglib_prop_")[1], fieldValue);     }     return fieldValuesMap;   }   /**    * 获取动态bean所有方法信息    *    * @return    */   public MultiValueMap getMethods() {     MultiValueMap map = new MultiValueMap();     Method[] methods = clazz.getMethods();     for (Method method : methods) {       Type[] genericParameterTypes = method.getGenericParameterTypes();       if (genericParameterTypes.length > 0) {         for (Type type : genericParameterTypes) {           map.put(method.getName(), type);         }       } else {         map.put(method.getName(), null);       }     }     return map;   }   /**    * 执行某个方法    *    * @param methodName    * @param parameters    * @return    * @throws InvocationTargetException    * @throws IllegalAccessException    * @throws NoSuchMethodException    */   public Object executeMethod(String methodName, Object... parameters) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {     ArrayList<Class> paramTypeList = new ArrayList();     for (Object paramType : parameters) {       paramTypeList.add(paramType.getClass());     }     Class[] classArray = new Class[paramTypeList.size()];     Method method = clazz.getMethod(methodName, paramTypeList.toArray(classArray));     Object invoke = method.invoke(dynamicBean, parameters);     return invoke;   }   /**    * 设置属性值    *    * @param property    * @param value    * @throws NoSuchFieldException    * @throws IllegalAccessException    */   public void setValue(String property, Object value) throws NoSuchFieldException, IllegalAccessException {     Field declaredField = clazz.getDeclaredField("$cglib_prop_" + property);     declaredField.setAccessible(true);     declaredField.set(dynamicBean, value);   }   /**    * 获取属性值    *    * @param property    * @return    * @throws NoSuchFieldException    * @throws IllegalAccessException    */   public Object getValue(String property) throws NoSuchFieldException, IllegalAccessException {     Field declaredField = clazz.getDeclaredField("$cglib_prop_" + property);     declaredField.setAccessible(true);     Object value = declaredField.get(dynamicBean);     return value;   }   public Object getEntity() {     return this.dynamicBean;   }   /**    * 利用cglib的BeanGenerator创建对象    *    * @param dynAttrMap    * @return    */   private Object generateBean(Map dynAttrMap) {     BeanGenerator generator = new BeanGenerator();     Iterator iterator = dynAttrMap.keySet().iterator();     while (iterator.hasNext()) {       String key = iterator.next().toString();       generator.addProperty(key, (Class) dynAttrMap.get(key));     }     return generator.create();   } }

test.class测试类测试动态生成bean

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.dym.attr; import com.dym.entity.DynamicBeanEntity; import org.apache.commons.collections.map.MultiValueMap; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; /**  * @author: zhaoxu  * @description:  */ public class test {   public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {     // 设置属性们,默认16     HashMap propertyMap = new HashMap(16);     propertyMap.put("name", String.class);     propertyMap.put("age", Integer.class);     propertyMap.put("height", Double.class);     // 生成动态 Entity     DynamicBeanEntity bean = new DynamicBeanEntity(propertyMap);     //设置属性值     bean.setValue("name", "zx");     bean.setValue("age", 22);     bean.setValue("height", 175.0);     //获取属性值     Map<String, Object> values = bean.getValues();     //获取可执行的方法     MultiValueMap methods = bean.getMethods();     //执行某个方法     bean.executeMethod("setAge", 23);     System.out.println("动态bean的age属性:"+bean.getValue("age"));   } }

test.class测试类测试链接数据库动态生成bean

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.dym.test; import com.dym.util.DBUtil; import com.dym.util.DynmicEntity; import org.springframework.stereotype.Component; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /**  * @author: zhaoxu  * @description:  */ @Component public class test {   public static void main(String[] args) {     ArrayList<DynmicEntity> beans = new ArrayList<>();     Connection conn = null;     PreparedStatement prst = null;     String sql = "";     sql = "select table_name from information_schema.tables where table_schema=\'public\'";     conn = DBUtil.getConn();     try {       prst = conn.prepareStatement(sql);       ResultSet rs = prst.executeQuery();       while (rs.next()) {         String tableName = rs.getString(1);         Map attrMap = new HashMap<>();         String findFieldSql = "SELECT format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull  \n" +             "FROM pg_class as c,pg_attribute as a where c.relname = \'" + tableName + "\' and a.attrelid = c.oid and a.attnum>0";         PreparedStatement fieldsPrst = conn.prepareStatement(findFieldSql);         ResultSet fieldsRs = fieldsPrst.executeQuery();         while (fieldsRs.next()) {           String fieldType = fieldsRs.getString(1);           String fieldName = fieldsRs.getString(2);           attrMap.put(fieldName, Object.class);         }         DynmicEntity bean = new DynmicEntity(attrMap);         beans.add(bean);       }     } catch (SQLException e) {       e.printStackTrace();     }     DBUtil.close(prst, conn);   } }

DBUtil.class:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 package com.dym.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /**  * @author: zhaoxu  * @description:  */ public class DBUtil {   private final static String URL="jdbc:postgresql://localhost:5432/dynmic";   private final static String NAME="postgres";   private final static String PASS="123456";   private static Connection conn=null;   /**    *    *@Title:DBUtil    *@Description:    */   public DBUtil(){   }   /**    *    * @Tiltle getConn    * @return Connection    * @Description:返回连接    */   public static Connection getConn(){     //告诉jvm使用mysql     try {       //加载驱动,string为驱动名字       Class.forName("org.postgresql.Driver");       //连接数据库,得到Connection连接       conn=DriverManager.getConnection(URL,NAME,PASS);       //System.out.println("连接数据库: "+conn);     }catch(ClassNotFoundException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }catch(SQLException e){       e.printStackTrace();     }     return conn;   }   //关闭结果对象集   public static void close(ResultSet rs){     if(rs!=null){       try{         rs.close();       }catch(SQLException e){         e.printStackTrace();       }     }   }   //关闭编译语句对象   public static void close(PreparedStatement prst){     if(prst!=null){       try{         prst.close();       }catch(SQLException e){         e.printStackTrace();       }     }   }   //关闭结果对象集   public static void close(Connection conn){     if(conn!=null){       try{         conn.close();       }catch(SQLException e){         e.printStackTrace();       }     }   }   //对于更新操作关闭资源   public static void close(PreparedStatement prst,Connection conn){     close(prst);     close(conn);   }   //关闭所有   public static void close(ResultSet rs,PreparedStatement prst,Connection conn){     close(rs);     close(prst);     close(conn);   } }

补充:java 反射 json动态转实体类

我就废话不多说了,大家还是直接看代码吧~

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package test.refect; public class Student {     // 姓名     private String name;     // 年龄     private String age;     // 住址     private String address;        public String getName() {         return name;     }        public void setName(String name) {         this.name = name;     }        public String getAge() {         return age;     }        public void setAge(String age) {         this.age = age;     }        public String getAddress() {         return address;     }        public void setAddress(String address) {         this.address = address;     }        @Override     public String toString() {         return "Student [name=" + name + ", age=" + age + ", address=" + address + "]";     }           public void sayHello(Book book){         System.out.println(book.getName());         } }
1 2 3 4 5 6 7 8 9 10 11 package test.refect; public class Book {     private String name;     public String getName() {         return name;     }        public void setName(String name) {         this.name = name;     }   }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package test.refect; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URLDecoder; import java.util.Iterator; import net.sf.json.JSONObject; public class Main {     public static void main(String[] args) throws Exception {                   //Student str --> Student 主类         String str = "test.refect.Student";         Class<?> clazz = Class.forName(str);                   //Book实体 str --> Book 参数类         String bookStr = "test.refect.Book";         Class<?> bookClazz = Class.forName(bookStr);                        //json --> Book    将参数类转为JSONOBJECT         String bookJson ="{\"name\":\"Java\"}";         JSONObject jsonObject=JSONObject.fromObject(bookJson);                   //实例化参数类         Object bookInStance = bookClazz.newInstance();                   // 通过JSONOBJECT 为参数类赋值         Iterator<?> keys = jsonObject.keys();     while (keys.hasNext()) {       Object key = keys.next();       Object value = jsonObject.get(key);       // 替换非法字符       String _key = String.valueOf(key).replaceFirst("\\W", "");       Field field = bookClazz.getDeclaredField(_key);       field.setAccessible(true);       field.set(bookInStance, URLDecoder.decode(String.valueOf(value), "UTF-8"));       field.setAccessible(false);     }           //将参数类注入到主类     Method method = clazz.getDeclaredMethod("sayHello", bookClazz);           //执行主类     method.invoke(clazz.newInstance(), bookInStance);           } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

标签:java,String,return,bean,cglib,import,public,name
From: https://www.cnblogs.com/zhncnblogs/p/16880502.html

相关文章

  • java使用mybatis-generator实现自动生成model、dao、xml
    pom中添加插件<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifa......
  • java逻辑运算中异或^
    本文主要阐明逻辑运算符^(异或)的作用a^b,相异为真,相同为假。注意,异或运算,还能交换两个变量。inta=1;intb=2;System.out.println("a="+a+",b="+b);a......
  • Java回调机制研究
    1.什么是回调函数回调函数,顾名思义,用于回调的函数。回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数。回调函数是一个工作流的一部分,由工作流来决定......
  • 大型CRM客户管理系统带小程序、H5 java源码(spring boot 后台 前端vue)
    CRM功能介绍1、系统管理:员工管理、角色管理、菜单管理、部门管理、岗位管理、字典管理、参数设置、日志管理2、系统监控:在线用户、定时任务、数据监控、服务监控3、系统工......
  • 上机题目(中级)- 将数组中的字符串按指定长度重新分割 (Java)
    题目如下:代码如下:packagehuawei;importjava.util.ArrayList;publicfinalclassDemo{/**功能:请编写一个函数,输入为一个字符串数组,*请按指定长度iInputLenth拆......
  • 上机题目(中级)- 两个超级大的整数相加相减 (Java)
    代码如下:publicclassAddSub{publicstaticvoidmain(String[]args){Stringa="4632864832684683568465765487657665765236465244";Stringb="47";i......
  • 上机题目(初级)- 下雪球(Java)
    上一节实现了滚雪球的代码,这一节实现下雪球的代码,简单考虑,制造一个循环,产生30个随机数,代码如下:importjava.awt.Color;importjava.awt.Frame;importjava.awt.Graphics;imp......
  • 如何通过Java 在 Word 中更改字体颜色
    在Word文档中使用不同的字体颜色既可以丰富文档外观,也能突出强调某些重点部分。当Word文档包含大量内容时,可以使用FreeSpire.DocforJava轻松更改Word文档中的字体颜色。......
  • java逻辑运算中 | | 和 | 的区别
    本文主要阐明逻辑运算中||(短路或)和|(逻辑或)的异同||和|的相同之处:只有二者都是假时,结果才为假,否则结果为true。||和|的不同之处在于:a||b:如果a为true......
  • java逻辑运算&&与&的区别
    本文主要阐述&&(短路与)和&(逻辑与)的运算异同:a&&b和a&b:共同之处是只有a和b同时为真时,结果才为真,否则为假不同点在于a&&b(短路与)若a为假时,则无需判断b,直接得出结......