当前位置: 代码迷 >> 综合 >> Hibernate中的QBC(标准查询)Criteria 即原生SQL查询
  详细解决方案

Hibernate中的QBC(标准查询)Criteria 即原生SQL查询

热度:64   发布时间:2023-10-26 08:43:46.0

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();}

 

  相关解决方案