springboot+mybatis写一个登录注册页面

目录

1.准备一个数据库

2.创建一个springboot工程(勾选所需依赖)

 3.创建实体类

3.在resources中创建mapper 文件夹,UserMapper.xml操作数据库

 4.创建UserMapper映射接口

5.创建UserService类进行处理

 6.创建控制类UserController类

 html


1.准备一个数据库

 

2.创建一个springboot工程(勾选所需依赖)

 3.创建实体类

3.在resources中创建mapper 文件夹,UserMapper.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.UserMapper">

<!--    根据用户名查找-->
    <select id="getByUsername" resultType="com.example.demo.entity.User">
        SELECT * FROM user WHERE username = #{username}
    </select>

<!--    注册-->
    <insert id="addUser">
        insert into user values (#{username},#{password})
    </insert>
</mapper>

 4.创建UserMapper映射接口

 

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    //根据username查询数据
    User getByUsername(String username);

    //注册
    Boolean addUser(User user);
}

5.创建UserService类进行处理

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    //根据username查询用户数据
    public User getByUsername(String username){
        return userMapper.getByUsername(username);
    }

    //增加用户
    public boolean addUser(User user){
        return userMapper.addUser(user);
    }

    //登陆处理
    public boolean login(User user){
        String name = user.getUsername();   //将输入的用户名密码分别赋值
        String psd = user.getPassword();
        User user1 = userMapper.getByUsername(name);
        if (user1 != null) {     //如果查询结果不为空
            if (user1.getPassword().equals(psd)) {       //密码比较
                return true;
            } else {
                return false;
            }
        }
            return false;
    }
}

 6.创建控制类UserController类

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    //根据username查询用户信息
    @RequestMapping("/getByUsername/{username}")
    public User getByUsername(@PathVariable String username){
        System.out.println(username);
        return userService.getByUsername(username);
    }

    //增加用户
    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println(user);
        if (userService.addUser(user)){
            return "注册成功";
        }else {
            return "注册失败";
        }
    }

    @RequestMapping("/login")
    public String loginin(User user){
        System.out.println(userService.login(user));
        if (userService.login(user)){
            return "登陆成功";
        }else {
            return "登陆失败";
        }
    }



}

 html

登陆页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function zhuce(){
            window.location.href="http://localhost:8080/zhuce.html";
        }
    </script>
</head>
<body>
    <form action="http://localhost:8080/login" method="post">
        用户名:<input id="username" name="username">
        密码:<input id="password" type="password" name="password">
        <input type="submit" value="登录">
    </form>
    <button onclick="zhuce()">注册</button>
</body>
</html>

注册页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
    <h1>注册页面</h1>
    <form method="post" action="http://localhost:8080/addUser">
        用户名:<input id="username" name="username">
        密码:<input name="password">
        <input type="submit">
    </form>
</body>
</html>
  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两年半的个人练习生^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值