first commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package com.budwk.app.base.sqlCallback;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.mysql.cj.MysqlType;
|
||||
import org.apache.commons.lang3.reflect.TypeUtils;
|
||||
import org.nutz.dao.impl.jdbc.BlobValueAdaptor;
|
||||
import org.nutz.dao.jdbc.Jdbcs;
|
||||
import org.nutz.dao.sql.Sql;
|
||||
import org.nutz.dao.sql.SqlCallback;
|
||||
import org.nutz.json.Json;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class FetchVoCallback implements SqlCallback {
|
||||
|
||||
protected Class<?> clazz;
|
||||
|
||||
public FetchVoCallback(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
|
||||
if (null != rs && rs.next()) {
|
||||
try {
|
||||
Object obj = clazz.getDeclaredConstructor().newInstance();
|
||||
String name = null;
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
int count = meta.getColumnCount();
|
||||
for (int i = 1; i <= count; ++i) {
|
||||
name = meta.getColumnLabel(i);
|
||||
int columnType = meta.getColumnType(i);
|
||||
String columnTypeName = meta.getColumnTypeName(i);
|
||||
|
||||
Field field = ReflectUtil.getField(clazz, name);
|
||||
|
||||
if(ObjectUtil.isNull(field)){
|
||||
continue;
|
||||
}
|
||||
|
||||
Object object = null;
|
||||
|
||||
if (columnType == 93 || columnType == 91) {
|
||||
object = rs.getTimestamp(i);
|
||||
} else if (columnType == 2004) {
|
||||
object = (new BlobValueAdaptor(Jdbcs.getFilePool())).get(rs, name);
|
||||
} else if (columnType == 2005) {
|
||||
object = rs.getString(i);
|
||||
} else if (columnType == -1) {
|
||||
if (columnTypeName.equals(MysqlType.JSON.getName())) {
|
||||
Class<?> type = field.getType();
|
||||
if (type.isAssignableFrom(List.class)) {
|
||||
System.out.println(rs.getString(i));
|
||||
//List的泛型怎么获取
|
||||
if (field.getGenericType() instanceof ParameterizedType) {
|
||||
ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
||||
Type[] actualTypeArguments = pt.getActualTypeArguments();
|
||||
for (Type typeArg : actualTypeArguments) {
|
||||
System.out.println("List Field Generic Type: " + TypeUtils.toString(typeArg));
|
||||
/*if(typeArg.getTypeName().equals(String.class.getName())){
|
||||
if(ObjectUtil.isNotNull(rs.getString(i))){
|
||||
object = Json.fromJsonAsList(String.class, rs.getString(i));
|
||||
break;
|
||||
}
|
||||
} else if(typeArg.getTypeName().equals(JSONObject.class.getName())) {
|
||||
if(ObjectUtil.isNotNull(rs.getObject(i))){
|
||||
object = Json.fromJsonAsList(JSONObject.class, rs.getString(i));
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
if(ObjectUtil.isNotNull(rs.getObject(i))){
|
||||
object = Json.fromJsonAsList(Class.forName(typeArg.getTypeName()), rs.getString(i));
|
||||
break;
|
||||
}
|
||||
//todo Map暂未实现
|
||||
}
|
||||
}else{
|
||||
object = rs.getString(i);
|
||||
}
|
||||
}else if(type.isArray()){
|
||||
object = Json.fromJsonAsArray(type, rs.getString(i));
|
||||
}else{
|
||||
object = Json.fromJson(type, rs.getString(i));
|
||||
}
|
||||
} else {
|
||||
object = rs.getObject(i);
|
||||
}
|
||||
} else {
|
||||
object = rs.getObject(i);
|
||||
}
|
||||
|
||||
//生成clazz类型的实例
|
||||
if(ObjectUtil.isNotNull(field)){
|
||||
ReflectUtil.setFieldValue(obj, field, object);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||
NoSuchMethodException | ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.budwk.app.base.sqlCallback;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.TypeUtil;
|
||||
import com.mysql.cj.MysqlType;
|
||||
import org.nutz.dao.DaoException;
|
||||
import org.nutz.dao.impl.jdbc.BlobValueAdaptor;
|
||||
import org.nutz.dao.jdbc.Jdbcs;
|
||||
import org.nutz.dao.pager.ResultSetLooping;
|
||||
import org.nutz.dao.sql.Sql;
|
||||
import org.nutz.dao.sql.SqlCallback;
|
||||
import org.nutz.dao.sql.SqlContext;
|
||||
import org.nutz.json.Json;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* VO 查询回调
|
||||
* 如果你的字段是json类型 那么也可以根据VO的类型来做映射
|
||||
* todo 暂时只支持List<String>
|
||||
* @author zhaoxinyu
|
||||
*/
|
||||
public class QueryVoCallback implements SqlCallback {
|
||||
|
||||
protected Class<?> clazz;
|
||||
|
||||
public QueryVoCallback(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Connection connection, ResultSet rs, Sql sql) throws SQLException {
|
||||
final ResultSetMetaData meta = rs.getMetaData();
|
||||
|
||||
|
||||
ResultSetLooping ing = new ResultSetLooping() {
|
||||
protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCount) {
|
||||
try {
|
||||
Object obj = clazz.getDeclaredConstructor().newInstance();
|
||||
String name = null;
|
||||
int count = meta.getColumnCount();
|
||||
for (int i = 1; i <= count; ++i) {
|
||||
name = meta.getColumnLabel(i);
|
||||
int columnType = meta.getColumnType(i);
|
||||
String columnTypeName = meta.getColumnTypeName(i);
|
||||
|
||||
Field field = ReflectUtil.getField(clazz, name);
|
||||
|
||||
if(ObjectUtil.isNull(field)){
|
||||
continue;
|
||||
}
|
||||
|
||||
Object object = null;
|
||||
|
||||
if (columnType == 93 || columnType == 91) {
|
||||
object = rs.getTimestamp(i);
|
||||
} else if (columnType == 2004) {
|
||||
object = (new BlobValueAdaptor(Jdbcs.getFilePool())).get(rs, name);
|
||||
} else if (columnType == 2005) {
|
||||
object = rs.getString(i);
|
||||
} else if (columnType == -1) {
|
||||
if (columnTypeName.equals(MysqlType.JSON.getName())) {
|
||||
try{
|
||||
Class<?> type = field.getType();
|
||||
|
||||
if (type.isAssignableFrom(List.class)) {
|
||||
Type typeArgument = TypeUtil.getTypeArgument(field.getGenericType());
|
||||
if(ObjectUtil.isNotNull(rs.getString(i))){
|
||||
object = Json.fromJsonAsList(Class.forName(typeArgument.getTypeName()), rs.getString(i));
|
||||
}
|
||||
}else if(type.isArray()){
|
||||
if(ObjectUtil.isNotNull(rs.getString(i))){
|
||||
object = Json.fromJsonAsArray(type.getComponentType(), rs.getString(i));
|
||||
}
|
||||
}else{
|
||||
object = Json.fromJson(type, rs.getString(i));
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
object = rs.getObject(i);
|
||||
}
|
||||
} else {
|
||||
object = rs.getObject(i);
|
||||
}
|
||||
|
||||
//生成clazz类型的实例
|
||||
if(ObjectUtil.isNotNull(field)){
|
||||
ReflectUtil.setFieldValue(obj, field, object);
|
||||
}
|
||||
}
|
||||
list.add(obj);
|
||||
} catch (SQLException e) {
|
||||
throw new DaoException(e);
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
ing.doLoop(rs, sql.getContext());
|
||||
return ing.getList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user