我试图通过使用Spring MVC和Hibernate从数据库中检查其值来验证这是否是注册用户。我真的不知道如何做到这一点我搜索了很多网站,但没有得到解决方案。
我的Bean类是
package net.codejava.spring.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "USERS")
public class User {
private int id;
private String username;
private String password;
private String email;
private String phno;
private String address;
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的Dao班是
package net.codejava.spring.dao;
import java.util.List;
import net.codejava.spring.model.User;
public interface UserDAO {
public List<User> list();
public User get(int id);
public void saveOrUpdate(User user);
public void delete(int id);
}
我的DaoImpl课程是
package net.codejava.spring.dao;
import java.util.List;
import net.codejava.spring.model.User;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class UserDAOImpl implements UserDAO {
@Autowired
private SessionFactory sessionFactory;
public UserDAOImpl() {
}
public UserDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<User> list() {
@SuppressWarnings("unchecked")
List<User> listUser = (List<User>) sessionFactory.getCurrentSession()
.createCriteria(User.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return listUser;
}
@Override
@Transactional
public void saveOrUpdate(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
@Override
@Transactional
public void delete(int id) {
User userToDelete = new User();
userToDelete.setId(id);
sessionFactory.getCurrentSession().delete(userToDelete);
}
@Override
@Transactional
public User get(int id) {
String hql = "from User where id=" + id;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<User> listUser = (List<User>) query.list();
if (listUser != null && !listUser.isEmpty()) {
return listUser.get(0);
}
return null;
}
}
我的主控制器类给出用户列表的方法是
@RequestMapping("/list")
public ModelAndView handleRequest() throws Exception {
List<User> listUsers = userDao.list();
ModelAndView model = new ModelAndView("UserList");
model.addObject("userList", listUsers);
return model;
}
这里我想要一个方法,它为我提供了用户名和密码,以便我可以验证它们。请原谅我,但我搜索过没找到任何好的解决方案