AppTest.java:
package com.shuoeasy.test; import java.util.Arrays; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Conjunction; import org.hibernate.criterion.Disjunction; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projection; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; import javassist.compiler.Javac.CtFieldWithInit; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } /** * 插入数据 */ @Test public void testInsert(){ User user = new User(); user.setUserName("张三"); Orders order1 = new Orders(); order1.setTitle("张三的订单1"); order1.setUser(user); Orders order2 = new Orders(); order2.setTitle("张三的订单2"); order2.setUser(user); user.getOrders().add(order1); user.getOrders().add(order2); session.save(user); session.save(order1); session.save(order2); } /** * QBC,面向对象方式查询数据 */ @Test public void testQBC(){ // 1.创建一个 Criteria 对象 Criteria criteria = session.createCriteria(User.class); // 2.添加查询条件:在 QBC 中查询条件使用 Criterion 来表示 // SELECT * FROM user WHERE userName='张三' AND id>10; criteria.add(Restrictions.eq("userName","张三")); criteria.add(Restrictions.gt("id", 10)); // 3.执行查询 User user = (User) criteria.uniqueResult(); System.out.println(user); } /** * 通过对象来做条件 */ @Test public void testQBC2(){ // 1.创建一个 Criteria 对象 Criteria criteria = session.createCriteria(Orders.class); // 2.添加查询条件:在 QBC 中查询条件使用 Criterion 来表示 // SELECT * FROM orders WHERE user_id=5 AND id>1; User user = new User(); user.setId(5); user.setUserName("张三"); criteria.add(Restrictions.eq("user",user)); criteria.add(Restrictions.gt("id", 1)); // 3.执行查询 List<Orders> order = criteria.list(); System.out.println(order); } /** * 加入或条件 */ @Test public void testQBCOr(){ // 1.创建一个 Criteria 对象 Criteria criteria = session.createCriteria(User.class); // 2.添加查询条件:在 QBC 中查询条件使用 Criterion 来表示 // SELECT * FROM user WHERE userName='张三' AND (id=1 OR id=2); Conjunction conjuntion = Restrictions.conjunction(); conjuntion.add(Restrictions.eq("userName", "张三")); // OR Disjunction disjunction = Restrictions.disjunction(); disjunction.add(Restrictions.eq("id",1)); disjunction.add(Restrictions.eq("id",2)); criteria.add(conjuntion); criteria.add(disjunction); // 3.执行查询 List<User> users = criteria.list(); System.out.println(users); } /** * 统计函数 */ @Test public void testSum(){ // 1.创建一个 Criteria 对象 Criteria criteria = session.createCriteria(User.class); // 2.添加查询条件:在 QBC 中查询条件使用 Criterion 来表示 // SELECT max(id) FROM; criteria.setProjection(Projections.max("id")); // 3.执行查询 int obj = (int)criteria.uniqueResult(); System.out.println(obj); } /** * 排序,分页 */ @Test public void testOrderBy(){ // 1.创建一个 Criteria 对象 Criteria criteria = session.createCriteria(User.class); // 2.添加查询条件:在 QBC 中查询条件使用 Criterion 来表示 // SELECT * FROM user ORDER BY username DESC, id DESC LIMIT 3,3 ; // criteria.add(Restrictions.eq("userName","张三")); // criteria.add(Restrictions.gt("id", 10)); // 排序 criteria.addOrder(Order.desc("userName")); criteria.addOrder(Order.desc("id")); // 分页 int pageNo = 3; int pageSize = 3; criteria.setFirstResult((pageNo -1) * pageSize); criteria.setMaxResults(pageSize); // 3.执行查询 List<User> users = criteria.list(); System.out.println(users); } }