DAO :
1 //查询 2 public List<?> createSqlQueryList(final String queryString); 3 //修改 4 public Boolean createSqlUpdate(final String queryString);
DAOImpl:
1 /** 2 * sqlQuery 返回list 3 */ 4 public List<?> createSqlQueryList(final String queryString) { 5 return (List<?>) getHibernateTemplate().execute( 6 new HibernateCallback<Object>() { 7 public List<?> doInHibernate( 8 org.hibernate.Session session) 9 throws org.hibernate.HibernateException { 10 SQLQuery query = session.createSQLQuery(queryString);//执行session语句! 11 List<?> rows = query.list(); 12 if (session != null && session.isConnected()) { 13 session.close(); 14 } 15 return rows; 16 } 17 }); 18 }
1 /** 2 * sqlQuery 执行sql语句修改方法 3 */ 4 public Boolean createSqlUpdate(final String queryString) { 5 return (Boolean) getHibernateTemplate().execute( 6 new HibernateCallback<Object>() { 7 public Boolean doInHibernate( 8 org.hibernate.Session session) 9 throws org.hibernate.HibernateException { 10 session.createSQLQuery(queryString).executeUpdate();//执行语句.executeUpdate方法 11 if (session != null && session.isConnected()) { 12 session.close(); 13 } 14 return true; 15 } 16 }); 17 }
注意(区别):
原文:https://www.cnblogs.com/-Jazz/p/10434252.html