JDBC Open source framework: Dbutils Custom Business Type-dependent converters

Source: Internet
Author: User

The handler conversion provided by Dbutils does not meet the needs of actual business development. For example, the enumeration to int, the time type LocalDateTime, the Entity object's property name and the field does not correspond.

MySQL table member structure fields: ID, member_name, sex, createtime

public class Member {
Private long ID;
Private String membername;
private sex sex;
Private LocalDateTime Createtime;
}

Enumeration sex time LocalDateTime is not provided by default and needs to be converted. The conversion of data requires the implementation of the Columnhandler interface. This interface provides two simple methods: 1, whether the type matches the public boolean match (Class<?> PropType);

2. Convert data public Object apply (ResultSet rs, int columnindex).

First, define two types of conversion handler

public class Localdatetimehandler implements Columnhandler {

@Override
Public boolean match (Class<?> PropType) {
Return Proptype.equals (Localdatetime.class);
}

@Override
Public Object Apply (ResultSet rs, int columnindex) throws SQLException {
if (Rs.gettimestamp (columnindex) = null) {
Return Rs.gettimestamp (columnindex). Tolocaldatetime ();
}
return null;
}
}
public class Sexhandler implements Columnhandler {

@Override
Public boolean match (Class<?> PropType) {
Return Proptype.equals (Sex.class);
}

@Override
Public Object Apply (ResultSet rs, int columnindex) throws SQLException {
For (Sex sex:Sex.values ()) {
if (sex.getindex () = = Rs.getint (columnindex)) {
return sex;
}
}
return null;
}
}
public enum Sex {
Male (1, "male"),
Female (0, "female")
;
private int index;
Private String description;

Sex (int index, String description) {
This.index = index;
this.description = description;
}

public int GetIndex () {
return index;
}
Public String getdescription () {
return description;
}
}
Second, reconstruct the beanprocessor

public class Mybeanprocessor extends Beanprocessor {

private static Serviceloader<columnhandler> Columnhandlers = Serviceloader.load (Columnhandler.class);

private static list<columnhandler> customlist = new arraylist<> ();

static {
Customlist.add (New Localdatetimehandler ());
Customlist.add (New Sexhandler ());
}

Public Mybeanprocessor (map<string, string> columntopropertymap) {
Super (COLUMNTOPROPERTYMAP);
}

@Override
Protected Object Processcolumn (ResultSet rs, int index, class<?> propType)
Throws SQLException {
Object retval = rs.getobject (index);
if (!proptype.isprimitive () && retval = = null) {
return null;
}
for (Columnhandler handler:columnhandlers) {
if (Handler.match (PropType)) {
retval = handler.apply (rs, index);
Break
}
}

for (Columnhandler handler:customlist) {
if (Handler.match (PropType)) {
retval = handler.apply (rs, index);
Break
}
}
return retval;
}
}

Third, pass the custom parameter transformation data

@Test
    public void Customquery () throws SQLException {
         Queryrunner Queryrunner = new Queryrunner ();
        String sql = "SELECT * from member";

        map<string, string> Map = new HashMap ();
        map.put ("Member_name", "membername");
        mybeanprocessor bean = new Mybeanprocessor (map);
        Rowprocessor convert = new basicrowprocessor (bean);

        beanlisthandler<member> handler = new Beanlisthandler ( Member.class,convert);
        list<member> List = Queryrunner.query (Getconn (), SQL, Handler);
        System.out.println (json.tojsonstring (list));
   }

JDBC Open source framework: Dbutils Custom Business Type-dependent converters

Related Article

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.