当前位置: 代码迷 >> SQL >> HibernateTemplate施行普通sql语句
  详细解决方案

HibernateTemplate施行普通sql语句

热度:45   发布时间:2016-05-05 13:48:40.0
HibernateTemplate执行普通sql语句
@Overridepublic List<ViewRecord> getViewRecordByUserAndRandom6(int userId) {	final int userIdf = userId;	List<ViewRecord> viewRecordList = this.getHibernateTemplate().executeFind(new HibernateCallback() {	public Object doInHibernate(Session session) throws HibernateException, SQLException {         SQLQuery query = session.createSQLQuery("select * from viewrecord where userId=? order by rand() limit 6");  	query.setInteger(0, userIdf);	return query.list();			}		});	return viewRecordList;	}

或者
@Overridepublic List<ViewRecord> getViewRecordByUserAndRandom6(int userId) {   String sql = "select * from viewrecord where userId=? order by rand() limit 6"   Session session = this.getSession();   List<ViewRecord> viewRecordList = session.createSQLQuery(sql).list();   return viewRecordList;}
  相关解决方案