欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

面试-springboot+Mybatis实现简单的登录功能

程序员文章站 2024-01-14 19:46:28
思路没有涉及到jwt和权限,只是简单的用户名和密码的校验就行嘞.postman测试登录功能获取数据库中的用户名信息pom org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE&...

思路

没有涉及到jwt和权限,只是简单的用户名和密码的校验就行嘞.
postman测试登录功能获取数据库中的用户名信息

pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--通用mapper起步依赖-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
    </dependencies>

启动类

@SpringBootApplication
@MapperScan(basePackages ={ "com.xyy.dao"})
public class APP {
    public static void main(String[] args) {
        SpringApplication.run(APP.class, args);
    }
}

controller

get请求,请求参数放在地址栏中进行传输

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/myLogin")
    public User login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = loginService.login(username, password);
        return user;
    }
}

service逻辑层

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public User login(String username, String password) {
        User user = loginMapper.login(username, password);
        if (user == null) {
            throw new RuntimeException("用户名或者密码输入错误");
        }
        return user;
    }
}

mapper通用方法

public interface LoginMapper extends Mapper<User> {
    @Select("select * from login where username=#{username} and password=#{password}")
    User login(@Param("username") String username, @Param("password") String password);
}

application.yml

server:
  port: 9008     #访问路径 
spring:
  application:
    name: nacos-provider  #注册中心中出现的名字
  datasource:    #地址改成自己的
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.93.128:3308/changgou-goods?   useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root

本文地址:https://blog.csdn.net/weixin_47785112/article/details/109274476