JPAは簡単な添削機能を実現


JPAは簡単な添削・改ざん機能を実現----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.dfl.ycp3.stock.common.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

/**
 * @author fyq
 * @time 2019/12/03
 * @Description      
 */
@Entity
@Table(name = "student")
@Data
public class Student implements Serializable {
     

    private static final long serialVersionUID = -2333472778385823832L;
    
    //  
    @Id
    @Column(name = "studentnumber")
    private String studentNumber;

	//  
    @Column(name = "studentname")
    private String studentName;

	//  
    @Column(name = "studentsex")
    private String studentSex;

	//    
    @Column(name = "studentbirthday")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date studentBirthday;

	//  
    @Column(name = "studentage")
    private Integer studentAge;

	//  
    @Column(name = "studenthobby")
    private String studentHobby;

	//    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "createtime")
    private Date createTime;

	//   
    @Column(name = "creator")
    private String creator;

	//    
    @Column(name = "updatetime")
    private Date updateTime;

	//   
    @Column(name = "updateperson")
    private String updatePerson;

    public Student() {
     
    }

    public Student(String studentNumber, String studentName, String studentSex, Date studentBirthday, Integer studentAge,
                   String studentHobby, Date createTime, String creator, Date updateTime, String updatePerson) {
     
        this.studentNumber = studentNumber;
        this.studentName = studentName;
        this.studentSex = studentSex;
        this.studentBirthday = studentBirthday;
        this.studentAge = studentAge;
        this.studentHobby = studentHobby;
        this.createTime = createTime;
        this.creator = creator;
        this.updateTime = updateTime;
        this.updatePerson = updatePerson;
    }

    public String getStudentNumber() {
     
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
     
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
     
        return studentName;
    }

    public void setStudentName(String studentName) {
     
        this.studentName = studentName;
    }

    public String getStudentSex() {
     
        return studentSex;
    }

    public void setStudentSex(String studentSex) {
     
        this.studentSex = studentSex;
    }

    public Date getStudentBirthday() {
     
        return studentBirthday;
    }

    public void setStudentBirthday(Date studentBirthday) {
     
        this.studentBirthday = studentBirthday;
    }

    public Integer getStudentAge() {
     
        return studentAge;
    }

    public void setStudentAge(Integer studentAge) {
     
        this.studentAge = studentAge;
    }

    public String getStudentHobby() {
     
        return studentHobby;
    }

    public void setStudentHobby(String studentHobby) {
     
        this.studentHobby = studentHobby;
    }

    public Date getCreateTime() {
     
        return createTime;
    }

    public void setCreateTime(Date createTime) {
     
        this.createTime = createTime;
    }

    public String getCreator() {
     
        return creator;
    }

    public void setCreator(String creator) {
     
        this.creator = creator;
    }

    public Date getUpdateTime() {
     
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
     
        this.updateTime = updateTime;
    }

    public String getUpdatePerson() {
     
        return updatePerson;
    }

    public void setUpdatePerson(String updatePerson) {
     
        this.updatePerson = updatePerson;
    }

}

-----------------------------------------------------------------------------------------------
package com.dfl.ycp3.stock.repository;

import com.dfl.ycp3.stock.common.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Repository
public interface StudentDao extends JpaRepository<Student,Integer>, JpaSpecificationExecutor<Student> {
     

    //    
    @Query(value = "SELECT s FROM Student s order by s.studentNumber")
    List<Student> findAll();

    //     
    @Query(value = "SELECT s FROM Student s WHERE s.studentNumber in(?1)")
    Student findByStudentNumber(String  studentNumber);

    //     
    @Modifying
    @Transactional
    @Query(value = "update Student set studentName= :studentName ,studentSex= :studentSex ,studentBirthday= :studentBirthday ," +
            "studentAge= :studentAge ,studentHobby= :studentHobby ,createTime= :createTime ,creator= :creator ," +
            "updateTime= :updateTime ,updatePerson= :updatePerson  where studentNumber = :studentNumber")
    void updateByStudentNumber(
    					  @Param("studentName")String studentName, @Param("studentSex")String studentSex,
                          @Param("studentBirthday") Date studentBirthday, @Param("studentAge")Integer studentAge,
                          @Param("studentHobby")String studentHobby, @Param("createTime")Date createTime,
                          @Param("creator")String creator, @Param("updateTime")Date updateTime,
                          @Param("updatePerson")String updatePerson,@Param("studentNumber")String studentNumber);

    //     
    @Modifying
    @Transactional
    @Query(value = "delete from Student where studentNumber= :studentNumber ")
    void deleteByStudentNumber(String studentNumber);


}


注意:1、クエリー文の「Studio」はエンティティクラスの名前で、データベースの名前ではありません2、変更と削除の戻り値タイプはvoid、intとIntegerの3種類の3、@Modifyingは@Transactionalと組み合わせて使用する必要があります.@Modifyingの役割は、実行するsql文が修正(修正または削除)操作であることを宣言することであり、@Transactionalの役割はトランザクションサポートを提供することであり、@Queryにエラーが発生した場合、実行前の状態にロールバックする
-----------------------------------------Controller---------------------------------------------------
package com.dfl.ycp3.stock.controller.api;

