1. 程式人生 > >Hibernate執行原生SQL

Hibernate執行原生SQL

nts rest tar current dex string new ash star

1、查詢指定字段

     public List<Object[]> getUseList( Integer index, Integer offset, String state, String search ){

            String sqlSelect = "select user_id, user_name ";
            String sql = sqlSelect + " from users where user_status in (:state) limit :index, :offset";

                SQLQuery sqlQuery 
= sessionFactory.getCurrentSession().createSQLQuery(sql); sqlQuery.setInteger("index", index); sqlQuery.setInteger("offset", offset); String [] stateList = state.split(","); sqlQuery.setParameterList("state", stateList); search
= "%" + search + "%"; sqlQuery.setString("userName", search); List<Object[]> restList = sqlQuery.list(); return restList; }

獲取結果集

for(Object[] restArr : restList){
                Map<String, Object> userMap = new
HashMap<>(); Integer userId = (Integer) restArr[0]; String userName = (String) restArr[1]; }

Hibernate執行原生SQL