Skip to content

grace-guides/gs-spring-boot-gorm

Repository files navigation

Main branch build status

Spring Boot with Grace GORM

Learn how to build a Spring Boot application using GORM.

Adding Grace Dependencies

plugins {
	id 'groovy'
	id 'org.springframework.boot'
	id 'io.spring.dependency-management'
	id 'org.graceframework.grace-core'
}

dependencies {
	implementation 'org.codehaus.groovy:groovy'
	implementation 'org.graceframework:grace-boot'
	implementation 'org.graceframework:grace-core'
	implementation 'org.graceframework:grace-plugin-core'
	implementation 'org.graceframework:grace-plugin-datasource'
	implementation 'org.graceframework.plugins:hibernate5'
	runtimeOnly 'com.h2database:h2'
}

Configure Hibernate Dialect and Datasource

Update src/main/resources/application.yml,

hibernate:
    show_sql: true
    hbm2ddl:
        auto: create
    dialect:  org.hibernate.dialect.H2Dialect
dataSource:
    dbCreate: create-drop
    pooled: true
    jmxExport: true
    driverClassName: org.h2.Driver
    url: jdbc:h2:file:./build/boot_dev;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password:

Creating Post Domain and GORM Service

Creating Post.groovy in app/domain,

class Post {

    String title

    static constraints = {
        title(blank: false, nullable: false, maxSize: 255)
    }

    String toString() {
        "#$id $title"
    }
}

Creating PostService.groovy in app/services,

@grails.gorm.services.Service(Post)
interface PostService {

    List<Post> findAll()

    Post save(Post post)

}

Now, GORM will generate $PostServiceImplementation for your application, then you can use it in your Spring Boot application just like in Grace,

// Post methods
new Post(title: 'Grace 2022.2.0 is released').save()
Post.list()
Post.where()

// PostService methods
postService.findAll()

Running the App

Staring the App by execute the following Gradle task,

./gradlew bootRun

In the src/main/groovy/grace/guides/GraceApplication, remember that, exculding the AutoConfigurations, HibernateJpaAutoConfiguration, DataSourceAutoConfiguration, TransactionAutoConfiguration in GraceApplication.

@SpringBootApplication(exclude = [HibernateJpaAutoConfiguration, DataSourceAutoConfiguration, TransactionAutoConfiguration])
class GraceApplication implements CommandLineRunner {

	@Autowired
	BootStrap bootStrap

	@Autowired
	PostService postService

	static void main(String[] args) {
		SpringApplication.run(GraceApplication, args)
	}

	@Override
	void run(String... args) throws Exception {
		println 'Prepare Sample Data '.padRight(94, '>')
		bootStrap.init()

		println()
		println 'Load All Posts '.padRight(94, '>')
		postService.findAll().each {
			println it 
		}

	}

}

Links

About

Spring Boot with Grace GORM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages