1. 程式人生 > >第11講 11.1 SpringBoot表單校驗

第11講 11.1 SpringBoot表單校驗

1,複製一個專案,改名為SpringDataValid,關閉其他專案, 以防干擾。右鍵專案,Maven,Update Project...

 


2,新建一個數據庫,db_studentinfo,修改配置檔案裡的資料庫連線。

server:
  port: 8888
  context-path: /
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_studentinfo
    password: root
    username: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

 


3,新建一個Student實體,指定表名 t_student,添加註解,Integer型別就不用加長度限制,姓名不能為空,年齡不能為空,且年齡不能小於18歲,

 

package com.cruise.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="t_student")
public class Student {
    
    @Id
    @GeneratedValue
    private Integer id;
    
    @NotBlank(message="輸入的名字無效!")
    @NotEmpty(message="姓名不能為空!")
    @Column(length=50)
    private String name;
    
    @NotNull(message="年齡不能為空!")
    @Min(value=18,message="年齡必須大於18歲!")
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

 


4,StudentDao介面,新增繼承

 

package com.cruise.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.cruise.entity.Student;

public interface StudentDao extends JpaRepository{
     
}

 


5,StudentService介面,寫介面方法,新增學生資訊,

 

package com.cruise.service;

import com.cruise.entity.Student;

public interface StudentService {

    public void add(Student student);
}

 


6,寫StudentServiceImpl 實現StudentService介面,注入StudentDao介面。刪除Account相關的類,方式干擾,

 

package com.cruise.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cruise.dao.StudentDao;
import com.cruise.entity.Student;
import com.cruise.service.StudentService;

/**
 * 
 * @author pengc
 *
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;

    @Override
    public void add(Student student) {
        studentDao.save(student);
    }
}

 


7, 寫StudentController ,注入StudentService,新增RestController註解(和ResponseBody註解等價,區別在於ResponseBody註解是加在方法上的,),自動轉成json。寫add()方法,新增學生資訊,

 

package com.cruise.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cruise.entity.Student;
import com.cruise.service.StudentService;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;
    
    @RequestMapping("/add")
    public String add(@Valid Student student,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return bindingResult.getFieldError().getDefaultMessage();
        }else{
            studentService.add(student);
            return "新增成功!";
        }
    }
}

 


8, 寫studentAdd.html 方法,匯入jquery,ajax 方式提交,

見下節...