springBoot+MyBatis实现简单的登录

前言

使用工具:idea、MySQL
技术:spring Boot

创建数据库及测试表

-- 创建数据库
CREATE DATABASE IF NOT EXISTS shoop DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 创建一个测试用的数据表
create table test_user(
  userId int auto_increment primary key,
  userName varchar(200),
  userPass varchar(200)
)

实体类(entity)

public class Users {
    private Integer userId;
    private String userName;
    private String userPass;
    //省略Getter和Setter方法
    }

接口(mapper)

@Mapper
public interface UsersMapper {
    //因为用户是唯一的,所以可以直接使用实体类并不用List<实体类>这种
    public Users select(Users users);
}

myBatis配置文件(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.springboot.mapper.UsersMapper">

    <select id="select" resultType="com.springboot.entity.Users">
        select userName,userPass from test_user where userName=#{userName} and userPass=#{userPass}
    </select>

</mapper>

业务层(service)

public interface IUsersService {
    //因为用户是唯一的,所以可以直接使用实体类并不用List<实体类>这种
    public Users select(Users users);
}

业务实现层(impl)

@Service
public class UserServiceImpl implements IUsersService {
    @Resource
    UsersMapper mapper;
    @Override
    public Users select(Users users) {
        return mapper.select(users);
    }
}

逻辑控制层(controller)

@RequestMapping("/Users")
@Controller
public class UsersController {
    @Resource
    IUsersService service;

    @RequestMapping("login")
    public String login(){
        return "/admin/login";
    }
    @RequestMapping("/select")
    public String select(Users users){
        Users usersList = service.select(users);
        if(usersList != null ) {
            return "/admin/welcome";
        }
        return "/admin/login";
    }

}

登录页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<style>
    #login{
        position: relative;
        top:200px;
        left: 600px;
    }
</style>
<body>
<form action="/Users/select" method="post">
<div id="login">
    账户:<input type="text" name="userName">
    <br/>
    密码:<input type="text" name="userPass">
    <br/>
    <input type="submit" value="登录">
</div>
</form>
</div>
</body>
</html>

以上就是用户登录的一个简单实例,项目源码https://pan.baidu.com/s/1Suc6iYCP7vOWXXAU-vupiw提取码:45vr

发布了22 篇原创文章 · 获赞 2 · 访问量 1137

猜你喜欢

转载自blog.csdn.net/javaasd/article/details/105167377