Java Spring Boot その7
今回は DB 実装部分のソースを紹介します。
エンティティ
package hello.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "tasks") public class Task { @Id @GeneratedValue private Integer id; @Column(nullable = false) private String context; public Task() { } public Task(Integer id, String context) { this.id = id; this.context = context; } public Integer getId() { return id; } public String getContext() { return context; } }
@Entity
エンティティクラスを表すためのアノテーション。
@Table(name = "tasks")
エンティティに紐づくテーブルを指定するアノテーション。デフォルトではエンティティ名がクラス名となります。
@Id
主キーのフィールドを表すアノテーション。
@GeneratedValue
主キーが自動採番されることを表す。生成方法を指定することもできる。
@Column(nullable = false)
カラムに対する設定を行うアノテーション。
nullable = false
と設定し、Not Null であることを設定。
リポジトリ
package hello.repository; import org.springframework.data.jpa.repository.JpaRepository; import hello.domain.Task; public interface TaskRepository extends JpaRepository<Task, Integer> { }
JpaRepository を継承したインターフェースを作成するだけで、取得、登録、更新、削除ができるようになります。(すごい!)
JpaRepository<Task, Integer>
のようにジェネリクスに対象のエンティティ、IDカラムのデータ型を宣言し、終了です。
サービス
package hello.service; import java.util.List; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import hello.domain.Task; import hello.repository.TaskRepository; @Service @Transactional public class TaskService { @Autowired TaskRepository taskRepository; public List<Task> findAll() { return taskRepository.findAll(); } public Task findOne(Integer id) { return taskRepository.findOne(id); } public Task create(Task task) { return taskRepository.save(task); } public Task update(Task task) { return taskRepository.save(task); } public void delete(Integer id) { taskRepository.delete(id); } }
@Service
サービスクラスを表すアノテーション
@Transactional
トランザクション境界を設定するアノテーション。
サービスに設定したため、トランザクション境界はサービスとなります。
@Autowired
DI (依存性注入) を行うためのアノテーション。
Autowired アノテーションを記述すると、自動的に DI を解決してくれます。
サービスクラス自体はリポジトリクラスの内容を呼び出す形なので特に説明はいらないと思います。
コントローラ
package hello; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.http.HttpStatus; 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.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import hello.domain.Task; import hello.service.TaskService; @RestController @RequestMapping("api/tasks") @EnableAutoConfiguration @ComponentScan public class RestSampleController { @Autowired TaskService taskService; @RequestMapping(method = RequestMethod.GET) List<Task> getTasks() { // GET api/tasks で実行されるメソッド return taskService.findAll(); } @RequestMapping(value = "{id}", method = RequestMethod.GET) Task getTask(@PathVariable Integer id) { // GET api/tasks/{id} で実行されるメソッド return taskService.findOne(id); } @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) Task createTask(@RequestBody Task task) { // POST api/tasks で実行されるメソッド return taskService.create(task); } @RequestMapping(value = "{id}", method = RequestMethod.PUT) Task updateTask(@PathVariable Integer id, @RequestBody Task task) { // PUT api/tasks/{id} で実行されるメソッド return taskService.update(task); } @RequestMapping(value = "{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) void deleteTask(@PathVariable Integer id) { // DELETE api/tasks で実行されるメソッド taskService.delete(id); } public static void main(String[] args) { SpringApplication.run(RestSampleController.class, args); } }
大きな変更はなく、モックデータを削除して、サービスクラスからデータを取得するようにした位です。
@ComponentScan
必要なコンポーネントを自動で読み込んでくれるアノテーションです。
これによりサービス、リポジトリ、エンティティクラスが読み込まれます。
以上で DB 実装部分は終了です。