@Test
public void testHql() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 测试数据
/*for (int i = 0; i < 20; i++) {
Customer customer = new Customer();
customer.setName("测试" + i + 1);
customer.setAge(20 + i);
session.save(customer);
}*/
// 1
// Query query = session.createQuery("from Customer");
// Query query = session.createQuery("from Customer where age >= 22");
// Query query = session.createQuery("from Customer where age > ?").setInteger(0, 22);
// Query query = session.createQuery("from Customer where age < :age").setInteger("age", 22);
// 分页
// Query query = session.createQuery("from Customer").setFirstResult(1).setMaxResults(4);
// 2
// Criteria query = session.createCriteria(Customer.class).add(Restrictions.lt("age", 22));
// Criteria query = session.createCriteria(Customer.class).add(Restrictions.like("name", "三", MatchMode.ANYWHERE));
// 分页
// Criteria query = session.createCriteria(Customer.class).addOrder(Order.desc("age")).setFirstResult(2).setMaxResults(4);
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
session.close();
}