SpringBoot 集成 Mybatis 实现用户登录
SpringBoot 集成 Mybatis 实现用户登录
这里我们书写这个的意义不仅是为了实现简单的用户登录功能, 而是为了了解 SpringBoot - MVC 设计模式的基本使用流程。 我们可以通过对用户登录一个功能的学习, 而拓展到各种常见功能如 CRUD 的操作。
① 引入 mybatis 依赖
引入 mysql 以及 mybatis 依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
② 创建 bean 类
Admin
package com.example.demo.bean;
public class Admin {
int id;
String username;
String password;
public Admin() {
}
public Admin(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "Admin{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
③ mapper 层
Admin.mapper
package com.example.demo.mapper;
import com.example.demo.bean.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface AdminMapper {
Admin login(Admin user);
void saveAdmin(Admin user);
void modAdmin(Admin user);
Admin findAdminById(int id);
}
AdminMapper.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="com.example.demo.mapper.AdminMapper">
<select id="login" parameterType="admin" resultType="admin">
select * from admin where username = #{username} and password = #{password};
</select>
<insert id="saveAdmin" parameterType="admin">
insert into admin(id,username,password)
values(#{id},#{username},#{password});
</insert>
<select id="findAdminById" parameterType="int" resultType="admin">
select * from admin where id = #{id};
</select>
<update id="modAdmin" parameterType="admin" >
update admin set username = #{username},password = #{password}
where id = #{id};
</update>
</mapper>
⑤ service 层
AdminService 接口
package com.example.demo.services;
import com.example.demo.bean.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
public interface AdminService {
Admin login(Admin user);
void signIn(Admin user);
void modAdmin(Admin user);
Admin findAdminById(int id);
}
AdminService 实现
package com.example.demo.services.impl;
import com.example.demo.bean.Admin;
import com.example.demo.mapper.AdminMapper;
import com.example.demo.services.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminMapper adminMapper;
public Admin login(Admin user) {
return adminMapper.login(user);
}
public void signIn(Admin user) {
adminMapper.saveAdmin(user);
}
public void modAdmin(Admin user) {
adminMapper.modAdmin(user);
}
public Admin findAdminById(int id) {
return adminMapper.findAdminById(id);
}
}
⑥ controller 层
AdminController
package com.example.demo.controller;
import com.example.demo.bean.Admin;
import com.example.demo.services.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@RequestMapping("/admin")
@Controller
public class AdminController {
@Autowired
AdminService adminService;
@RequestMapping("/login")
public String login(Admin admin, Model model){
Admin loginAdmin = adminService.login(admin);
if(loginAdmin != null){
return "dashboard";
} else {
model.addAttribute("login_msg", "用户名或密码错误!");
return "index";
}
}
}
⑦ 前端界面
index.html 集成 thymeleaf
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{asserts/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" th:action="@{/admin/login}">
<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tips}">Please sign in</h1>
<label class="sr-only" >Username</label>
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" >Password</label>
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</button>
<p class="mt-5 mb-3 text-muted">? 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
</form>
</body>
</html>
⑧ 数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`username` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`password` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
⑨ 运行结果
OVER~
|