检索有关特定ID的特定记录信息

时间:2018-06-27 11:04:12

标签: java spring hibernate spring-boot

    NewAccountDAOImpl 
    it is getting particular record id, username and password
  according to that it should retrieve the records

    @Transactional
        @Modifying
        public boolean checkLogin(int id, String username, String password){
            System.out.println("In Check login"+ id);
            System.out.println("In Check login"+ username);
            System.out.println("In Check login"+ password);

            Session session = sessionFactory.openSession();
            boolean userFound = false;
            //Query using Hibernate Query Language
        //tring SQL_QUERY =" from NewAccount as n where n.id=? and n.username=? and password=?";

            String SQL_Query=" from NewAccount where id=:id";
            Query query = session.createQuery(SQL_Query);
            query.setParameter("id",id).uniqueResult();
                //query.setParameter(0,id);
                //query.setParameter(1,username);
                //query.setParameter(2,password);
            List list = query.list();

            if ((list != null) && (list.size() > 0)) {
                userFound= true;
            }

            session.close();
            return userFound;              
       }

控制器类

从银行余额表格中获取信息,例如ID,用户名,密码。 我将它们添加到checkLogin方法参数,它返回布尔值

@RequestMapping(value = "/balanceSave", method = RequestMethod.GET)
    public ModelAndView saveBk(ModelAndView model, HttpServletRequest req,
            HttpServletResponse res, @ModelAttribute NewAccount newaccount) {

    int id=Integer.parseInt(req.getParameter("id"));
        String username=req.getParameter("username");
        String password=req.getParameter("password");

        boolean userExists  = newaccountService.checkLogin( id, username, password);


        if(userExists ){


          model.addObject("newaccount", newaccount);
        return new ModelAndView("redirect:viewBalanceMoney");
    }

    return new ModelAndView("BalanceForm");

}

here i am sending list data to a jsp page viewbalanc

//查看新帐户余额

 @RequestMapping(value = "/viewBalanceMoney", method = RequestMethod.GET)
    public ModelAndView viewBalanceMoney(ModelAndView model) {

        //  public NewAccount getNewAccount(int newaccountid);

          List<NewAccount> listnewaccount = newaccountService.getAllNewAccounts();
          model.addObject("listnewaccount", listnewaccount);
          model.setViewName("viewBalanc");
          return model;
    }

image1显示余额表    它将输入发送到控制器方法

图2显示了检索到的记录,但是我需要特定的id记录信息

image2

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用@PathVariable并为该accountId调用方法。

  @RequestMapping(value = "/viewBalanceMoney/{newAccountId}", method = RequestMethod.GET)
            public ModelAndView viewBalanceMoney(@PathVariable("newAccountId") Integer newaccountid,
 ModelAndView model) {

               //write code for fetching data for newaccountid
            }