jaxb object XML list conversion

wrote
 
 
 

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util .List;
/**
* @Description: Use the reflection mechanism to automatically bind the ResultSet to the JavaBean; automatically call the corresponding method in the javaBean according to the recordset.
*
* @param <T>
*/
public class SetDB2BeanUtil<T> {
/**
* @param clazz
* javaBean to be encapsulated
* @param rs
* recordset
* @return ArrayList contains multiple javaBeans
* @throws Exception
*/
public List<T> getList(Class<T> clazz, ResultSet rs) {
Field field = null;
List<T> lists = new ArrayList<T>();
try {
// Get the ResultSet column name
ResultSetMetaData rsmd = rs.getMetaData();
// Get the number of columns in the recordset
int counts = rsmd.getColumnCount();
// Define counts String variables
String[] columnNames = new String[counts];
String[] objNames = new String[counts];
// Assign values ​​to each variable (field names are all converted to lowercase)
for (int i = 0; i < counts; i++) {
columnNames[i] = rsmd.getColumnLabel(i + 1).toLowerCase();
objNames[i] = rsmd.getColumnLabel(i + 1).toLowerCase().replaceAll("_", "" );
}
// Variable ResultSet
while (rs.next()) {
T t = clazz.newInstance();
// Reflection, bind from ResultSet to JavaBean
for (int i = 0; i < counts; i++) {

// Set the parameter type, this type should be the same as the type in javaBean, not the type in the database
field = clazz.getDeclaredField(objNames[i]);

/ / Here is the type to get the bean attribute
Class<?> beanType = field.getType();

// According to the rs column name, assemble one of the set methods in the javaBean, object is the data in the first row and first column of the database
Object value = rs.getObject(columnNames[i]);

if (value != null) {

// here is to get the type of database field
Class<?> dbType = value.getClass();

// handle date type mismatch
if ( dbType == java.sql.Timestamp.class
&& beanType == java.util.Date.class) {
value = new java.util.Date(
((java.sql.Timestamp) value).getTime());
}
/ / handle double type mismatch
if (dbType == java.math.BigDecimal.class
&& beanType == Double.class) {
value = new Double(value.toString());
}
// 处理int类型不匹配问题
if (dbType == java.math.BigDecimal.class
&& beanType == Integer.class) {
value = new Integer(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Long.class) {
value = new Long(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Short.class) {
value = new Short(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Byte.class) {
value = new Byte(value.toString());
}
if (dbType == String.class
&& beanType == String.class) {
value = new String(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == BigDecimal.class) {
value = new BigDecimal(value.toString());
}
}

String setMethodName = "set"
+ firstUpperCase(columnNames[i]);
// The first parameter is the method name passed in, and the second parameter is passed in The type of;
Method m = t.getClass().getMethod(setMethodName, beanType);

// The second parameter is the data passed to the set method; if it is the get method
, m.invoke(t, value) can be omitted;
}
lists .add(t);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return lists;
}

/**
* @param clazz
* bean class
* @param rs
* result set (only the first result is encapsulated)
* @return the bean object that encapsulates the query result
*/
public T getObj(Class<T> clazz , ResultSet rs) {
Field field = null;
T obj = null;
try {
// Get the ResultSet column name
ResultSetMetaData rsmd = rs.getMetaData();
// Get the number of columns in the recordset
int counts = rsmd.getColumnCount();
/ / Define counts String variables
String[] columnNames = new String[counts];
String[] objNames = new String[counts];
// Assign values ​​to each variable (field names are all converted to lowercase)
for (int i = 0; i < counts; i++) {
columnNames[i] = rsmd.getColumnLabel(i + 1).toLowerCase();
objNames[i] = rsmd.getColumnLabel(i + 1).toLowerCase().replaceAll("_", "");
}
// variables ResultSet
if (rs.next()) {
T t = clazz.newInstance();
// Reflection, bind from ResultSet to JavaBean
for (int i = 0; i < counts; i++) {
try{
// set parameter type , this type should be the same as the type in javaBean, not the type in the database
field = clazz.getDeclaredField(objNames[i]);

}catch(Exception ex){
ex.printStackTrace();
continue;
}

// here is Get the type of bean attribute
Class<?> beanType = field.getType();

// According to the rs column name, assemble one of the set methods in the javaBean, object is the data in the first row and first column of the database
Object value = rs. getObject(columnNames[i]);

if (value != null) {

// here is to get the type of database field
Class<?> dbType = value.getClass();

// handle date type mismatch
if (dbType == java.sql.Timestamp.class
&& beanType == java.util.Date.class) {
value = new java.util.Date(
((java.sql.Timestamp) value).getTime());
}
// handle double type mismatch
if (dbType = = java.math.BigDecimal.class
&& beanType == Double.class) {
value = new Double(value.toString());
}
// handle int type mismatch
if (dbType == java.math.BigDecimal.class
&& beanType == Integer.class) {
value = new Integer(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Long.class) {
value = new Long(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Short.class) {
value = new Short(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == Byte.class) {
value = new Byte(value.toString());
}
if (dbType == String.class
&& beanType == String.class) {
value = new String(value.toString());
}
if (dbType == java.math.BigDecimal.class
&& beanType == BigDecimal.class) {
value = new BigDecimal(value.toString());
}
}

String setMethodName = "set"
+ firstUpperCase(columnNames[i]);
// The first parameter is the method name passed in, and the second parameter is the type passed in;
Method m = t.getClass().getMethod(setMethodName, beanType);

// The second parameter is the data passed to the set method; if it is the get method
, m.invoke(t, value);
}
obj = t;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return obj;
}

// capitalize initials
public static String firstUpperCase(String old) {
old=old.replaceAll("_", "");
return old.substring(0, 1).toUpperCase() + old. substring(1);
}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326769483&siteId=291194637