使用SpringBoot實現登錄注冊的幾個問題


一、用戶名密碼都正確的情況下被登錄攔截器攔截

 

 

 

控制台報錯:org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.spbt.mapper.EmpeeMapper.selectName'.  It's likely that neither a Result Type nor a Result Map was specified.

這個異常是在mapper文件的<select>標簽中沒有指定 resultType 或者 resultMap,也就是說沒有指定返回值類型或者返回值類型的map集合

所以檢查自己的mapper文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spbt.mapper.EmpeeMapper">

<select id="selectName" parameterType="String">
select username from empee where username=#{username}
</select>

<select id="selectPwdByName" parameterType="String">
select password from empee where username=#{username}
</select>

</mapper>

可以發現我的<select>標簽中沒有指定 resultType,而是指定的
parameterType(參數類型)

解決:將parameterType修改為resultType
登錄成功:



二、頁面沒有顯示傳遞的消息
這是LoginController的實現登錄代碼
@RequestMapping("/empee/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model, HttpSession session){
if (empeeMapper.selectName(username)!=null){
//用戶名存在
if (empeeMapper.selectPwdByName(username).equals(password)){
//密碼也正確
session.setAttribute("loginEmpee",username);
return "redirect:/main.html";
}else {
model.addAttribute("msg","密碼錯誤");
return "redirect:/index";
}
}else {
model.addAttribute("msg","用戶名不存在,請注冊");
return "redirect:/index";
}
}

我輸入錯誤的信息,而點擊登錄之后應該會反饋給頁面

解決:這其實是一個非常低級的錯誤,因為我設置了重定向redirect:/index,所以重新定回這個頁面當然就沒有反饋信息了,去掉多余的redirect:/就可以了


三、添加(注冊)成功卻跳轉到空白頁

 

 
        

 點擊添加,出現空白頁

但是查看自己的數據庫發現注冊是成功的

 

 

 

 控制台報錯:org.apache.ibatis.binding.BindingException: Mapper method 'com.spbt.mapper.EmpeeMapper.insertEmpee' has an unsupported return type: class com.spbt.pojo.Empee

 可以發現出錯原因在於EmpeeMapper文件的insertEmpee方法,而且是返回值類型的錯誤

檢查EmpeeMapper:

 

 我使用的是Empee類型的返回值類型,而我的Controller文件中並沒有寫他的返回值

 

 解決方法1:把EmpeeMapper文件的insertEmpee方法修改為void類型,因為在EmpeeMapper.xml文件中我的這個方法沒有聲明返回值

 

解決方法2:在EmpeeMapper.xml文件中為這個方法添加一個該類型(Empee)的返回值  resultType="Empee"

添加成功並且成功跳轉

我還在網上看見另一種解決方法,是修改為int類型,經過驗證確實可行
由於之后沒有進行其他操作,因此不清楚這樣做會對之后的操作是否有影響

最后推薦一個非常不錯的SpringBoot學習視頻: https://www.bilibili.com/video/av75233634
這是講師的博客: https://www.cnblogs.com/hellokuangshen


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM