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 创建时间: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 //开启事务
28 tx=session.beginTransaction();
29 //插入即保存
30 session.save(user);
31 } catch (Exception e) {
32 throw new RuntimeException(e);
33 }finally{
34 //提交事务
35 tx.commit();
36 //关闭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 //提交事务
54 tx.commit();
55 //关闭事务
56 session.close();
57 }
58
59 }
60
61 @Override
62 public User selectUserId(int id) {
63 //获取session
64 Session session=null;
65 //事务
66 Transaction tx=null;
67 try {
68 //获取session
69 session=HibernateUtils.getSession();
70 //开启事务
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 //提交事务
78 tx.commit();
79 //关闭session
80 session.close();
81 }
82 }
83
84 @Override
85 public List<User> selectAll() {
86 //session
87 Session session=null;
88 //事务
89 Transaction tx=null;
90 try {
91 //获取session
92 session=HibernateUtils.getSession();
93 //开启事务
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 //提交事务
104 tx.commit();
105 //关闭session
106 session.close();
107 }
108 }
109
110 @Override
111 public List<User> selectAll(String name) {
112 //session
113 Session session=null;
114 //事务
115 Transaction tx=null;
116 try {
117 //获取session
118 session=HibernateUtils.getSession();
119 //开启事务
120 tx=session.beginTransaction();
121 //HQL语句
122 Query q = session.createQuery("from User user where user.name=? ");
123 //注意:参数索引从0开始
124 q.setString(0, name);
125
126 return q.list();
127 } catch (Exception e) {
128 throw new RuntimeException();
129 }finally{
130 //提交事务
131 tx.commit();
132 //关闭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 //事务
142 Transaction tx=null;
143 try {
144 //获取session
145 session=HibernateUtils.getSession();
146 //开启事务
147 tx=session.beginTransaction();
148 //HQL查询
149 Query q = session.createQuery("from User ");
150 //设置分页参数
151 q.setFirstResult(index);//查询起始行
152 q.setMaxResults(count);//查询返回的行数
153
154 return q.list();
155 } catch (Exception e) {
156 throw new RuntimeException();
157 }finally{
158 //提交事务
159 tx.commit();
160 //关闭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 //先根据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 //提交事务
181 tx.commit();
182 //关闭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 创建时间: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 //根据用户的编号进行查询
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 //根据用户姓名查询信息
57 List<User> list=dao.selectAll("李四");
58 System.out.println(list);
59 }
60
61 @Test
62 public void selectPage(){
63 //分页查询,第一个参数是起始行,第二个参数是每页的个数
64 List<User> list=dao.selectPage(0, 5);
65 System.out.println(list);
66 }
67 }
运行效果如下所示: