Previously we have done an example of CRUD web application using Grails 3 which were using default database H2 or HSQLDB in memory database. Now, we are creating an example of using Grails 3 and MongoDB.
Before we started, please make sure you have Grails 3 and MongoDB installed and MongoDB server is running in the background. If that clear, let's get started.
1. Create New Project
Open console or terminal the goto your projects folder the type this command to create a new project.
grails create-app grails3-mongodb
This command tells grails to create new project calls "grails3-mongodb". Go to the new created project folder.
cd grails3-mongodb
Type this command to enter Grails interactive mode.
grails
2. Configuring Project
For making grails 3 works with MongoDB, open and edit build.gradle in a root of the project folder. Disable hibernate plugin in build scripts dependencies.
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.8.2"
// classpath "org.grails.plugins:hibernate4:5.0.10"
}
In main dependencies disable hibernate4, hibernate-ehcache and h2. Add MongoDB dependencies, so build gradle will look like this.
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.8.2"
// classpath "org.grails.plugins:hibernate4:5.0.10"
}
}
version "0.1"
group "grails3.mongodb"
apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"org.grails.grails-gsp"
apply plugin:"asset-pipeline"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:mongodb"
// compile "org.grails.plugins:hibernate4"
// compile "org.hibernate:hibernate-ehcache"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"
// runtime "com.h2database:h2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}
assets {
minifyJs = true
minifyCss = true
}
Next, we need to configure mongodb connection. Open grails-app/conf/application.yml. Remove default hibernate and h2 config then replace with this config.
environments:
development:
grails:
mongodb:
host: "localhost"
port: 27017
username: ""
password: ""
databaseName: "bookstore"
production:
grails:
mongodb:
host: "localhost"
port: 27017
username: ""
password: ""
databaseName: "bookstore"
We will use "bookstore" as database name, host, and port using default MongoDB for deployment on the same server with application container.
Compile your application to get all dependencies.
compile
3. Create Domain
Based on database name "bookstore" we are creating a domain name "book". Type this command in grails interactive console.
create-domain-class com.bookstore.Book
This command will create two files.
| Created grails-app/domain/com/bookstore/Book.groovy
| Created src/test/groovy/com/bookstore/BookSpec.groovy
File in the grails-app folder is domain file and file in src is unit test file. Edit Book.groovy and create some fields like this.
package com.bookstore
class Book {
String isbn
String title
String description
String author
String publisher
Double price
Date publishDate
Date createDate = new Date
static constraints = {
}
}
Back to grails console, generate CRUD scaffolding for this domain.
generate-all com.bookstore.Book
This command will generate all required files for CRUD functions.
| Rendered template Controller.groovy to destination grails-app/controllers/com/bookstore/BookController.groovy
| Rendered template Spec.groovy to destination src/test/groovy/com/bookstore/BookControllerSpec.groovy
| Scaffolding completed for grails-app/domain/com/bookstore/Book.groovy
| Rendered template edit.gsp to destination grails-app/views/book/edit.gsp
| Rendered template create.gsp to destination grails-app/views/book/create.gsp
| Rendered template index.gsp to destination grails-app/views/book/index.gsp
| Rendered template show.gsp to destination grails-app/views/book/show.gsp
| Views generated for grails-app/domain/com/bookstore/Book.groovy
4. Test Application
To make sure this application works, type this command in the interactive console. Before type that command, please make sure your MongoDB server is running.
run-app
Open this URL http://localhost:8080 in the browser.
You can see book controller in the bottom left of the screen. Just click on it.
Congratulations, now you can try all CRUD function using Grails 3 and MongoDB.
You can find the source code in github.
Thanks.