Spring boot4之数据校验

Spring boot 数据校验

  • entity

    1 package com.wiggin.entity;

    2 

    3 import lombok.Data;

    4 import org.hibernate.validator.constraints.Length;

    5 

    6 import javax.validation.constraints.Min;

    7 import javax.validation.constraints.NotEmpty;

    8 import javax.validation.constraints.NotNull;

    9 

   10 

   11 @Data

   12 public class User {

   13         @NotNull(message = "id不能为空")

   14         private Long id;

   15         @NotEmpty(message = "姓名不能为空")

   16         @Length(min = 2,message = "姓名长度不能小于两位")

   17         private String name;

   18         @Min(value = 16,message = "年龄必须大于16")

   19         private int age;

   20 }

  • Handler

    1       @GetMapping("/validatorUser")

    2         public void validatorUser(@Valid User user, BindingResult bindingResult){

    3                 // 打印user对象

    4                 System.out.println(user);

    5                 if (bindingResult.hasErrors()){

    6                         // 获取所有error

    7                         List<ObjectError> list = bindingResult.getAllErrors();

    8                         for (ObjectError objectError:list){

    9                                 // objectError.getCode() 为错误类型,objectError.getDefaultMessage()为错误信息

   10                                 System.out.println(objectError.getCode() + "-" + objectError.getDefaultMessage());

   11                         }

   12                 }

   13         }

Spring boot 整合JDBC

  • pom.xml

    1                 <!-- 引入JDBC-->

    2                 <dependency>

    3                         <groupId>org.springframework.boot</groupId>

    4                         <artifactId>spring-boot-starter-jdbc</artifactId>

    5                 </dependency>

    6                 <dependency>

    7                         <groupId>mysql</groupId>

    8                         <artifactId>mysql-connector-java</artifactId>

    9                         <version>8.0.21</version>

   10                 </dependency>

  • application.yml

    1 server:

    2     port: 8080

    3 spring:

    4     thymeleaf:

    5         prefix: classpath:/templates/

    6         suffix: .html

    7         encoding: UTF-8

    8         mode: HTML5

    9     datasource:

   10         url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

   11         username: root

   12         password: 123456

   13         driver-class-name: com.mysql.cj.jdbc.Driver

  • User

    1 package com.wiggin.entity;

    2 

    3 import lombok.AllArgsConstructor;

    4 import lombok.Data;

    5 import lombok.NoArgsConstructor;

    6 import org.hibernate.validator.constraints.Length;

    7 

    8 import javax.validation.constraints.Min;

    9 import javax.validation.constraints.NotEmpty;

   10 import javax.validation.constraints.NotNull;

   11 

   12 

   13 @Data

   14 @AllArgsConstructor

   15 @NoArgsConstructor

   16 public class User {

   17         @NotNull(message = "id不能为空")

   18         private Long id;

   19         @NotEmpty(message = "姓名不能为空")

   20         @Length(min = 2,message = "姓名长度不能小于两位")

   21         private String name;

   22         @Min(value = 60,message = "成绩必须大于60")

   23         private double score;

   24 }

  • UserRepository

    1 package com.wiggin.repository;

    2 

    3 import com.wiggin.entity.User;

    4 

    5 import java.util.Collection;

    6 import java.util.List;

    7 

    8 public interface UserRepository {

    9         public List<User> findAll();

   10         public User findById(long id);

   11         public void save(User user);

   12         public void update(User user);

   13         public void deleteById(long id);

   14 }

  • UserRepositoryImpl

    1 package com.wiggin.repository.impl;

    2 

    3 import com.wiggin.entity.User;

    4 import com.wiggin.repository.UserRepository;

    5 import org.springframework.beans.factory.annotation.Autowired;

    6 import org.springframework.jdbc.core.BeanPropertyRowMapper;

    7 import org.springframework.jdbc.core.JdbcTemplate;

    8 import org.springframework.stereotype.Repository;

    9 

   10 import java.util.Collection;

   11 import java.util.List;

   12 

   13 @Repository

   14 public class UserRepositoryImpl implements UserRepository {

   15         @Autowired

   16         private JdbcTemplate jdbcTemplate;

   17         @Override

   18         public List<User> findAll() {

   19                 // new BeanPropertyRowMapper<>(User.class)映射实体类

   20                 return jdbcTemplate.query("select * from users ",new BeanPropertyRowMapper<>(User.class));

   21         }

   22 

   23         @Override

   24         public User findById(long id) {

   25 

   26                 return jdbcTemplate.queryForObject("select * from users where id = ?",new Object[]{id},new BeanPropertyRowMapper<>(User.class));

   27         }

   28 

   29         @Override

   30         public void save(User user) {

   31                 jdbcTemplate.update("insert into users(name,score) value (?,?)",user.getName(),user.getScore());

   32         }

   33 

   34         @Override

   35         public void update(User user) {

   36                 jdbcTemplate.update("update users set name = ?,score = ? where id = ?", user.getName(),user.getScore(),user.getId());

   37         }

   38 

   39 

   40         @Override

   41         public void deleteById(long id) {

   42                 jdbcTemplate.update("delete from users where id = ?",id);

   43         }

   44 }

  • UserHandler

    1 package com.wiggin.controller;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.entity.User;

    5 import com.wiggin.repository.UserRepository;

    6 import org.springframework.beans.factory.annotation.Autowired;

    7 import org.springframework.stereotype.Controller;

    8 import org.springframework.web.bind.annotation.*;

    9 

   10 import java.util.List;

   11 

   12 @RestController

   13 @RequestMapping("/user")

   14 public class UserHandler {

   15 

   16         @Autowired

   17         private UserRepository userRepository;

   18 

   19         @GetMapping("/findAll")

   20         public List<User> findAll(){

   21                 return userRepository.findAll();

   22         }

   23 

   24         @GetMapping("/findById/{id}")

   25         public User findById(@PathVariable("id") long id){

   26                 return userRepository.findById(id);

   27         }

   28 

   29         @PostMapping("/save")

   30         // @RequestBody User user把请求的body转为java对象,@ResponseBodyjava对象转为body内容

   31         public void save(@RequestBody User user){

   32                 userRepository.save(user);

   33         }

   34 

   35         @PutMapping("/update")

   36         // @RequestBody User user把请求的body转为java对象,@ResponseBodyjava对象转为body内容

   37         public void update(@RequestBody User user){

   38                 userRepository.update(user);

   39         }

   40 

   41         @DeleteMapping("/deleteById/{id}")

   42         public void deleteById(@PathVariable("id") long id){

   43                 userRepository.deleteById(id);

   44         }

   45 

 

posted @ 2020-08-13 22:44  wigginess  阅读(161)  评论(0编辑  收藏  举报