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