溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis與Spring Boot集成數(shù)據(jù)校驗(yàn)

發(fā)布時(shí)間:2024-09-11 16:56:27 來源:億速云 閱讀:81 作者:小樊 欄目:編程語言

MyBatis 與 Spring Boot 集成數(shù)據(jù)校驗(yàn)可以通過以下幾個(gè)步驟來實(shí)現(xiàn):

  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. 創(chuàng)建實(shí)體類

創(chuàng)建一個(gè)實(shí)體類,例如 User,并使用 JSR 380 注解進(jìn)行數(shù)據(jù)校驗(yàn):

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

public class User {
    @NotBlank(message = "用戶名不能為空")
    @Size(min = 3, max = 20, message = "用戶名長度必須在3到20個(gè)字符之間")
    private String username;

    @NotBlank(message = "密碼不能為空")
    @Size(min = 6, max = 20, message = "密碼長度必須在6到20個(gè)字符之間")
    private String password;

    // 省略 getter 和 setter 方法
}
  1. 創(chuàng)建 Mapper 接口

創(chuàng)建一個(gè) MyBatis Mapper 接口,例如 UserMapper,并定義相應(yīng)的 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. 創(chuàng)建 Service 層

創(chuàng)建一個(gè) 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. 創(chuàng)建 Controller 層

創(chuàng)建一個(gè) 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 "用戶創(chuàng)建成功";
    }
}

現(xiàn)在,當(dāng)你向 /users 發(fā)送 POST 請求時(shí),Spring Boot 會(huì)自動(dòng)對請求體中的 JSON 數(shù)據(jù)進(jìn)行校驗(yàn),如果數(shù)據(jù)不符合要求,將返回相應(yīng)的錯(cuò)誤信息。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI