用jpa写原生sql的时候,真的想骂人,各种问题。可是jsql又不支持sql的一些函数,只能写原生sql。
这里就先记录第一个坑,用jpa注解查询完之后发现无法用以往的实体类直接映射接收,百度谷歌无果...
这里实在没办法,用了个笨办法,将结果集用工具类转成实体类再返回。
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 954L
* @create 2019/10/30 17:27
*/
public class EntityUtils {
/**
* 将数组数据转换为实体类
* 此处数组元素的顺序必须与实体类构造函数中的属性顺序一致
*
* @param list 数组对象集合
* @param clazz 实体类
* @param <T> 实体类
* @param model 实例化的实体类
* @return 实体类集合
*/
public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz, Object model) {
List<T> returnList = new ArrayList<T>();
if (list.isEmpty()) return returnList;
Object[] co = list.get(0);
List<Map> attributeInfoList = getFiledsInfo(model);
Class[] c2 = new Class[attributeInfoList.size()];
if (attributeInfoList.size() != co.length) {
return returnList;
}
for (int i = 0; i < attributeInfoList.size(); i++) {
c2[i] = (Class) attributeInfoList.get(i).get("type");
}
try {
for (Object[] o : list) {
Constructor<T> constructor = clazz.getConstructor(c2);
returnList.add(constructor.newInstance(o));
}
} catch (Exception ex) {
return returnList;
}
return returnList;
}
private static Object getFieldValueByName(String fieldName, Object modle) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = modle.getClass().getMethod(getter, new Class[]{});
Object value = method.invoke(modle, new Object[]{});
return value;
} catch (Exception e) {
return null;
}
}
private static List<Map> getFiledsInfo(Object model) {
Field[] fields = model.getClass().getDeclaredFields();
List<Map> list = new ArrayList(fields.length);
Map infoMap = null;
for (int i = 0; i < fields.length; i++) {
infoMap = new HashMap(3);
infoMap.put("type", fields[i].getType());
infoMap.put("name", fields[i].getName());
infoMap.put("value", getFieldValueByName(fields[i].getName(), model));
list.add(infoMap);
}
return list;
}
}
@Query(value = "select ID as id, ITEM_NAME as itemName, IS_SORT as isSort from SPF_OT_TAX_ITEM " + "WHERE PARENT_ID = :parentId", nativeQuery = true)
List<Object[]> queryTaxItemVOByParentId(@Param("parentId") String parentId);
@Override
public List<TaxItem.TaxItemVO> queryTaxItemVOByParentId(String parentId) {
List<Object[]> objects = taxItemRepository.queryTaxItemVOByParentId(parentId);
return EntityUtils.castEntity(objects, TaxItem.TaxItemVO.class, new TaxItem.TaxItemVO());
}
public static class TaxItemVO{
private String id;
private String itemName;
private Integer isSort;
public TaxItemVO() {
super();
}
public TaxItemVO(String id, String itemName, Integer isSort) {
this.id = id;
this.itemName = itemName;
this.isSort = isSort;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Integer getIsSort() {
return isSort;
}
public void setIsSort(Integer isSort) {
this.isSort = isSort;
}
}
对你有帮助的话,右上角给个赞呗~
转自JPA原生sql返回值转自定义实体类_jpa自定义sql返回数据封装到实体类-CSDN博客