spring-boot入门(三)——登录接口

在pow.xml文件中导入依赖



    mysql
    mysql-connector-java
    runtime




    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.0

resource下的application.yml文件添加数据库配置

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/spring_boot

文件目录

catalogue.png

mysql数据库字段
field.png

bean -> user 写完private String user_name; private String password;后鼠标右击Generate自动创建get和set

package com.example.demo.bean;

public class User {
    private String user_name;
    private String password;

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

loginController:method = RequestMethod.POST使用post请求

package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class loginController{
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/loginHandle", method = RequestMethod.POST)
    public Map loginHandle(User user, HttpSession session) {
        Map map = new HashMap<>();
        User loginUser = userService.getByUserNameAndPassword(user);
        if (loginUser != null) {
            session.setAttribute("loginUser", loginUser);
            System.out.println("loginUser:" + loginUser);
            map.put("msg", "登录成功");
        } else {
            map.put("msg", "用户名或密码错误");
        }
        return map;
    }
}

UserMapper

package com.example.demo.mapper;

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

@Mapper
public interface UserMapper {
    @Select("select * from usertest where user_name = #{user_name,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}")
    User getByUserNameAndPassword(User user);
}

UserService

package com.example.demo.service;

import com.example.demo.bean.User;

public interface UserService {
    public User getByUserNameAndPassword(User user);
}

UserServiceImpl

package com.example.demo.service.impl;

import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Override
    public User getByUserNameAndPassword(User user) {
        User User = userMapper.getByUserNameAndPassword(user);
        return User;
    }
}

重启后浏览器输入http://localhost:8080/loginHandle请求的执行顺序表示层LoginController -> 业务逻辑层UserService -> UserServiceImpl -> 数据访问层UserMapper

image.png

你可能感兴趣的:(spring-boot入门(三)——登录接口)