springboot整合vue和mybaits项目实战笔记--------后台登录验证实现

  • 配置application.yml
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.model
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/deliverysystem
    username: root
    password: heyanying
server:
  port: 9000
  • bean下编写user类
public class User {
    private int id;
    private String name;
    private String password;
    private String role;
    private String phonenumber;
    private String ordernumber;

    public User() {
    }

    public User(String name, String password, String role, String phonenumber, String ordernumber) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.role = role;
        this.phonenumber = phonenumber;
        this.ordernumber = ordernumber;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public String getOrdernumber() {
        return ordernumber;
    }

    public void setOrdernumber(String ordernumber) {
        this.ordernumber = ordernumber;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", role='" + role + '\'' +
                ", phonenumber='" + phonenumber + '\'' +
                ", ordernumber='" + ordernumber + '\'' +
                '}';
    }
}
  • 编写controller下logincontroller
import com.alibaba.fastjson.JSON;
import com.nupt.deliverysysytem.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {

    @Autowired
    UserDao userDao;

    @RequestMapping("/login") //访问路径
    public String login(@RequestBody User user){
        String flag="error";
        User us= userDao.getUserByMassage(user.getUserName(),user.getPassword());
        HashMap<String,Object> res=new HashMap<>();
        if (us!=null){
            flag="ok";
        }
        res.put("flag",flag);
        res.put("user",user);
        String res_json=JSON.toJSONString("res");
        return res_json;
    }
}
  • dao层uerdao
import com.nupt.deliverysysytem.bean.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao {
    public User getUserByMassage(@Param("username") String username, @Param("password") String password);
}

  • Mapper文件下写usermapper.xml(resulttype是返回类型)
<?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/nupt/deliverysysytem/dao/UserDao">
    <select id="getUserByMassage" resultType="com/nupt/deliverysysytem/bean/User">
      SELECT * FROM user  WHERE
      username=#{username} AND password=#{password}
    </select>
</mapper>
  • 记得加mapperscan扫描dao
@MapperScan("com/nupt/deliverysysytem/dao")

@SpringBootApplication
public class DeliverysystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(DeliverysystemApplication.class, args);
    }

}

知识点

  • @RestController = @Controller + @ResponseBody组成

    @Controller 将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化。当然也有语义化的作用,即代表该类是充当Controller的作用
    @ResponseBody 它的作用简短截说就是指该类中所有的API接口返回的数据,甭管你对应的方法返回Map或是其他Object,它会以Json字符串的形式返回给客户端,如果返回的是String类型,则仍然是String。

@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}
@Controller
@ResponseBody
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

上面两种写法等价。

  • @Autowired注解

    由Spring框架定义,用于描述类中的属性后者方法(构造方法,set方法)。
    Spring框架在项目运行时假如发现由它管理的Bean对象中有使用@Autowired注解描述的属性或者方法,会按照指定规则为属性赋值(DI)。
    如果有多个实现类不加@Qualifier注解指定是哪个实现类会报错。

  • @Repository
    把类标识成可用于 @Autowired 注解自动装配的 bean 的类

  • JSON.toJSONString则是将对象转化为Json字符串

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值