spring boot 判斷賬號密碼進行頁面跳轉
阿新 • • 發佈:2018-11-12
首先:controller
本文程式碼:https://download.csdn.net/download/qq_40979622/10710377
資料庫在文章最後有提到。
@RequestMapping("/") public ModelAndView index(){ ModelAndView mv = new ModelAndView("login"); return mv; } //這裡的思路是從前臺獲取的資料引數 直接傳到findByNameAndAge中,作為資料庫的查詢條件,如果查不出來,就是空,反之就可以查出來資料,查出來的資料的SIZE()就有長度,這樣作為判斷的思路。 @PostMapping("/login") public ModelAndView Login(@RequestParam("username") String username, @RequestParam("userpassword") String userpassword){ System.out.println(username+":"+userpassword); List<User> users= userService.findByNameAndAge(username,Integer.parseInt(userpassword)); // for (Login l:loginService.findAll()) { // System.out.println(l.getUsername()+":"+l.getUserpassword()); // } ModelAndView success = new ModelAndView(); if(users.size()>0) success.setViewName("success"); else success.setViewName("404"); return success; } 其次:respository
//這裡要注意@Query的格式問題。
@Query(value = "select * from User as u where u.name=?1 and u.age=?2",nativeQuery = true)
List<User> findByNameAndAge(String name, Integer age);
service 中的和之前我寫的增刪該查一樣。可以看之前發的。我還是寫上吧。
下面是service 中的介面
List<User> findByNameAndAge(String name,Integer age);
下面是service裡面的impl.
@Override
public Login findByUsernameAndUserpassword(String username, String userpassword) {
return loginRepository.findByUsernameAndUserpassword(username,userpassword);
}
這裡的資料庫
前臺: 下面是service裡面的impl.
本人之前除錯了好多次,總結下,第一:
這裡用的是集合獲取資料庫中的使用者賬號密碼,所以程式碼全程的都要注意用List<>集合。
第二:在設定資料庫屬性的時候,一定要注意資料庫的配置,樓主就是因為資料庫設定的時候複製之前的資料庫屬性,導致資料庫連結到別的地方,導致失敗。