gpt4 book ai didi

java - 在 Spring MVC hibernate 中验证用户名和密码

转载 作者:行者123 更新时间:2023-11-30 10:02:14 26 4
gpt4 key购买 nike

我有这段 hibernateTemplate 代码来验证用户名和密码,但这段代码在 MYSQL 上运行良好,但在 Oracle 中失败,并且在创建表但未保存值时在 DAO 类中显示出一些问题。对于项目来说,它是必须使用 Oracle

package com.infotech.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.infotech.dao.StudentDAO;
import com.infotech.model.Student;

@Repository("studentDAO")
public class StudentDAOImpl implements StudentDAO {

@Autowired
private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Override
public boolean saveStudent(Student student) {
int id = (Integer)hibernateTemplate.save(student);
if(id>0)
return true;
return false;
}
@SuppressWarnings("unchecked")
@Override
public Student getStudentDetailsByEmailAndPassword(String email,String password){
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Student.class);
detachedCriteria.add(Restrictions.eq("email", email));
detachedCriteria.add(Restrictions.eq("password", password));
List<Student> findByCriteria = (List<Student>) hibernateTemplate.findByCriteria(detachedCriteria);
if(findByCriteria !=null && findByCriteria.size()>0)
return findByCriteria.get(0);
else
return null;
}
}

**现在我再次更改了 DAO 类而不是 HibernateTemplate 我使用了 session 接口(interface),现在值保存在数据库中但是用户名和密码的验证在 Hibernate 模板中没有发生一些问题。请建议更改而不是 hibernate 模板我应该用什么

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.infotech.dao.StudentCredentialDao;
import com.infotech.model.Student;
import com.infotech.model.StudentCredential;

@Repository("StudentCredentialDAO")
public class StudentCredentialDaoImpl implements StudentCredentialDao {

@Autowired
private SessionFactory sessionFactory;
@Autowired
private HibernateTemplate hibernateTemplate;


public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public boolean saveStudentCred(StudentCredential stuCred) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(stuCred);
tx.commit();
session.close();
return true;
}
@SuppressWarnings("unchecked")
@Override//This method is creating problem as I need to use something else other than Hibernate Template
public StudentCredential getStudentDetailsByUnameAndPassword(String uname,String password){
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(StudentCredential.class);
detachedCriteria.add(Restrictions.eq("uname", uname));
detachedCriteria.add(Restrictions.eq("password", password));
List<StudentCredential> findByCriteria = (List<StudentCredential>)hibernateTemplate.findByCriteria(detachedCriteria);
if(findByCriteria !=null && Hibernate template .size()>0)
return findByCriteria.get(0);
else
return null;
}
}

最佳答案

@Repository
public class LoginDAOImpl implements LoginDAO {

@Autowired
private SessionFactory sessionFactory;

@Override
public String loginCheck(String customerID, String password) {

Session currentSession = sessionFactory.getCurrentSession();

Query theQuery = currentSession.createQuery("from UserAccount u where u.customerID=:id AND u.password=:pass");
theQuery.setParameter("id", customerID);
theQuery.setParameter("pass", password);

List results = theQuery.list();

if ((results!=null) && (results.size()>0)){
return "success";
}
else {
return "failed";
}

}

你可以这样使用

关于java - 在 Spring MVC hibernate 中验证用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com