MyBatis与Spring Boot集成数据校验

发布时间:2024-09-11 16:56:27 作者:小樊
来源:亿速云 阅读:81

MyBatis 与 Spring Boot 集成数据校验可以通过以下几个步骤来实现:

  1. 添加依赖

pom.xml 文件中添加 MyBatis、Spring Boot Starter Web、Spring Boot Starter Validation 和 MyBatis-Spring-Boot-Starter 的依赖:

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>
  1. 创建实体类

创建一个实体类,例如 User,并使用 JSR 380 注解进行数据校验:

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

public class User {
    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 20, message = "用户名长度必须在3到20个字符之间")
    private String username;

    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
    private String password;

    // 省略 getter 和 setter 方法
}
  1. 创建 Mapper 接口

创建一个 MyBatis Mapper 接口,例如 UserMapper,并定义相应的 SQL 查询语句:

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();
}
  1. 创建 Service 层

创建一个 Service 层,例如 UserService,并注入 UserMapper

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> findAll() {
        return userMapper.findAll();
    }
}
  1. 创建 Controller 层

创建一个 Controller 层,例如 UserController,并注入 UserService

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public List<User> findAll() {
        return userService.findAll();
    }

    @PostMapping("/users")
    public String createUser(@Valid @RequestBody User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return bindingResult.getFieldError().getDefaultMessage();
        }
        userService.findAll();
        return "用户创建成功";
    }
}

现在,当你向 /users 发送 POST 请求时,Spring Boot 会自动对请求体中的 JSON 数据进行校验,如果数据不符合要求,将返回相应的错误信息。

推荐阅读:
  1. 使用Spring boot如何实现对Mybatis进行集成
  2. Spring Boot 如何实现与MyBatis搭配使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:MyBatis在Spring Boot中的SQL模板

下一篇:MyBatis在Spring Boot中的分页插件选择

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》