¡¡¡¡1.¼ò½é ÔÚÒ»°ãµÄдbean×é¼þµÄʱºò£¬¶¼±ØÐëҪдsetterºÍgetter·½·¨£¬µ±È»¼ÙÈçÎÒÃÇÊÂÏÈÒѾ֪µÀbeanµÄÏà¹ØÊôÐÔºÍ ·½·¨£¬Ð´beanÊDZȽϼòµ¥µÄ£¬µ«ÊÇ×é¼þÌ«¶àµÄʱºò£¬Öظ´±àд¾³£ÊÇ¿ÝÔ﷦ζÁîÈËÑá·³µÄ¡£µ«µ±ÓÐЩʱºòÎÒôÐè Òªµ÷Óö¯Ì¬¶ÔÏóµÄÊôÐÔµÄʱºò£¬ÎÒÃÇÓ¦¸ÃÔõôÀ´É趨ºÍ»ñÈ¡¶ÔÏóµÄÊôÐÔÄØ£¿BeanUtils¾Í¿ÉÒÔ°ïÖúÎÒÃÇÀ´½â¾öÕâ ¸öÎÊÌâ¡£ËüÐèÒªCollections°üºÍlogging°üµÄÖ§³Ö¡£×îа汾1.7,Îĵµ»¹ÊÇ1.6.1µÄ. 2.ÒѾʹÓÃBeanUtilsµÄÊÂÀý *¹¹½¨ºÍJava¶ÔÏóÄ£Ðͽ»»¥µÄ½Å±¾ÓïÑÔ£¬±ÈÈçBean Scripting Framework *¹¹½¨web²ãÏÔʾºÍÏàËÆÓÃ;µÄÄ£°åÓïÑÔ´¦ÀíÆ÷£¬Èçjsp»òÕßVelocity *¹¹½¨ÊÊÓÃÓÚjspºÍxsp»·¾³µÄ×Ô¶¨Òå±êÇ©¿â£¬ÈçJakarta Taglibs,Struts,Cocoon *´¦Àí»ùÓÚXMLµÄÅäÖÃ×ÊÔ´Îļþ£¬ÈçAnt´´½¨½Å±¾£¬Ó¦ÓóÌÐò²¿ÊðÎļþºÍTomcatµÄxmlÅäÖÃÎļþ 3.API¼ò½é BeanUtils µÄ Java API Ö÷ÒªµÄ package ×ܹ²Ëĸö org.apache.commons.beanutils Õâ¸öÊÇBeanUtils×î»ù±¾µÄ°ü£¬ÌṩÁË´¦ÀígetterºÍsetter·½·¨ÊôÐÔµÄÀà org.apache.commons.beanutils.converters Converter½Ó¿ÚµÄ±ê׼ʵÏÖ£¬ÔÚÆô¶¯µÄʱºòºÍConvertUtilsһͬע²á org.apache.commons.beanutils.locale ×é¼þµÄ±¾µØ»¯´¦Àí°ü org.apache.commons.beanutils.locale.converters LocaleConverter½Ó¿ÚµÄ±ê׼ʵÏÖ£¬Æô¶¯µÄʱºòºÍLocaleConvertUtils һͬע²á 4.org.apache.commons.beanutils½éÉÜ ÕâËĸö°ü±¾×¨ÌâÖ»½éÉÜÕâÒ»¸ö,ÆäËûµÄʹÓõÄʱºòÇë×Ô¼º²Î¿¼ËüµÄÎĵµ,ÎĵµÄÚÈÝдµÃºÜºÃ. 1.PropertyUtils ËüÖ§³ÖÈýÖÖÀàÐ͵ÄÊôÐÔÖµ-Simple£¬Indexed£¬Mapped ÎÒÃÇÏÈ´´½¨Ò»¸ö¼òµ¥µÄbean public class Employee { public Address getAddress(String type); public void setAddress(String type, Address address); public Employee getSubordinate(int index); public void setSubordinate(int index, Employee subordinate); public String getFirstName(); public void setFirstName(String firstName); public String getLastName(); public void setLastName(String lastName); } *SimpleÊôÐÔ·ÃÎÊ·½·¨ PropertyUtils.getSimpleProperty(Object bean, String name) PropertyUtils.setSimpleProperty(Object bean, String name, Object value) ʵÏÖ´úÂ룺 Employee employee = ...; String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName"); String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName"); ... manipulate the values ... PropertyUtils.setSimpleProperty(employee, "firstName", firstName); PropertyUtils.setSimpleProperty(employee, "lastName", lastName); *IndexedÊôÐÔ·ÃÎÊ·½·¨ PropertyUtils.getIndexedProperty(Object bean, String name) PropertyUtils.getIndexedProperty(Object bean, String name, int index) PropertyUtils.setIndexedProperty(Object bean, String name, Object value) PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value) ʵÏÖ´úÂ룺 Employee employee = ...; int index = ...; String name = "subordinate[" + index + "]"; Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, name);//¸ù¾ÝvalueÈ¡µÃÊôÐÔ
Employee employee = ...; int index = ...; Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, "subordinate", index);//¸ù¾ÝË÷Òý ֵȡµÄÊôÐÔÖµ *MappedÊôÐÔ·ÃÎÊ·½·¨ PropertyUtils.getMappedProperty(Object bean, String name) PropertyUtils.getMappedProperty(Object bean, String name, String key) PropertyUtils.setMappedProperty(Object bean, String name, Object value) PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value) ʵÏÖ´úÂ룺 Employee employee = ...; Address address = ...; PropertyUtils.setMappedProperty(employee, "address(home)", address);//¸ù¾ÝÊý×é //ÄÚµÄÖµÀ´È¥µÄÏàÓ¦µÄÊôÐÔÖµ Employee employee = ...; Address address = ...; PropertyUtils.setMappedProperty(employee, "address", "home", address); *NestedÊôÐÔ·ÃÎÊ·½·¨//NestedµÄÒâ˼ÊDzÎÊýÖаüº¬×é¼þ PropertyUtils.getNestedProperty(Object bean, String name) PropertyUtils.setNestedProperty(Object bean, String name, Object value) ʵÏÖ´úÂ룺 String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city"); 2.BeanUtils.DynaBean and BeanUtils.DynaClass ½Ó¿Ú½éÉÜ DynaBean±ØÐëÓÐÒ»¸öbeanÀ´ÊµÏÖÕâ¸ö½Ó¿Ú£¬DynaClass±ØÓÐÒ»¸öbeanµÄÊôÐÔ¼¯À´ÊµÏÖ *BasicDynaBean and BasicDynaClass - »ù±¾µÄ Dynamic ÀàÐÍ »ù±¾µÄAPI: BasicDynaClass(java.lang.String name, java.lang.Class dynaBeanClass, DynaProperty[] properties) BasicDynaBean(DynaClass dynaClass) ÎÒÃÇÏß¶¨Òåһϻù±¾µÄ´úÂ룺 //¶¨Ò嶯̬ÊôÐÔ¼¯ DynaProperty[] props = new DynaProperty[]{ new DynaProperty("address", java.util.Map.class), new DynaProperty("subordinate", mypackage.Employee[].class), new DynaProperty("firstName", String.class), new DynaProperty("lastName", String.class) }; //´´½¨¶¯Ì¬ÀàÀ´É趨¶¯Ì¬ÊôÐÔÖµ BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props); DynaBean employee = dynaClass.newInstance(); employee.set("address", new HashMap()); employee.set("subordinate", new mypackage.Employee[0]); employee.set("firstName", "Fred"); employee.set("lastName", "Flintstone"); *ResultSetDynaClass (Wraps ResultSet in DynaBeans) - ʹÓà ResultSet µÄ Dynamic JavaBean API£º ResultSetDynaClass(java.sql.ResultSet resultSet) ResultSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase) Connection conn = ...; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery ("select account_id, name from customers"); Iterator rows = (new ResultSetDynaClass(rs)).iterator(); while (rows.hasNext()) { //ÀûÓö¯Ì¬bean½øÐÐÊä³ö DynaBean row = (DynaBean) rows.next(); System.out.println("Account number is " + row.get("account_id") + " and name is " + row.get("name")); } rs.close(); stmt.close(); *RowSetDynaClass (Disconnected ResultSet as DynaBeans) - ʹÓà RowSet µÄ Dynamic JavaBean API£º RowSetDynaClass(java.sql.ResultSet resultSet) RowSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase)
Connection conn = ...; // ´Ó»º³å³ØÈ¡µÃÁ¬½Ó Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT ..."); RowSetDynaClass rsdc = new RowSetDynaClass(rs); rs.close(); stmt.close(); ...; //¹Ø±ÕÁ¬½Ó·µ»Ø»º³å³Ø List rows = rsdc.getRows(); ...; // ´¦ÀíµÃµ½µÄÐÐ 2.BeanUtils.ConvertUtils ½éÉÜ Õâ¸ö°üÖ÷ÒªÓÃÀ´×ª»»´Órequest´«µÝ¹ýÀ´µÄº¯Êý Ö÷Òªº¯Êý£º ConvertUtils().convert(java.lang.Object value) ConvertUtils().convert(java.lang.String[] values, java.lang.Class clazz) ConvertUtils().convert(java.lang.String value, java.lang.Class clazz) ʵÏÖÀý×Ó£º HttpServletRequest request = ...; MyBean bean = ...; HashMap map = new HashMap(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); map.put(name, request.getParameterValues(name)); } BeanUtils.populate(bean, map);//ÓÃConvertUtils½øÐÐת»» Ŀǰ֧³ÖµÄת»»ÀàÐÍ£º sjava.lang.BigDecimal java.lang.BigInteger boolean and java.lang.Boolean byte and java.lang.Byte char and java.lang.Character java.lang.Class double and java.lang.Double float and java.lang.Float int and java.lang.Integer long and java.lang.Long short and java.lang.Short java.lang.String java.sql.Date java.sql.Time java.sql.Timestamp 5.²Î¿¼×ÊÁÏ»òÎÄÕÂ: *http://jakarta.apache.org/commons/beanutils/api/index.Html *http://jakarta.apache.org/commons/beanutils/index.html *http://apache.Linuxforum.net/dist/jakarta/commons/beanutils/binaries/commons-beanutils-1.6.1.zip
|