QBC查询:
//Criteria 标准//conjunction连接词And//disConjunction Or//Restriction 约束@Testpublic void testQBC2() {Criteria criteria = session.createCriteria(Item.class);//添加排序criteria.addOrder(Order.asc("id"));//添加分页int pageNo = 2;int size = 1;criteria.setFirstResult((pageNo - 1) * size);criteria.setMaxResults(size);Conjunction conjunction = Restrictions.conjunction();
// conjunction.add(Restrictions.eq("id", 1));
// conjunction.add(Restrictions.like("name", "衣",MatchMode.END));Disjunction disjunction = Restrictions.disjunction();disjunction.add(Restrictions.eq("id", 1));disjunction.add(Restrictions.like("name", "球",MatchMode.END));criteria.add(conjunction);criteria.add(disjunction);List<Item> items = criteria.list();System.out.println(criteria);items.forEach((item) -> System.out.println(item.getName()));}
//统计查询@Testpublic void testQBC3() {Criteria criteria = session.createCriteria(Item.class);criteria.setProjection(Projections.max("amount"));System.out.println(criteria.uniqueResult());}
原生SQL查询:
@Testpublic void testSqlQuery() {String sql = "insert into category(name) values(?)";Query query = session.createSQLQuery(sql).setString(0, "母婴");query.executeUpdate();}