将List数组对象转换成List集合对象

使用jpa的原生sql,将返回的记过封装到list map中是用的工具类
由于jpa对于左连接不支持,只能写原生的sql
hql.append("SELECT u.id as id,u.account as account,u.name as userName,r.name as roleName,")
        .append(sql2)
        .append(",u.cardNo as cardNo,u.email as email,u.printAuth as printAuth,u.colorAuth as colorAuth")
        .append(" from user as u")
        .append(" LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");
    query=em.createNativeQuery("select count(u.id) from user as u LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");


public static List, Object>> listArrayToListMap(List list,String... keys) {
    if(list==null||list.size()==0){
        return null;
    }
    List, Object>> mapList = new ArrayList,Object>>();
    for(int i=0;i;i++){
        if(!(list.get(i) instanceof Map)){
            HashMap, Object> map = new HashMap, Object>();
            Object[] m = (Object[])list.get(i);
            for(int j=0;jlength;j++){
                map.put(keys[j],m[j]);
            }
            mapList.add(map);
        }
    }
    return mapList;
} 
  
关于jps原生sql
query=em.createNativeQuery("select count(u.id) from user as u LEFT JOIN department d ON u.departmentId=d.id LEFT JOIN role r ON u.roleId=r.id");
BigInteger  bi= (BigInteger) query.getSingleResult();
返回类型是BigInteger  
而HSQL返回的是LONG类型


你可能感兴趣的:(java基础)