repository类
public interface MiddleRepositity extends JpaRepository <Middle,Long>{
@Query(nativeQuery = true,value = "select count(*) as con,m.user_id,s.`type`,s.name,s.id as starId from middle m left join star s on(m.star_id=s.id) group by user_id,s.id,s.`type`,s.name;")
public List<Object[]> getSelectData();
}
实体类vo层
@Data
public class SelectVo {
private BigInteger con;
private BigInteger user_id;
private String type;
private String name;
private BigInteger star_id;
public SelectVo(BigInteger con, BigInteger user_id, String type, String name, BigInteger star_id) {
this.con = con;
this.user_id = user_id;
this.type = type;
this.name = name;
this.star_id = star_id;
}
方法类
public class ObjectUtil {
//转换实体类
public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) throws Exception {
List<T> returnList = new ArrayList<T>();
Object[] co = list.get(0);
Class[] c2 = new Class[co.length];
//确定构造方法
for (int i = 0; i < co.length; i++) {
c2[i] = co[i].getClass();
}
for (Object[] o : list) {
Constructor<T> constructor = clazz.getConstructor(c2);
returnList.add(constructor.newInstance(o));
}
return returnList;
}
}