Groovyを使ったSpring Boot, Data JPA, MySQL操作の簡単な実装

# 環境

OS X Yosemite 10.10.3
jdk1.8.0_45.jdk
MySQL Server version: 5.5.28 Source distribution

## Spring Tool Suite 

Version: 3.6.4.RELEASE
Build Id: 201503100337
Platform: Eclipse Kepler SR2 (4.3.2)

Gradle IDE plugin Version: 3.6.4.201503050952-RELEASE
Groovy-Eclipse plugin Version: 2.9.2.xx-201502282108-e43j8


# 前提条件

下の状態に実装を加えます

> Spring Boot/Groovy/EclipseでHello world
http://qiita.com/quwahara/items/f4b1d30855fff83da3b8

# Data JPAとMySQL Connectorの依存関係をbuild.gradleに設定する

前提条件のbuild.gradleに// Addを追記

```groovy:build.gradle

// 省略

dependencies {
    // Add
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    // Add
    compile 'mysql:mysql-connector-java'

    compile 'org.codehaus.groovy:groovy-all'
    compile 'org.springframework.boot:spring-boot-starter-web'

    // 省略
}

// 省略
```

Package ExplorerでProjectのRoot要素を右クリック、Gradle → Refresh All

# Data Model(Domain)と操作(Repository)を追加

```groovy:src/main/java/hello/domain/Memo.groovy
package hello.domain

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
class Memo {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(nullable = false, unique = true, updatable = false)
    Long id

    @Column(nullable = false)
    String text

    @Column(nullable = false)
    Date creationDate

}
```

```groovy:src/main/java/hello/service/MemoRepository.groovy
package hello.service

import hello.domain.Memo

import org.springframework.data.repository.CrudRepository


interface MemoRepository extends CrudRepository<Memo, Long> {

}
```

# Controllerを追加

```groovy:src/main/java/hello/controller/MemoController.groovy
package hello.controller

import hello.domain.Memo
import hello.service.MemoRepository

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody


@Controller
class MemoController {

    @Autowired
    private MemoRepository memoRepository

    @RequestMapping("/memo/new")
    @ResponseBody
    Memo new_(@RequestParam(required=false) String text) {
        Memo memo = new Memo(text:text?:"", creationDate:new Date())
        return memoRepository.save(memo)
    }

    @RequestMapping("/memo/find-all")
    @ResponseBody
    List<Memo> findAll() {
        return memoRepository.findAll()
    }

    @RequestMapping("/memo/find/{id}")
    @ResponseBody
    List<Memo> find(@PathVariable Long id) {
        Memo memo = memoRepository.findOne(id)
        return (memo?[memo]:[]) as Memo[]
    }
}
```

# Component Scanを指定

```groovy:src/main/java/hello/SampleController.groovy
package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
// Add
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
// Add
@ComponentScan
@EnableAutoConfiguration
public class SampleController {
    // 省略
}
```

# application.propertiesにDatabase接続設定を追加

```properties:src/main/resources/application.properties
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/gpdb?characterEncoding=UTF-8
spring.datasource.username=user
spring.datasource.password=password

# JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database=MYSQL
# validate | update | create | create-drop
spring.jpa.hibernate.ddl-auto=create-drop
```

# アプリケーションを実行

Package Explorerでhello/SampleController.groovyを選択、右クリック
Debug As → Spring Boot App

ブラウザで下のURLを開く
http://localhost:8080/memo/new
http://localhost:8080/memo/find-all
http://localhost:8080/memo/find/1


# 参考

* Source
https://github.com/quwahara/GP/tree/spring-jpa-groovy

# 次の記事

Thyemeleafの簡単な実装
http://qiita.com/quwahara/items/a5e8b4c5d1b039b99730