• Rhythmk 学习 Hibernate 09


    1、初始数据

    @Test
    	public void test01() {
    		Session session = null;
    
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			session.beginTransaction();
    
    			for (int i = 0; i < 100; i++) {
    				book entity = new book();
    				entity.setBookCode("book-code-" + i);
    				entity.setBookname("book-name" + i);
    				entity.setPrice(i);
    				session.save(entity);
    			}
    			session.getTransaction().commit();
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    2、查询对象

    @Test
    	public void test02() {
    		Session session = null;
    		// form 对象名 非 表名
    		String sql = " from book ";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    			List<book> list = (List<book>) query.list();
    
    			for (book b : list) {
    				System.out.println(b);
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    3、查询单字段

    @Test
    	public void test03() {
    		/*
    		 * 查询单字段
    		 */
    		Session session = null;
    
    		String sql = "  select bookname from book ";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    			List<String> list = (List<String>) query.list();
    
    			for (String b : list) {
    				System.out.println(b);
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    4、查询指定字段

    	@Test
    	public void test04() {
    		/*
    		 * 查询多字段,且指定条数Object[]
    		 */
    		Session session = null;
    
    		String sql = "  select   bookname,bookCode from book ";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    			// 指定5条
    			query.setMaxResults(5);
    			List<Object[]> list = (List<Object[]>) query.list();
    
    			for (Object[] b : list) {
    				System.out.println(b[0] + "," + b[1]);
    
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    5、返回Map对象

    @Test
    	public void test05() {
    		/*
    		 * 查询多字段,且指定Map
    		 */
    		Session session = null;
    
    		String sql = "  select   new Map(bookname,bookCode) from book ";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    
    			query.setMaxResults(5);
    			List<Map> list = (List<Map>) query.list();
    
    			for (Map b : list) {
    
    				System.out.println((String) b.get("0") + ","
    						+ (String) b.get("1"));
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    6、序列化参数

       6.1、

    @Test
    	public void test06() {
    
    		Session session = null;
    
    		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    			query.setInteger("book_id", 5);
    			query.setDouble("price", 5);
    			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as
    			// col_1_0_ from t_book book0_ where book0_.book_id>? and
    			// book0_.price>?
    			List<Object[]> list = (List<Object[]>) query.list();
    
    			for (Object[] b : list) {
    				System.out.println(b[0] + "," + b[1]);
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

       6.2、

    @Test
    	public void test07() {
    
    		Session session = null;
    
    		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			Query query = session.createQuery(sql);
    			query.setInteger(0, 5);
    			query.setDouble(1, 5);
    			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as
    			// col_1_0_ from t_book book0_ where book0_.book_id>? and
    			// book0_.price>?
    			List<Object[]> list = (List<Object[]>) query.list();
    
    			for (Object[] b : list) {
    				System.out.println(b[0] + "," + b[1]);
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    

      

    7、执行SQL

    @Test
    	public void test08_jdbc() {
    		Session session = null;
    //Rhythmk 
    		String sql = "  select bookCode,bookname from t_book  where  book_id>? and price>?";
    		try {
    			session = HibernateUtil.getSessionFactory().openSession();
    
    			SQLQuery query = session.createSQLQuery(sql);
    			query.setInteger(0, 5);
    			query.setDouble(1, 5);
    
    			List<Object[]> list = (List<Object[]>) query.list();
    			for (Object[] b : list) {
    				System.out.println(b[0] + "," + b[1]);
    			}
    
    		} catch (HibernateException e) {
    
    			e.printStackTrace();
    		} finally {
    			if (session != null)
    				session.close();
    		}
    	}
    
    }
    

      http://pan.baidu.com/s/1hqGDNq0

  • 相关阅读:
    ld: cannot find lgcc_s
    Ubuntu 12.10 图形显示未知
    awk分析Nginx日志中的cookie
    Ubuntu 10.04 WPS 问题汇总
    objc[20556]:Class JavaLaunchHelper is implemented in both xxx 警告处理
    在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )
    Spring Boot 系列(二)单元测试&网络请求
    Java自定义注解
    Spring Boot 系列(一)快速入门
    Spring 自定义注解,配置简单日志注解
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3722043.html
Copyright © 2020-2023  润新知