标签:hibernate native sql mysql 原生sql
使用原生 SQL 的工具类:
public SQLQuery findQueryByNativeSQL(final String sql, final List values, final int first, final int limit) { Object obj = this.getHibernateTemplate().execute(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery query = session.createSQLQuery(sql); query.setCacheable(true); if (values != null) { for (int i = 0; i < values.size(); i++) { query.setParameter(i, values.get(i)); } } if (first > 0) { query.setFirstResult(first); } if (limit > 0) { query.setMaxResults(limit); } return query; } }); if (obj != null) return (SQLQuery) obj; return null; }
SQLQuery query = this.findQueryByNativeSQL(sql.toString(), values, pge * limit, limit);
public class ArticleDTO implements Serializable { private Integer id; private ArticleMeetingProp articleMeetingProp; public ArticleMeetingProp getArticleMeetingProp() { return articleMeetingProp; } public void setArticleMeetingProp(ArticleMeetingProp articleMeetingProp) { this.articleMeetingProp = articleMeetingProp; } public Integer getId() {return id;} public void setId(Integer id) { this.id = id; } }
query.addEntity("articleMeetingProp", ArticleMeetingProp.class); //query.addScalar(“id", Hibernate.INTEGER); //如果字段有别名 query.setResultTransformer(Transformers.aliasToBean(ArticleDTO.class));
List<ArticleDTO> articleDTOList = query.list();
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:hibernate native sql mysql 原生sql
原文地址:http://blog.csdn.net/wenniuwuren/article/details/48698871