首页 > 解决方案 > 检索特定 id 上的特定记录信息

问题描述

    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;
    }

图 1 显示了将输入发送到控制器方法的平衡表

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

图2

在此处输入图像描述

标签: javaspringhibernatespring-boot

解决方案


您可以使用@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
            }

推荐阅读