-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathuserDao.java
More file actions
84 lines (70 loc) · 2.41 KB
/
userDao.java
File metadata and controls
84 lines (70 loc) · 2.41 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.jtspringproject.JtSpringProject.dao;
import java.util.List;
import javax.persistence.NoResultException;
import javax.sound.midi.Soundbank;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.jtspringproject.JtSpringProject.models.User;
@Repository
public class userDao {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
@Transactional
public List<User> getAllUser() {
Session session = this.sessionFactory.getCurrentSession();
List<User> userList = session.createQuery("from CUSTOMER").list();
return userList;
}
@Transactional
public User saveUser(User user) {
this.sessionFactory.getCurrentSession().saveOrUpdate(user);
System.out.println("User added" + user.getId());
return user;
}
// public User checkLogin() {
// this.sessionFactory.getCurrentSession().
// }
@Transactional
public User getUser(String username,String password) {
Query query = sessionFactory.getCurrentSession()
.createQuery("from User where username = :username");
query.setParameter("username",username);
try {
User user = (User) query.getSingleResult();
System.out.println(user.getPassword());
if(password.equals(user.getPassword())) {
return user;
}else {
return new User();
}
}catch(Exception e){
System.out.println(e.getMessage());
User user = new User();
return user;
}
}
@Transactional
public boolean userExists(String username) {
Query query = sessionFactory.getCurrentSession().createQuery("from User where username = :username");
query.setParameter("username",username);
return !query.getResultList().isEmpty();
}
@Transactional
public User getUserByUsername(String username) {
Query<User> query = sessionFactory.getCurrentSession().createQuery("from User where username = :username", User.class);
query.setParameter("username", username);
try {
return query.getSingleResult();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
}