ResultSet data in Java is automatically populated into classes & class instance fills PreparedStatement

Source: Internet
Author: User
Tags aliases reflection

Demand:

(1) Extract resultset value into a class is very troublesome, need a new variable, using the corresponding member of the set method to assign value, can be based on class, directly extract the data in the ResultSet, finally return the instantiated class?

(2) You can use PreparedStatement to precompile SQL statements with variables, before execute, you need to fill in the variable value, one setting is too troublesome, can provide a class variable, using the value of the class member variable to automatically populate the PreparedStatement?

Such a feature many open source framework can be achieved, but do not want to because of such a small amount of demand to learn so large a set of framework, so I realized a bit, summed up their own realization ideas.

The following two issues to implement this framework must be taken into account:

The field name of the database table may not be the same as the member name of the class. For example, a database table often has such a named Modify_time, which is rarely used in a class, and is generally preferred to modifytime such a naming method.

The order of the variables in the precompiled SQL statement and the order of the member variables in the class may vary.

How to map the value of a class member to the corresponding variable in the precompiled PreparedStatement.

The way to resolve issues 1 and 2 is to provide aliases for class members, using the annotation (callout) mechanism in Java, with the callout interface as follows:

1 @Target (Elementtype.field)

2 @Retention (Retentionpolicy.runtime)

3 public @interface Column {

4 String value ();

5}

Examples of annotations:

public class Work extends workabstraction{
        
    @Column (value = "Coop_type")
    private Integer cooptype;
        
    Private String subtitle;
    Private String coworker;
    Private String tutor;
        
    @Column (value = "Create_time")
    private String createtime;
        
    @Column (value = "Modify_time")
    private String modifytime;
    Private String content;

There is a work table in the database, a field name is Modify_time, the corresponding field in the corresponding entity class work is named Modifytime, using @column (value = "Modify_time") Provides alias Modify_time for property Modifytime, and for properties without aliases, the default is the same as the column name in resultset.

Only providing aliases is not enough, from the resultset can get to the query results of the column Name field, if you want to fill in the ResultSet data in a class, you need a data structure:

Hashmap<class<?>,hashmap<string,field>> Mappingpools;

The primary key of the HASHMAP is the value of the class,hashmap of the data class or a map,hashmap<string,field> The primary key of the map is the alias of the member variable in the class, the value is the Java reflection Type Field, Using the Java reflection mechanism, you can easily populate data classes with data by field.

The next question is known class, how to parse out the HASHMAP structure above, the code is as follows:

public class Beanmappingpool {private static lock lock = new Reentrantlock ();
    private static hashmap<class<?>,hashmap<string,field>> Mappingpools;
    static{mappingpools = new hashmap<class<?>,hashmap<string,field>> ();
    }; public static hashmap<string,field> Getfieldsmap (Class<?> objclass) {if Mappingpools.get (objClass)!=
        NULL) return Mappingpools.get (objclass);
        Lock.lock ();
            if (Mappingpools.get (objclass)!= null) {Lock.unlock ();
        Return Mappingpools.get (objclass);
        } hashmap<string,field> pools = new hashmap<string,field> (); for (; Objclass!= object.class, objclass = Objclass.getsuperclass ()) for (Field F:objclass.getdeclaredfields ())
                {f.setaccessible (true);
                Column col = f.getannotation (column.class); if (col!= null) pools.put (Col.value (), F);
            Else Pools.put (F.getname (), f);
        } mappingpools.put (objclass, pools);
        Lock.unlock ();
    return pools; }
}

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.