1 package com.bie.dao.impl;
2
3 import java.util.List;
4
5 import org.hibernate.Query;
6 import org.hibernate.Session;
7 import org.hibernate.Transaction;
8
9 import com.bie.dao.UserDao;
10 import com.bie.po.User;
11 import com.bie.utils.HibernateUtils;
12
13 /**
14 * @author BieHongLi
15 * @version 創(chuàng)建時(shí)間:2017年3月10日 下午1:35:27
16 *
17 */
18 public class UserDaoImpl implements UserDao{
19
20 @Override
21 public void insertUser(User user) {
22 Session session=null;
23 Transaction tx=null;
24 try {
25 //獲取session
26 session=HibernateUtils.getSession();
27 //開啟事務(wù)
28 tx=session.beginTransaction();
29 //插入即保存
30 session.save(user);
31 } catch (Exception e) {
32 throw new RuntimeException(e);
33 }finally{
34 //提交事務(wù)
35 tx.commit();
36 //關(guān)閉session
37 session.close();
38 }
39
40 }
41
42 @Override
43 public void updateUser(User user) {
44 Session session=null;
45 Transaction tx=null;
46 try {
47 session=HibernateUtils.getSession();
48 tx=session.beginTransaction();
49 session.update(user);
50 } catch (Exception e) {
51 throw new RuntimeException(e);
52 }finally{
53 //提交事務(wù)
54 tx.commit();
55 //關(guān)閉事務(wù)
56 session.close();
57 }
58
59 }
60
61 @Override
62 public User selectUserId(int id) {
63 //獲取session
64 Session session=null;
65 //事務(wù)
66 Transaction tx=null;
67 try {
68 //獲取session
69 session=HibernateUtils.getSession();
70 //開啟事務(wù)
71 tx=session.beginTransaction();
72
73 return (User)session.get(User.class, id);
74 } catch (Exception e) {
75 throw new RuntimeException(e);
76 }finally{
77 //提交事務(wù)
78 tx.commit();
79 //關(guān)閉session
80 session.close();
81 }
82 }
83
84 @Override
85 public List<User> selectAll() {
86 //session
87 Session session=null;
88 //事務(wù)
89 Transaction tx=null;
90 try {
91 //獲取session
92 session=HibernateUtils.getSession();
93 //開啟事務(wù)
94 tx=session.beginTransaction();
95 //HQL語句
96 Query q=session.createQuery("from User");
97
98 List<User> list=q.list();
99 return list;
100 } catch (Exception e) {
101 throw new RuntimeException(e);
102 }finally{
103 //提交事務(wù)
104 tx.commit();
105 //關(guān)閉session
106 session.close();
107 }
108 }
109
110 @Override
111 public List<User> selectAll(String name) {
112 //session
113 Session session=null;
114 //事務(wù)
115 Transaction tx=null;
116 try {
117 //獲取session
118 session=HibernateUtils.getSession();
119 //開啟事務(wù)
120 tx=session.beginTransaction();
121 //HQL語句
122 Query q = session.createQuery("from User user where user.name=? ");
123 //注意:參數(shù)索引從0開始
124 q.setString(0, name);
125
126 return q.list();
127 } catch (Exception e) {
128 throw new RuntimeException();
129 }finally{
130 //提交事務(wù)
131 tx.commit();
132 //關(guān)閉session
133 session.close();
134 }
135 }
136
137 @Override
138 public List<User> selectPage(int index, int count) {
139 //session
140 Session session=null;
141 //事務(wù)
142 Transaction tx=null;
143 try {
144 //獲取session
145 session=HibernateUtils.getSession();
146 //開啟事務(wù)
147 tx=session.beginTransaction();
148 //HQL查詢
149 Query q = session.createQuery("from User ");
150 //設(shè)置分頁參數(shù)
151 q.setFirstResult(index);//查詢起始行
152 q.setMaxResults(count);//查詢返回的行數(shù)
153
154 return q.list();
155 } catch (Exception e) {
156 throw new RuntimeException();
157 }finally{
158 //提交事務(wù)
159 tx.commit();
160 //關(guān)閉session
161 session.close();
162 }
163 }
164
165 @Override
166 public void deleteUser(int id) {
167 Session session=null;
168 Transaction tx=null;
169 try {
170 session=HibernateUtils.getSession();
171 tx=session.beginTransaction();
172 //先根據(jù)id查詢出用戶的信息再刪除
173 Object obj=session.get(User.class, id);
174 if(obj!=null){
175 session.delete(obj);
176 }
177 } catch (Exception e) {
178 throw new RuntimeException();
179 }finally{
180 //提交事務(wù)
181 tx.commit();
182 //關(guān)閉session
183 session.close();
184 }
185 }
186
187
188 }
8:后一步,完成測試。源碼如下所示:
1 package com.bie.test;
2
3 import java.util.List;
4
5 import org.junit.Test;
6
7 import com.bie.dao.UserDao;
8 import com.bie.dao.impl.UserDaoImpl;
9 import com.bie.po.User;
10
11 /**
12 * @author BieHongLi
13 * @version 創(chuàng)建時(shí)間:2017年3月10日 下午2:34:48
14 *
15 */
16 public class UserCRUD {
17
18 private UserDao dao=new UserDaoImpl();
19
20 @Test
21 public void insertUser(){
22 dao.insertUser(new User("曹操","111","111111","
111111@163.com"));
23
24 }
25
26 @Test
27 public void updateUser(){
28 User user=new User();
29 user.setId(1);
30 user.setName("劉備222");
31
32 dao.updateUser(user);
33 }
34
35 @Test
36 public void deleteUser(){
37 dao.deleteUser(30);
38 }
39
40 @Test
41 public void selectUserId(){
42 //根據(jù)用戶的編號(hào)進(jìn)行查詢
43 User user=dao.selectUserId(1);
44 System.out.println(user);
45 }
46
47 @Test
48 public void selectUser(){
49 //查詢用戶的所有信息
50 List<User> list = dao.selectAll();
51 System.out.println(list);
52 }
53
54 @Test
55 public void selectUserName(){
56 //根據(jù)用戶姓名查詢信息
57 List<User> list=dao.selectAll("李四");
58 System.out.println(list);
59 }
60
61 @Test
62 public void selectPage(){
63 //分頁查詢,第一個(gè)參數(shù)是起始行,第二個(gè)參數(shù)是每頁的個(gè)數(shù)
64 List<User> list=dao.selectPage(0, 5);
65 System.out.println(list);
66 }
67 }
運(yùn)行效果如下所示: