SpringBoot+MyBatis實現登錄案例

1.創建一個SpringBoot項目

2.創建表

create database login;
 
use login;
 
 
create table userlogin(
username varchar(49),
password varchar(49)
)

3.idea連接mysql

4. 創建實體類UserLogin

@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "userlogin")/*
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
*/
public class UserLogin {
    private String username;
    private String password;
}

5.創建mapper。數據持久層UserLoginMapper接口

@Mapper
@Repository
public interface UserLoginMapper {
 
    //定義增刪改查方法
 
    //查詢所有
    public List<UserLogin> queryAll();
    //添加數據
 
    public int add(UserLogin userLogin);
 
    //根據用戶名查詢
 
    public UserLogin queryByUsername(String username);
}

6.在resources下建UserLoginMapper.xml文件

<?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="自己的mapper接口">
 <select id="queryAll" resultType="自己的實體類接口">
  select * from userlogin
 </select>
 <insert id="add" parameterType="自己的實體類接口">
  insert into userlogin values (#{username},#{password})
 </insert>
 <select id="queryByUsername" resultType="自己的實體類接口">
  select * from userlogin where username=#{username}
 </select>
</mapper>

7.編寫UserLogService接口

public interface UserLonginService {
    public interface UserLoginServicesl {
        //查詢所有
        public List<UserLogin> queryAll();
        //添加數據
        public int add(UserLogin userLogin);
        //根據用戶名查詢
        public UserLogin queryByUsername(String username);
    }
}

8.編寫控制層MyController

@Controller
public class MyController {
 //controller層調用service層
 //創建一個service層對象
 @Autowired
 UserLoginServicesImpl userLoginServices;
 
 //登錄
 @RequestMapping("/toLogin")
 public String toLogin() {
  return "login";
 }
 
 @RequestMapping("/loginSuccess")
 public String loginSuccess(Model model, UserLogin userLogin) {
  //查詢用戶名是否存在
  UserLogin userLogin1 = userLoginServices.queryByUsername(userLogin.getUsername());
  if (userLogin1 != null) { //如果用戶名存在
   if (userLogin1.getPassword().equals(userLogin.getPassword())) {
    System.out.println(userLogin1.toString());
    return "success";
   } else {
    model.addAttribute("data", "密碼不正確");
    return "login";
   }
 
  } else { //用戶名不存在,直接返回到登錄頁面
   model.addAttribute("data", "該用戶不存在,請先註冊");
   return "login";
  }
 }
 
 
 //註冊
 @RequestMapping("/toRegister")
 public String toRegister() {
  return "register";
 }
 
 @RequestMapping("/toRegisterSuccess")
 public String toRegisterSuccess(Model model,UserLogin userLogin) {
  //添加一條記錄到數據庫中
  int add = userLoginServices.add(userLogin);
  System.out.println("插入數據成功");
  model.addAttribute("data","註冊成功,請登錄");
  return "login";
 
 }
}

9.前端頁面

10.login.html 登錄的頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>login</title>
</head>
<body background="back.jpg">
<div align="center">
 <br><br><h2>登錄界面</h2><br><br>
 <span th:text="${data}" style="text-color:red;font-size: 10px"></span>
 <form method="get" action="/loginSuccess">
  用戶名:<input type="text" name="username" placeholder="請輸入用戶名" required/><br><br>
  密碼:<input type="text" name="password" placeholder="請輸入密碼" required/><br><br>
  <input type="submit" value="登錄">
 </form>
 <br>
 <form method="get" action="/toRegister">
  <input type="submit" value="註冊">
 </form>
</div>
</body>
</html>

11.註冊界面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <title>register</title>
</head>
<body background="back.jpg" >
<div align="center">
 <br><br><h1>註冊界面</h1><br><br>
 <form method="get" action="/toRegisterSuccess">
  用戶名:<input type="text" name="username" placeholder="請輸入用戶名" required/><br><br>
  密碼:<input type="text" name="password" placeholder="請輸入密碼" required/><br><br>
  確認密碼:<input type="text" name="password2" placeholder="請輸入密碼" required/><br><br>
  <input type="submit" value="註冊">
 </form>
</div>
</body>
</html>

success.html:成功頁面

以上就是SpringBoot+MyBatis登錄案例的詳細內容,更多關於SpringBoot MyBatis登錄的資料請關註WalkonNet其它相關文章!

推薦閱讀: