ONE Goal , ONE Passion !
HQL(Hibernate Query Language)是一种Hibernate专用的查询语句,基于面向对象的模式,将SQL语句转化成对象的操作格式.
@Test
public void query(){
// sql : select 列名 from 表明;
// hql : select 属性名 from 类名;
// select 别名.属性 from 类名 as 别名;
String hql = "from User";
Query query = session.createQuery(hql);
List list = query.list();
System.out.println(list);
}
查询多个属性时,返回的是对象数组;
其中对象中第一个为id,第二个元素为pwd
String hql = "select id,pwd from user";
List<Object[]> list = session.createQuery(hql);
String hql = "select count(*) from User";
Query query = session.createQuery(hql);
Object uniqueResult = query.uniqueResult();
setFirstResult(N):查询的第N条数据编号
setMaxResults(20):查询的数据总量
String hql = "from User";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(20);
List list = query.list();
System.out.println(list);
String hql = "from User where name = ? and age = ?";
Query query = session.createQuery(hql);
query.setParameter(0, "fy"); // query.setString(1, "fy");
query.setParameter(1, "18");
List list = query.list();
System.out.println(list);
常用写法:
String hql = "from User where name = :name and age = :age";
query.setString("name", "fy");
query.setString("age", "18");
聚合函数
count 返回的结果:Long
max/min 返回的结果:原始类型
sum 返回的结果:Long或Double
avg 返回的结果:Double
由于返回结果通常为单一数据,常使用uniqueResult()获得结果
Query q = s.createQuery("select address,count(address) from UserModel group by address");
Query q = s.createQuery("from UserModel order by age desc");
前提: 对象模型中需要有UserModel(age,name)构造方法,参数顺序必须与hql中投影一致.
s.createQuery("select new UserModel(age,name) from UserModel");
Criteria查询又名QBC查询,以面向对象格式完成查询操作,实现真正的自动生成SQL语句.
Criteria criteria = session.createCriteria(User.class);
//Hibernate: select this_.name as y0_ from userdata this_
方式一:
Property pro = Property.forName("name");
criteria.setProjection(pro);
方式二:
PropertyProjection property = Projections.property("name");
criteria.setProjection(property);
List list = criteria.list();
criteria.setProjection(property);不能重复复制,如果重复设置的话会把上一个覆盖.
1.声明查询属性的列表对象
ProjectionList li = Projections.projectionList();
2.声明要查询的属性
PropertyProjection name = Projections.property("name");
PropertyProjection id = Projections.property("id");
3.将查询的属性放入属性列表
li.add(id);
li.add(name);
4.为criteria设置属性列表
criteria.setProjection(li);
List
count: 行数
Projection condition = Projections.rowCount();
max/min: 最大值
Projection conditionMax = Projections.max("age");
Projection conditionMin = Projections.min("age");
Sum : 和
Projection conditionSum = Projections.sum("age");
Avg : 平均数
Projection conditionAvg = Projections.avg("age");
Projections.groupProperty("address");
criteria.addOrder(Order.desc("age"));
criteria.addOrder(Order.asc("age"));
条件查询使用
Restrictions.eq 等于= Restrictions.allEq 使用Map,使用key/value进行多个等于的判断
Restrictions.gt 大于>
Restrictions.ge 大于等于>=
Restrictions.lt 小于<
Restrictions.le 小于等于<=
Restrictions.between 对应sql的between子句
Restrictions.like 对应sql的like子句
Restrictions.in 对应sql的in子句
Restrictions.and and 关系
Restrictions.or or关系
Restrictions.sqlRestriction Sql限定查询
Criterion c = Restrictions.allEq(arg0);
criteria.add(c);
将查询字段在view层封装到DetachedCriteria对象中,
DetachedCriteria不依赖session即可创建. 这样session就不会暴露在view以及业务层.最后在Controller层,通过session把DetachedCriteria装换成可以执行查询的Criteria.
表现层:
DetachedCriteria dc = DetachedCriteria.forClass(UserModel.class);
dc.add(Restrictions.like("address", "%"+address+"%"));
dc.add(Restrictions.between("age", age1, age2));
数据层:
Criteria c = dc.getExecutableCriteria(session);
c.list();
String sql = "select * from tbl_user";
SQLQuery sq = session.createSQLQuery(sql);
sq.list(); //uniqueResult()
String sql = "select u.* from tbl_user u";
SQLQuery sq = session.createSQLQuery(sql);
sq.addEntity("别名",模型类.class); // sq.addEntity("u",模型类.class);
<query name="">query>
<sql-query name="">sql-query>
配置SQL语句时,为避免语句中具有与XML语法格式冲突的符号,通常使用使用XML语法.
<sql-query name="这条语句的名字"> :age]]>sql-query> :age 也是占位符
Query q = s.getNamedQuery("配置中的查询名字");