以下Hibernate查询是我在上学那会总结的,时隔多年,但用法依旧,长时间不用偶尔楼上一眼代码也是信手拈来
HQL查询
实体查询 && 属性查询
SessionFactory = null; Session = null; try { sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Query = session.createQuery("from App_User"); // session.createQuery("from App_User as a where name = 'admin' "); // session.createQuery("select username from App_User ") // 属性查询 List<App_User> list = query.list(); } catch (Exception e) { e.printStackTrace(); }
参数绑定
? 占位符
session = sessionFactory.openSession(); // Query = session.createQuery("select userName from App_User where userName = ? "); Query query = session.createQuery("from App_User where userName = ? "); query.setString(0, "xiaokui"); List<App_User> list = query.list();
: 命名参数
session = sessionFactory.openSession(); Query query = session.createQuery("from App_User where userName = :name "); query.setString("name", "xiaokui"); // 这里不能使用 占位符索引方式 List<App_User> list = query.list();
封装参数
第一步:先定义一个普通查询类 public class QueryUser { private String name; private int age; private String address; // 省略getter和setter方法 } 第二步:封装参数 QueryUser qu = new QueryUser(); qu.setName("xiaokui"); qu.setAge(20); 第三步:查询 session = sessionFactory.openSession(); Query = session.createQuery("from App_User where (username like :name) and (age = :age)"); query.setProperties(qu); List<App_User> list = query.list();
分页
(1) 根据结果获得记录数
除了Hibernate中 int count = list.size()
,还有以下方式获取总记录数
ScrollableResults sr = query.scroll(); sr.last(); if (sr.getRowNumber() >= 0) { this.totalResults = sr.getRowNumber() + 1; }else { this.totalResults = 0; }
(2) 计算总页数
int totalPages (count%pageSize == 0) ? (count/pageSize) :(count/pageSize + 1);
(3) 实现分页
query.setFirstResult((pageIndex - 1) * pageSize); // 第一条记录的位置 query.setMaxResults(pageSize); // 返回的最大记录条数 List<App_User> list = query.list();
子查询
session.createQuery("select * from App_User where roleid = (select roleid from role roleid = 1)");
连接查询
内联接 inner join 或 join
迫切内联接 inner join fetch 或 join fetch
左外联接 left outer join 或 left join
迫切左外联接 left outer join fetch 或 left join fetch
左外联接 right outer join 或 right join
Form Entity inner join [fetch] Entity.property
session.createQuery("from App_User u inner join fetch roleid r");
命名查询
<hibernate-mapping package="cn.wingfly.bean"> <class catalog="money_note" name="App_User" table="app_user"> <id name="uuid" type="java.lang.Integer"> <column name="uuid" /> <generator class="increment" /> </id> ... </class> <query name="loginUser"> <![CDATA[ from User u where u.name = :name and u.password = :password; ]]> </query> </hibernate-mapping> Query query = session.getNamedQuery("loginUser"); query.setString("name", "xiaokui"); query.setString("password", "0123456"); List<App_User> list = query.list();
分组、统计、排序,直接在sql语句中操作
例如:select sum(price) from house group by id having sum(price) > 1000
Criteria查询
查询所有
sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(App_User.class); List<App_User> list =criteria.list();
查询条件
序号 | 方法 | 说明 |
1 | Restrictions.eq() | = |
2 | Restrictions.allEq() | 使用Map 使用key/value等进行多个相等值对比 |
3 | Restrictions.gt() | > |
4 | Restrictions.ge() | >= |
5 | Restrictions.lt() | < |
6 | Restrictions.le() | <= |
7 | Restrictions.between() | between子句 |
8 | Restrictions.like() | like |
9 | Restrictions.in() | In |
10 | Restrictions.add() | add |
11 | Restrictions.or() | or |
12 | Restrictions.not() | not |
Criteria criteria = session.createCriteria(App_User.class); criteria.add(Restrictions.eq("userName", "xiaokui")); // 查询用户名为xiaokui的数据 List<App_User> list =criteria.list();
使用Example查询
sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(App_User.class); App_User user = new App_User(); user.setUserName("xiaokui"); user.setPassWord("0123456"); criteria.add(Example.create(user)); List<App_User> list =criteria.list();
查询排序
session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(App_User.class); criteria.addOrder(Order.desc("id")); // criteria.addOrder(Order.asc("id")); List<App_User> list =criteria.list();
分页
session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(App_User.class); criteria.setFirstResult(10); // 从第10条开始,最大显示8条数据 criteria.setMaxResults(8); List<App_User> list =criteria.list();
统计查询
通过 ProjectionList 和 Projections 查询统计
序号 | 方法 | 说明 |
1 | Projections.avg() | avg 函数 |
2 | Projections.count() | count 函数 |
3 | Projections.max() | max 函数 |
4 | Projections.min() | min 函数 |
5 | Projections.sum() | sum函数 |
session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(App_User.class); ProjectionList pl = Projections.projectionList(); pl.add(Projections.groupProperty("roleid")); // 分组 pl.add(Projections.rowCount()); criteria.setProjection(pl); List<App_User> list =criteria.list();
未经允许请勿转载:程序喵 » Hibernate查询方式总结