Optimized beanutils components for Java JDBC

Source: Internet
Author: User

1. beanutils Components 1.1 Introduction

The JavaBean operation is very frequent in the program , so Apache provides a set of open source APIstofacilitate the JavaBean the operation! That is , the beanutils component.

  Beanutils components, the role is to simplify the operation of JavaBean!

The user can download the beanutils component from www.apache.org and then introduce the jar file in the project !

Use Benutils Components:

    1. Introducing the Commons-beanutils-1.8.3.jar Core package
    2. Introduction Log Support package : Commons-logging-1.1.3.jar

if the log jar file is missing, error:

Java.lang.noclassdeffounderror:org/apache/commons/logging/logfactory

At Org.apache.commons.beanutils.convertutilsbean.<init> (convertutilsbean.java:157)

At Org.apache.commons.beanutils.beanutilsbean.<init> (beanutilsbean.java:117)

At Org.apache.commons.beanutils.beanutilsbean$1.initialvalue (beanutilsbean.java:68)

1.2 Example , basic usage

Method   1: Copy of object Properties

Beanutils. Copyproperty (admin, "UserName", "Jack");

Beanutils. SetProperty (admin, "age", 18);

Method   2: Copy of the object

Beanutils. copyproperties (newadmin, admin);

Method   3: map data Copy to JavaBean Note:the key in map is to be associated with JavaBean The property name is consistent "

Beanutils. Populate (Adminmap, map);

1     //1: Basic Operation of the JavaBean2 @Test3      Public voidTest1 ()throwsException {4 5         //creating a User Object6User User =NewUser ();7 8         //A:beanutiles component implements copy of object properties9Beanutils.copyproperty (User, "UserName", "Zhang San");TenBeanutils.setproperty (User, "age", 22); One         //Summary 1: Type conversions are automatic for basic data types A  -         //B:beanutiles Component Implementation copy of Object -User NewUser =NewUser (); the beanutils.copyproperties (newuser, user); -  -         //C:beanutiles Component Implementation copies map data to Objects -         //first create a map collection and give the data +map<string, object> maptest =NewHashmap<string, object>(); -Maptest.put ("UserName", "Lzl"); +Maptest.put ("Age", 21); A         //creating a User Object atUser UserMap =NewUser (); - beanutils.populate (UserMap, maptest); -  -         //Test: - System.out.println (usermap); -}
1.3 Example : copy of a date type

to register a date type converter, see the following code in 2 ways:

1 //Custom Date type converters2 @Test3      Public voidDate1 ()throwsException {4 5         //simulating form data6String name = "Mark";7String age = "22";8String birth = "1994-09-09";9 Ten         //1: Create Object OneUser User =NewUser (); A  -         //2: Registration date Converter: Custom  -  theConvertutils.register (NewConverter () { -  - @Override -              PublicObject Convert (Class type, object value) { +                 //1: First to judge the incoming string data -                 if(Type! = Date.)class) { +                     return NULL; A                 } at                 if(Value = =NULL|| "". Equals (Value.tostring (). Trim ())) { -                     return NULL; -                 } -              -                 Try { -                     //2: String conversion to date in                     //formatting dates, defining -SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); to                     returnSdf.parse (value.tostring ()); +}Catch(ParseException e) { -                     Throw NewRuntimeException (e); the                 } *             } $ Panax Notoginseng}, Date.class); -       //Use a copy of the object's properties to copy the required variables (attributes) into the user object theBeanutils.copyproperty (User, "UserName", name); +Beanutils.copyproperty (User, "age", age); ABeanutils.copyproperty (User, "Birthdayf", birth); the  + System.out.println (user); -}

Optimized beanutils components for Java JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.