package com.hanqi.dao; import static org.junit.Assert.*; import java.util.*; import org.hibernate.service.*; import org.hibernate.cfg.Configuration; import org.hibernate.*; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.junit.*; public class TestNews { //构建会话工厂对象 较为消耗资源 SessionFactory sf = null; //构建会话对象 Session se = null; //开启事务 Transaction tr = null; @Before public void init() { System.out.println("这是测试之前的代码"); //第一步:构建一个配置类 Configuration conf = new Configuration().configure(); //第二步:构建注册器类 ServiceRegistry serv = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); //构建会话工厂对象 较为消耗资源 sf = conf.buildSessionFactory(serv); //构建会话对象 se = sf.openSession(); //开启事务 tr = se.beginTransaction(); } //HQL查询 @Test public void testquery() { // 2 - 1 查询 // String hql = "select n.id,n.title from News n order by n.id desc"; //News 是一个类 // 2 - 2 查询 // String hql = "select count(n.id),min(n.id),max(n.id) from News n"; // 2 - 3 group by 分组 // String hql = "select n.title,count(n.title) from News n group by n.title"; // 2 - 4 group by having 分组 // String hql = "select n.title,count(n.title) from News n group by n.title having count(n.title) > 4"; // 2 - N 输出方式 /* List<Object[]> l = query.list(); for(Object[] news : l) { for(Object o : news) { System.out.println( o + " "); } System.out.println(); } */ } //HQL查询 @Test public void testQuery() { // 1 - 1 //String hql = "from News n where n.id = :ID and n.title like :Title"; // 1 - 2 //String hql = "from News n where n.id = ? and n.title like ?"; //降序排序 // 1 - 3 // String hql = "from News n order by n.id desc"; /* // 1 - 1 List<News> l = query.setInteger("ID", 23) //参数名只要能够与语句中的参数名匹配起来就可以 .setString("Title", "%标题%") .list(); */ /* // 1 - 2 List<News> l = query.setInteger(0, 23) .setString(1, "%标题%") .list(); */ // 1 - 3 // List<News> l = query.List(); //遍历输出查询 // 1 - N 均使用下列方式遍历 /* List<News> l = query.list(); for(News news : l) { System.out.println(news); } */ } //HQL分页查询 @Test public void testQueryPage() { // 1 - 4 String hql = "from News n"; //生成Query对象 Query query = se.createQuery(hql); //分页 //页数 int pagenum = 3; //每页最大行数 int rows = 5; List<News> l = query.setFirstResult((pagenum - 1) * rows) //起始行的编号 .setMaxResults(rows) //每页显示最大行数 .list(); System.out.println("结果集行数 = " + l.size() ); for(News news : l) { System.out.println(news); } } //一级缓存 @Test public void testYJHC() { News news1 = (News)se.get(News.class, 23); System.out.println(news1); News news2 = (News)se.get(News.class, 23); System.out.println(news2); } @Test public void test() { System.out.println("这是测试"); //执行操作 //数据查询 //查询,转成,持久化状态 News news2 = (News)se.get(News.class, 22); // News 对象名 = (News(强制转化为的类型))会话对象.get.(实体类类型,主键值) System.out.println(news2); } @Test public void testGet() { //执行操作 //执行数据插入 News news = new News();//转成,瞬时状态(临时状态) news.setTitle("标题"); news.setContent("内容"); news.setCreatedate(new Date()); news.setAuthor("作者"); //保存,瞬时状态转成持久化状态 se.save(news); //将实体类传进去 news.setTitle("新标题栏3"); //将对应数据进行修改 //刷新 se.flush(); //主动执行一次刷新 //提交事务 tr.commit(); //关闭会话 se.close(); //构建会话对象 se = sf.openSession(); //开始事务 tr = se.beginTransaction(); // news.setTitle("改变的标题"); // news.setId(1); //托管状态 se.saveOrUpdate(news); //会自动判断插入与修改 // se.update(news); //将托管状态转为持久化状态 news.setContent("新的内容1"); //删除 News news3 = (News)se.get(News.class, 1); //(News.class, 要删除的id号) se.delete(news3); } //延迟加载 @Test public void testLoad() { News news4 = (News)se.load(News.class, 1); System.out.println("id = " + news4.getId()); System.out.println("title = " + news4.getTitle()); } @After public void destroy() { System.out.println("测试之后执行"); //提交事务 tr.commit(); //关闭会话 se.close(); //关闭会话工厂 sf.close(); } }