import com.dfl.ycp3.common.utils.DateUtil;
import com.dfl.ycp3.stock.common.model.Student;
import com.dfl.ycp3.stock.repository.StudentDao;
import com.wordnik.swagger.annotations.ApiParam;
import com.yichengpai.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(description = "    ")
@RestController
@RequestMapping(value = "/student")
public class StudentController {
     
    @Autowired
    private StudentDao studentDao;
    private static final Logger logger= LoggerFactory.getLogger(StudentController.class);

    @ApiOperation(value = "      ",httpMethod = "GET")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public List<Student> findAll(){
     
        List<Student> list = studentDao.findAll();
        return list;
    }


    @ApiOperation(value = "         ",httpMethod = "GET")
    @RequestMapping(value = "/findByStudentNumber",method = RequestMethod.GET)
    public Result findByStudentNumber(
            @RequestParam(value = "studentNumber",required = true) String studentNumber
    ){
     
        Student student = studentDao.findByStudentNumber(studentNumber);
        if (student == null){
     
            return Result.fail("fail", "      ,    !");
        }
        return Result.success(student);
    }


    @ApiOperation(value = "      ",httpMethod = "POST")
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public Result addStudent(
            @ApiParam("  (  ,    )") @RequestParam(value = "studentNumber", required = true) String studentNumber,
            @ApiParam("  ") @RequestParam(value = "studentName", required = true) String studentName,
            @ApiParam("  ") @RequestParam(value = "studentSex", required = false) String studentSex,
            @ApiParam("    ") @RequestParam(value = "studentBirthday", required = false) String studentBirthday,
            @ApiParam("  ") @RequestParam(value = "studentAge", required = false) Integer studentAge,
            @ApiParam("  ") @RequestParam(value = "studentHobby", required = false) String studentHobby,
            @ApiParam("    ") @RequestParam(value = "createTime", required = false) String createTime,
            @ApiParam("   ") @RequestParam(value = "creator", required = false) String creator,
            @ApiParam("    ") @RequestParam(value = "updateTime", required = false) String updateTime,
            @ApiParam("   ") @RequestParam(value = "updatePerson", required = false) String updatePerson
    ) {
     
        Date birthday = DateUtil.parseStringToDate(studentBirthday,"yyyy-MM-dd");
        Date ctime = DateUtil.parseStringToDate(createTime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
        Date utime = DateUtil.parseStringToDate(updateTime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
        Student student = studentDao.findByStudentNumber(studentNumber);
        if(student !=null){
     
            return Result.fail("fail", "       ,      !");
        }else {
     
            studentDao.save(new Student(studentNumber, studentName, studentSex, birthday, studentAge, studentHobby, ctime, creator, utime, updatePerson));
            return Result.success("");
        }
    }


    @ApiOperation(value = "         ",httpMethod = "POST")
    @RequestMapping(value = "/updateByStudentNumber",method = RequestMethod.POST)
    public Result updateByStudentNumber(
            @ApiParam("  (  ,    )") @RequestParam(value = "studentNumber", required = true) String studentNumber,
            @ApiParam("  ") @RequestParam(value = "studentName", required = false) String studentName,
            @ApiParam("  ") @RequestParam(value = "studentSex", required = false) String studentSex,
            @ApiParam("    ") @RequestParam(value = "studentBirthday", required = false) String birthday,
            @ApiParam("  ") @RequestParam(value = "studentAge", required = false) Integer studentAge,
            @ApiParam("  ") @RequestParam(value = "studentHobby", required = false) String studentHobby,
            @ApiParam("    ") @RequestParam(value = "createTime", required = false) String ctime,
            @ApiParam("   ") @RequestParam(value = "creator", required = false) String creator,
            @ApiParam("    ") @RequestParam(value = "updateTime", required = false) String utime,
            @ApiParam("   ") @RequestParam(value = "updatePerson", required = false) String updatePerson
    ) {
     
        Date studentBirthday = DateUtil.parseStringToDate(birthday,"yyyy-MM-dd");
        Date createTime = DateUtil.parseStringToDate(ctime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
        Date updateTime = DateUtil.parseStringToDate(utime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
        Student student = studentDao.findByStudentNumber(studentNumber);
        if (student != null){
     
            studentDao.updateByStudentNumber(studentName,studentSex,studentBirthday,studentAge,studentHobby,
                    createTime,creator,updateTime,updatePerson,studentNumber);
            return Result.success("");
        }else{
     
            return Result.fail("fail", "      ,    !");
        }

    }


    @ApiOperation(value = "         ",httpMethod = "POST")
    @RequestMapping(value = "/deleteByStudentNumber",method = RequestMethod.POST)
    public Result deleteByStudentNumber(
            @ApiParam("     ") @RequestParam(value = "studentNumber", required = true) String studentNumber

    ) {
     
        Student student = studentDao.findByStudentNumber(studentNumber);
        if (student !=null){
     
            studentDao.deleteByStudentNumber(studentNumber);
            return Result.success("");
        }else{
     
            return Result.fail("fail", "      ,    !");
        }
    }
    
}