gradleで作ります。
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-mustache' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.flywaydb:flyway-core' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
YAMLファイルの作成
src/main/resources/application.yml
spring: datasource: url:jdbc:postgresql://localhost:5432/test driverClassName:org.postgresql.Driver username:root password: mvc: favicon: enabled:false
ん? Nullにするとエラーになるな
V1__Create.sql
create table tsubuyaki ( id serial primary key, txt varchar(100) not null, version integer not null default 0, updated_time timestamp not null default current_timestamp, created_time timestamp not null default current_timestamp );
Model
package com.example.demo.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Version; import org.hibernate.validator.constraints.NotEmpty; @Entity public class Tsubuyaki extends TimestampEntity { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public long id; @NotEmpty public String txt; @Version public long version; }
package com.example.demo.model; import java.sql.Timestamp; import java.util.Date; import javax.persistence.Column; import javax.persistence.MappedSuperclass; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; @MappedSuperclass public abstract class TimestampEntity { public Timestamp updatedTime; @Column(updatable=false) public Timestamp createdTime; @PrePersist public void prePersist() { Timestamp ts = new Timestamp((new Date()).getTime()); this.createdTime = ts; this.updatedTime = ts; } @PreUpdate public void preUpdate() { this.updatedTime = new Timestamp((new Date()).getTime()); } }
Repository
package com.example.demo.repository; import org.springframework.data.repository.CrudRepository; import com.example.demo.model.Tsubuyaki; public interface TsubuyakiRepository extends CrudRepository<Tsubuyaki, Long>{ Iterable<Tsubuyaki> findAllByOrderByUpdatedTimeDesc(); }
controller
package com.example.demo.controller; import java.util.Collections; import java.util.Map; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.demo.model.Tsubuyaki; import com.example.demo.repository.TsubuyakiRepository; @RestController @RequestMapping("/tsubuyaki") public class TsubuyakiController { @Autowired TsubuyakiRepository repo; @RequestMapping(method=RequestMethod.POST) public Map<String, Tsubuyaki> create( @Valid @RequestBody Tsubuyaki tsubuyaki ){ return Collections.singletonMap( "tsubuyaki", repo.save(tsubuyaki)); } @RequestMapping(method=RequestMethod.GET) public Map<String, Tsubuyaki> read(){ return Collections.singletonMap( "tsubuyaki", repo.findAllByOrderByUpdatedTimeDesc()); } @RequestMapping(path="/{id}", method=RequestMethod.PUT) public void update( @PathVariable Long id, @RequestParam String txt ){ Tsubuyaki tsubuyaki = repo.findOne(id); tsubuyaki.txt = txt; repo.save(tsubuyaki); } @RequestMapping(path="/{id}", method=RequestMethod.DELETE) public void delete( @PathVariable Long id ){ repo.delete(id); } }
ぐぬぬぬ。。。