SpringBoot:SpringBoot结合Jsp和Mybatis实现一个简单的登录

一、准备工作

  1. 先导入一些css和js文件
    SpringBoot:SpringBoot结合Jsp和Mybatis实现一个简单的登录_第1张图片
  2. 新建三个页面,登录页,失败页,成功页
    login.jsp
<html>
<head>
    <title>logintitle>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css" />
    <script src="/js/jquery-3.3.1.min.js">script>
    <script src="/js/bootstrap.js">script>
head>
<body>
    <div class="container col-xs-2 col-xs-push-4">
        <h1>Loginh1>
        <form action="/login">
            <div class="form-group">
                <label for="username">Usernamelabel>
                <input type="text" class="form-control" id="username" name="username" placeholder="Username">
            div>
            <div class="form-group">
                <label for="password">Passwordlabel>
                <input type="password" class="form-control" id="password" name="password" placeholder="Password">
            div>
            <button type="submit" class="btn btn-default">Submitbutton>
        form>
    div>
body>
html>

success.jsp

<html>
<head>
    <title>successtitle>
head>
<body>
    <h1>successh1>
body>
html>

fail.jsp

<html>
<head>
    <title>failtitle>
head>
<body>
    <h1>failh1>
body>
html>

二、开始写后台代码了

  1. User实体类
public class User {
    private Integer id;
    private String username;
    private String password;

    public Integer getId(){
        return id;
    }

    public void SetId(Integer 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;
    }
}
  1. UserMapper接口
@Mapper
public interface UserMapper {
    //根据username查找用户
    User findUser(String username);
}
  1. UserMapper.xml


<mapper namespace="com.example.demo.mapper.UserMapper">
    <select id="findUser" resultType="com.example.demo.domain.User">
        select * from tb_user where username = #{0}
    select>
mapper>
  1. UserService接口
public interface UserService {
    //根据用户名查找用户
    User getUserByName(String username);
}
  1. UserService实现类
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByName(String username) {
        return userMapper.findUser(username);
    }
}
  1. UserController
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password){
        User user = userService.getUserByName(username);
        if (user == null)
            return "fail";
        if(user.getPassword().equals(password))
            return "success";
        else
            return "fail";
    }
}

三、访问流程

用户提交表单后根据action找到对应RequestMapping中的路径,根据input标签中的name传递参数到Controller。
Controller通过Service传递username参数找到user对象。
Service通过Mapper找到对应的Mapper,Mapper通过映射找到xml执行Sql查出数据。

你可能感兴趣的:(SpringBoot,Java)