-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDAOImpl.java
More file actions
49 lines (38 loc) · 1.42 KB
/
UserDAOImpl.java
File metadata and controls
49 lines (38 loc) · 1.42 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package AlexTest.dao;
import AlexTest.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
public void addUser(User user) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(user);
}
public void removeUser(Integer id) {
User user = sessionFactory.getCurrentSession().load(User.class, id);
if (null != user) {
sessionFactory.getCurrentSession().delete(user);
}
}
public void updateUser(User user) {
sessionFactory.getCurrentSession().update(user);
}
public User getUserById(Integer id) {
return this.sessionFactory.getCurrentSession().load(User.class, id);
}
@SuppressWarnings("unchecked")
public List<User> search(String searchText) {
Session session = sessionFactory.getCurrentSession();
if (searchText == null || searchText.isEmpty()) {
return session.createQuery("from User").list();
}
String query = "select * from user where name like '%"+searchText+"%'";
return sessionFactory.getCurrentSession().createSQLQuery(query).addEntity(User.class).list();
}
}