本文共 1176 字,大约阅读时间需要 3 分钟。
对于复杂SQL,可以直接调用native sql来是实现
public List findPartners() { List partnerList = new ArrayList<>(); // 这里的em是注入的EntityManager List partners = this.em.createNativeQuery("select id, no, name from partner").getResultList(); for(Object obj : partners){ Object[] objArray = this.convertToObjectArray(obj); Partner partner = new Partner(); partner.setId(objArray[0].toString()); partner.setNo(objArray[1].toString()); partner.setName(objArray[2].toString()); partnerList.add(partner); } return partnerList; } // 将Object转化为数组 private Object[] convertToObjectArray(Object array) { Class ofArray = array.getClass().getComponentType(); if (ofArray.isPrimitive()) { List ar = new ArrayList(); int length = Array.getLength(array); for (int i = 0; i < length; i++) { ar.add(Array.get(array, i)); } return ar.toArray(); } else { return (Object[]) array; } }
转载于:https://www.cnblogs.com/roostinghawk/p/6510197.html