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

本文主要是介绍springboot整合vue和mybaits项目实战笔记--------后台登录验证实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 配置application.yml
mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.demo.model
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/deliverysystemusername: rootpassword: 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;}@Overridepublic 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 {@AutowiredUserDao 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  WHEREusername=#{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字符串

这篇关于springboot整合vue和mybaits项目实战笔记--------后台登录验证实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/941259

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

Security OAuth2 单点登录流程

单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性。当拥有这项属性时,当用户登录时,就可以获取所有系统的访问权限,不用对每个单一系统都逐一登录。这项功能通常是以轻型目录访问协议(LDAP)来实现,在服务器上会将用户信息存储到LDAP数据库中。相同的,单一注销(single sign-off)就是指

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物