Hibernate原生sql用例

public Map<String,String> getUserById(String userId) {
        Map<String, String> map = new HashMap<String, String>();
        String sql="select  a.userNo,(select h.code from  t_class h 
         where h.id = a.class_id ) as code from 
         t_user a where a.id ='"+userId+"' ";

        Query q=em.createNativeQuery(sql);
        List<Object[]> resultList = (List<Object[]>)q.getResultList();

        if(null != resultList && resultList.size()>0){
            for (Object[] objects : resultList) {
    map.put("userNo", ((String) objects[0]!=null?(String) objects[0]:""));
    map.put("code", ((String) objects[1]!=null?(String) objects[1]:""));    
            }
        }
        return map;
    }
Map<String,String> map = this.getUserById(userId);
                userNo = map.get("userNo");
                code = map.get("code");

猜你喜欢

转载自blog.csdn.net/icecoola_/article/details/79886210