Rhythmk 学习 Hibernate 09 - Hibernate HQL
1、初始数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
@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、查询对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
@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、查询单字段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
@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、查询指定字段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
@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对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
@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、
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
@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、
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 |
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
@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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。