In this step we will connect database and create table using Grails domain. For connecting database, just create a database named grailstorial and update in conf/application.yml file
Default
Updated
dataSource: pooled: true jmxExport: true driverClassName: org.h2.Driver username: sa password: ''
dataSource: pooled = true driverClassName = "com.mysql.jdbc.Driver" dialect = org.hibernate.dialect.MySQL5InnoDBDialect
environments: development: dataSource: dbCreate: create-drop url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE environments: development: dataSource: dbCreate: update url: "jdbc:mysql://localhost:3306/grailstorial" username : "testuser" password : "pass123"
Add the mysql deps as runtime in the dependencies of your build.gradle. E.g.
runtime
dependencies
build.gradle
runtime 'mysql:mysql-connector-java:5.1.36'
Create a domain by following command
grails create-domain-class Contact At now, run the app, if you see a table in the database, then database connection is okay. Database connection is okay and we need to modify domain class. A domain class represents the core model behind in your application and is typically mapped onto database tables. class Contact { String firstName String lastName String email String city String country String message static constraints = { } } Run app again and you will see table filed according to domain class defined variable. For more about grails domain and constraints - http://docs.grails.org/3.1.1/ref/Domain%20Classes/constraints.html