休眠选择查询要选择两个参数?

时间:2019-03-06 18:26:51

标签: hibernate hql hibernate-criteria hibernate-query

HTML

<form class="login100-form validate-form" action="ActionController" method="post">
                    <span class="login100-form-title p-b-34">
                        Account Login
                    </span>

                    <div class="wrap-input100 rs1-wrap-input100 validate-input m-b-20" data-validate="Type Email Address">
                        <input class="input100" type="email" name="txtEmail" placeholder="User name">
                        <span class="focus-input100"></span>
                    </div>
                    <div class="wrap-input100 rs2-wrap-input100 validate-input m-b-20" data-validate="Type password">
                        <input class="input100" type="password" name="txtPassword" placeholder="Password">
                        <span class="focus-input100"></span>
                    </div>

                    <div class="container-login100-form-btn">
                        <input class="login100-form-btn" value="Signin" type="submit" name="action">
                    </div>

                    <div class="w-full text-center p-t-27 p-b-239">
                        <span class="txt1">
                            Forgot
                        </span>

                        <a href="forgetpass.jsp" class="txt2">
                         password?
                        </a>
                    </div>

                    <div class="w-full text-center">
                        <a href="SignUp.html" class="txt3">
                            Sign Up
                        </a>
                    </div>
                </form>

ActionController

else if(action.equalsIgnoreCase("signin"))
        {
            String email = request.getParameter("txtEmail");
            String pass = request.getParameter("txtPassword");

            UserDao udao = new UserDao();
            udao.UserLogin(email, pass);
            if(udao==null)
            {
                out.println("<script type=\"text/javascript\">");
                out.println("alert('Wrong Email Or Password');");
                out.println("</script>");

                response.sendRedirect("SignLog.html");
            }
            else {
                HttpSession session = request.getSession();
                session.setAttribute("uemail", email);
                request.getRequestDispatcher("index.html").forward(request, response);
            }

        }

道课

public UserBean UserLogin(String useremail, String password)
    {
        System.out.println("Enter Dao");

        Session session = SiteUtil.createSession();
        Transaction tr = session.beginTransaction();

        System.out.println("Query Processing");

        String sql_query = "FROM UserBean U WHERE U.email = :uemail AND U.password = :upassword";
        Query query = session.createQuery(sql_query);
        query.setParameter("uemail", useremail);
        query.setParameter("upassword", password);

        return (UserBean) query.uniqueResult();
    }

即使我输入了错误的电子邮件和密码,它也会重定向到索引页面。

控制台
查询处理 休眠:     选择         userbean0_.id为id1_0_,         userbean0_.Email作为Email2_0_,         userbean0_.Password作为Password3_0_     从         用户注册userbean0_     哪里         userbean0_.Email =?         和userbean0_.Password =?

1 个答案:

答案 0 :(得分:0)

根据您的代码:

String sql_query = "FROM UserBean U WHERE U.email = :uemail AND U.password = :upassword";
        Query query = session.createQuery(sql_query);
        query.setParameter("uemail", useremail);
        query.setParameter("upassword", password);

        return (UserBean) query.list();

我认为您可以检查query.list()的大小,还可以在通过UserBean类进行转换后检查一些数据。

您的代码会这样:

UserBean userBean =(UserBean)query.list();

现在您可以检查 userBean

中某些数据的空值

更新

您可以使用下面的程序。

 import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.CrudRepository;

    /**
     * Created by skarim on 3/7/19.
     */
    public interface UserRepo extends CrudRepository<UserBean,Integer>{

        @Query("SELECT U FROM UserBean U WHERE U.email = ?1 AND U.password = ?2")
        UserBean findUserByEmailAndPassword(String email,String passWord);

    }

在这里,UserBean是您的实体类。假设它具有ID,电子邮件和密码字段。

您的查询就像:SELECT U FROM UserBean U WHERE U.email = ?1 AND U.password = ?2

在此,函数findUserByEmailAndPassword中的电子邮件是第一个参数,密码是第二个参数

现在,您可以创建UserRepo的实例,并使用适当的值调用函数findUserByEmailAndPassword

@Autowired
UserRepo userRepo;
UserBean user=userRepo.findUserByEmailAndPassword("some@email.com","1234");

现在您对用户有价值。

有关更多信息,您可以检查this linkthis link

希望这会对您有所帮助。

谢谢:)