在DAO层获取到hibernate模板,即hql执行crude操作:
this.getHibernateTemplate().get(User.class, id);
String hql=" FROM User info WHERE id='"+id+"';
this.getHibernateTemplate().find(sql);
(String)this.getHibernateTemplate().save(infopo);
Serializable pk = this.getHibernateTemplate().save(contractInfo);
return (String) pk;
该处返回的时生成的一个主键
this.getHibernateTemplate().update(infopo);
this.getHibernateTemplate().delete(pH);
SQLQuery是hibernate用于支持原生sql的接口类,然后调用list()或者uniqueResult()方法进行查询,
SQLQuery sqlQuery = session.createSQLQuery(" select * from Customer");
sqlQuery.addEntity(Customer.class);
注意这里查询的必须时全部的字段,否则会报错列名无效;
SQLQuery createSQLQuery=this.getSession().createSQLQuery("select a,b from t")
.addScalar("a").addScalar("b");
return createSQLQuery.list();
action:
List> list = statisticalQueryService.findPrescription(Bean bean,startIndex,this.getNumPerPage());
for (int i = 0; i < list.size(); i++) {
Object []object = (Object[]) list.get(i); // 将获取到的值,用object[]接受
bean= new StatisticalQueryVo(); //bean自建的 不对应数据库表,用于接收值,和返回页面值
bean.setContractNo((String) object[0]);
}
DAO:
public List findPrescription( StatisticalQueryConditionVo statisticalQueryConditionVo, int startIndex, int numPerPage) {
session = this.getSession();
StringBuilder str = new StringBuilder();
str.append("select t1.a,t1.b,t2.c,t2.d from t1 left join t2 on t1.id = t2.id "); //连表查询返回多个表的字段
Query query = session.createSQLQuery(hql).setFirstResult(startIndex).setMaxResults(numPerPage);// 分页操作
List list = query.list();
return list;
}
再将数组 object的值 赋值到 实体类返回到前端即可。
String sql = "insert delete update table where id= '?' "
this.getSession().createSQLQuery(sql).executeUpdate();