Java-JPA原生SQL查询返回自定义DTO:
import com.tjgeo.njsosms.framework.repository.IBaseRepository; import com.tjgeo.njsosms.risk.support.entity.Source; import org.springframework.data.jpa.repository.Query; import java.util.List; /** * ${Description} * * @author wenmingming on 2020/4/13 */ public interface SourceRepository extends IBaseRepository<Source, String> { Source findBySourceId(String sourceId); Source findBySourceIdAndShowState(String sourceId, Integer showState); /** * XX首页-风险柱形图统计 * @return */ @Query(nativeQuery = true, value = "SELECT S1.RISKLEVEL,(SELECT S2.DICNAME FROM SYS_DICTIONARY S2 WHERE S2.DICTYPE='业务板块' AND S2.DICVALUE=S1.PLATE)BIZNAME FROM RISK_SOURCE_DATA S1 WHERE S1.RISKLEVEL IS NOT null") List<Object[]> findByGroupStatics00(); /** * XX首页-风险柱形图统计+线路条件 * @return */ @Query(nativeQuery = true, value = "SELECT S1.RISKLEVEL,(SELECT S2.DICNAME FROM SYS_DICTIONARY S2 WHERE S2.DICTYPE='业务板块' AND S2.DICVALUE=S1.PLATE)BIZNAME FROM RISK_SOURCE_DATA S1 WHERE S1.RISKLEVEL IS NOT null AND (S1.LINEID=:lineId OR S1.LINEID='allLines')") List<Object[]> findByGroupStatics00AndLineId(String lineId); }
Java对下转Object转Class实体对象:
import org.slf4j.Logger; 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; /** * JPA-扩展工具 * @param */ public class JpaObjUtil { /** * 将数组数据转换为实体类 * 此处数组元素的顺序必须与实体类构造函数中的属性顺序一致 * * @param list 数组对象集合 * @param clazz 实体类 * @param <T> 实体类 * @param model 实例化的实体类 * @param logger 日志祖级 * @return 实体类集合 */ public static <T> List<T> castEntity(Logger logger,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) { logger.error("实体数据转化为实体类发生异常:异常信息:{}", ex.getMessage()); return returnList; } return returnList; } /** * 根据属性名获取属性值 * * @param fieldName 属性名 * @param modle 实体类 * @return 属性值 */ 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; } } /** * 获取属性类型(type),属性名(name),属性值(value)的map组成的list * * @param model 实体类 * @return list集合 */ 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; } }
扩展用例用法:
List<Object[]> queryObj=Lists.newArrayList();
if (Strings.isNotEmpty(lineId)){
queryObj=sourceRepository.findByGroupStatics00AndLineId(lineId);
}
else{
queryObj=sourceRepository.findByGroupStatics00();
}
GroupRiskStatic01Dto resultModel=new GroupRiskStatic01Dto();
List<GroupRiskStatic01Dto> riskList= JpaObjUtil.castEntity(logger ,queryObj,GroupRiskStatic01Dto.class,resultModel);
标签:实体类,return,自定义,JPA,list,S1,List,import,DTO From: https://www.cnblogs.com/oumi/p/18040047