使用Groovy的简单实现Spring Boot、Data JPA和MySQL操作

环境

OS X Yosemite 10.10.3
jdk1.8.0_45.jdk
MySQL 服务器版本: 5.5.28 源码分发

春季工具套件

版本:3.6.4.RELEASE
构建编号:201503100337
平台:Eclipse Kepler SR2 (4.3.2)

Gradle的IDE插件版本:3.6.4.201503050952-RELEASE
Groovy-Eclipse插件版本:2.9.2.xx-201502282108-e43j8

“前提条件”

在下面的状态下添加实现

在Spring Boot/Groovy/Eclipse中编写Hello World
http://qiita.com/quwahara/items/f4b1d30855fff83da3b8

请在build.gradle文件中配置Data JPA和MySQL连接器的依赖关系。

在前提条件的build.gradle文件中添加// Add


// 省略

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'

    // 省略
}

// 省略

在包资源管理器中,右击项目的根元素,选择Gradle → 全部刷新。

添加数据模型(领域)和操作(存储库)

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

}
package hello.service

import hello.domain.Memo

import org.springframework.data.repository.CrudRepository


interface MemoRepository extends CrudRepository<Memo, Long> {

}

添加控制器

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[]
    }
}

指定组件扫描

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 {
    // 省略
}

将Database的连接设置添加到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

以下是下一篇文章。

Thymeleaf的简单实现
http://qiita.com/quwahara/items/a5e8b4c5d1b039b99730

Thymeleaf的简易实现
http://qiita.com/quwahara/items/a5e8b4c5d1b039b99730

广告
将在 10 秒后关闭
bannerAds