首页 > 其他分享 >springboot mybatis-plus 登录接口

springboot mybatis-plus 登录接口

时间:2023-11-06 22:33:44浏览次数:30  
标签:springboot 登录 spring 接口 private plus user mybatis

下面是使用SpringBoot和MyBatis-Plus实现登录接口的示例代码:

  1. 添加依赖

在pom.xml文件中添加以下依赖:

<dependencies>
    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Mybatis Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
    </dependency>

    <!-- 数据库驱动 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.199</version>
    </dependency>

    <!-- 其他依赖... -->
</dependencies>
  1. 创建数据库表

创建一个名为user的数据库表,包含以下字段:

字段名

类型

描述

id

BIGINT

用户ID

name

VARCHAR

用户名

pwd

VARCHAR

用户密码

  1. 创建实体类

创建一个名为User的实体类,映射数据库表user

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;
    private String pwd;
}
  1. 创建Mapper接口

创建一个名为UserMapper的Mapper接口,使用MyBatis-Plus提供的BaseMapper进行CRUD操作:

public interface UserMapper extends BaseMapper<User> {
}
  1. 编写配置文件

application.properties文件中配置数据库信息:

spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
mybatis-plus.configuration.map-underscore-to-camel-case=true
  1. 编写登录接口

创建一个名为UserController的控制器,在其中编写登录接口:

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", user.getName()).eq("pwd", user.getPwd());
        User dbUser = userMapper.selectOne(wrapper);
        if (dbUser != null) {
            return "登录成功";
        } else {
            return "登录失败,用户名或密码错误";
        }
    }
}

该接口使用POST方式请求,接收一个User类型的参数user,其中包含用户名和密码。接口通过使用MyBatis-Plus提供的QueryWrapper查询用户信息,如果查询到了数据,则返回登录成功,否则返回登录失败。

  1. 测试登录接口

启动应用程序,在浏览器中访问http://localhost:8080/login,进行登录测试。例如,以下是使用cURL工具进行登录测试的示例命令:

curl -X POST -H "Content-Type: application/json" -d '{"name":"admin","pwd":"123456"}' http://localhost:8080/login

如果返回登录成功,则表示登录接口测试通过。

标签:springboot,登录,spring,接口,private,plus,user,mybatis
From: https://blog.51cto.com/u_16018896/8218913

相关文章

  • springboot+mybatis-plus批量删除
    可以使用Mybatis-Plus提供的deleteBatchIds方法来实现批量删除。示例代码:@AutowiredprivateMybatisPlusMappermybatisPlusMapper;publicvoiddeleteBatch(List<Long>ids){mybatisPlusMapper.deleteBatchIds(ids);}其中,MybatisPlusMapper是你的Mapper接口,继承了BaseMa......
  • 如何在idea中创建一个SpringBoot项目
    在IntelliJIDEA中创建一个SpringBoot项目非常简单。下面是一步一步的指南:打开IntelliJIDEA:启动IntelliJIDEA,确保你已经安装并配置好Java开发环境。创建新项目:如果你在IDEA的欢迎界面,点击"CreateNewProject"。如果你已经有一个项目打开,可以通过选择"File"->"New......
  • Java:SpringBoot实现JDK动态代理和CGLIB动态代理
    (目录)需要代理的对象//接口publicinterfacePayService{voidpay();}//实现publicclassAliPayServiceimplementsPayService{@Overridepublicvoidpay(){System.out.println("AliPayService");}}1.JDK动态代理在JDK动态代......
  • javaweb-- Mybatis参数传递
     Mybatis提供了ParamNameResolver类进行封装 传入多个参数时,mybatis会将参数封装成Map集合map.put("arg0",参数值1)map.put("param1",参数值1)map.put("arg1",参数值2)map.put("param2",参数值2) ......
  • 通过mybatis-plus的自定义拦截器实现控制 mybatis-plus的全局逻辑删除字段的控制 (修改
    需求:过滤部分请求不实现mybatis-plus的逻辑删除看到网上关于mybatis-plus的自定义拦截器的文章有的少想了想自己写了一篇欢迎参考指正通过springboot的拦截器在请求进来时标记需要实现的需求的逻辑importlombok.Data;@DatapublicclassSyncBo{privateBoolean......
  • SpringBoot通过自定义注解+反射机制比较两个对象不同的属性值
    publicclassFieldComparisonUtil{/**•直接返回一个新的对象,并且对象的值只有被修改的部分••@paramold•@paramsource•@paramisParent•@paramtarget目标对象•@return/**•@paramold进行属性比较的原始数据•@paramsource进行属性比......
  • Linux脚本:批量启动docker容器、批量启动springboot、批量启动Vuejs
    批量启动springboot#!/bin/bash#检查容器是否已经启动check_container(){sudodockerps|grep"$1">/dev/nullif[$?-ne0];thenecho"$1containerisnotrunning.Starting$1..."sudodockerstart"$1"elseecho......
  • springboot第44集:Kafka集群和Lua脚本
    servers:Kafka服务器的地址。这是Kafka集群的地址,生产者将使用它来发送消息。retries:在消息发送失败时,生产者将尝试重新发送消息的次数。这个属性指定了重试次数。batchSize:指定了生产者在发送消息之前累积的消息大小(以字节为单位)。一次性发送多个消息可以提高性能。linger:指定了生......
  • springboot入门
    两年没写了。。连右下的小人都没了。得开始新一阶段的学习了。先从学习springboot及其前置内容开始学习。然后简单复习一下vue框架。idea在创建maven的springboot工程时自动下了个依赖,尽量选择版本低一点的。。适配java8不容易出问题。一旦出问题了多重建项目就会重新下springboo......
  • 【mysql】获取某个表所有列名【mybatis】
    方法1:[仅指定表名]selectCOLUMN_NAMEfrominformation_schema.COLUMNSwheretable_name='your-table-name'; 方法2:[指定表名+数据库名]selectCOLUMN_NAMEfrominformation_schema.COLUMNSwheretable_name='your-table-name'andtable_schema='your-DB......