使用HQL一直很慌张,没有底气
闲来抽了时间把常用的操作测试一遍,亲测可用
1、无条件 查询所有字段
String hql = "from Customer u " ;
2、添加where语句条件查询
String hql = "from Customer u where u.customerSource=2" ;
3、多条件 and 查询
String hql = "from Customer u where u.customerSource=1 and u.customerID=1" ;
4、查询指定字段
String hql = "select u.customerID,u.customerSource from Customer u where u.customerSource=2" ;
5、查询TOP
String hql = "select u.customerID,u.customerSource from Customer u " ;
this.getHibernateTemplate().setMaxResults(3);
6、分组汇总
String hql = "select u.customerSource,count(1) from Customer u group by u.customerSource " ;
7、传参条件查询
String hql = "from Customer u where u.customerSource=?" ;
List list = this.getHibernateTemplate().find(hql,new Integer(2).longValue());
8、bean中外键直接引用查询
private Long customerSource; 其实就是 BaseDict 中的 private Long