Create a JPA Application using Groovy and Spring Boot

You can create a Spring Boot application using Groovy either from the IDE or using the online Spring Boot application generator http://start.spring.io and selecting Groovy as the language.

You’ll now see how to develop a simple Spring Boot web application using Groovy, Spring Data JPA, and Thymeleaf. Add the Web, Thymeleaf, JPA, and H2 starters dependencies to your application.

spring init --language=groovy -dweb,thymeleaf,jpa,h2 demogroovy 

Create a JPA entity called Customer.groovy:

package com.example.demogroovy;
import javax.persistence.*@Entity @Table(name = "CUSTOMER")
class Customer {
  @Id @GeneratedValue(strategy = GenerationType.AUTO)
  Long id
  String name
  String email
}

As you are using Groovy, you don’t need to create setters and getters for your entity properties.

Create a Spring Data JPA repository for the Entity:

package com.example.demogroovy;

import org.springframework.data.jpa.repository.JpaRepository;

interface CustomerRepository extends JpaRepository<Customer, Long> {
  Customer findByName(String name);
}

Next, create a SpringMVC controller to show the list of customers, as shown here:

package com.example.demogroovy;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping @Controller class HomeController {
  @Autowired CustomerRepository repo;
  @GetMapping("/") String home(Model model) {
    model.addAttribute("customers", repo.findAll())
    "home"
  }
}

Create the Thymeleaf view src/main/resources/templates/home.html to render customers:

<html xmlns:th="http://www.thymeleaf.org">
     
    <head>
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
             
        <title>Customers List</title>
             
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         
    </head>
     
    <body>
         
        <table>
                 
            <thead>
                     
                <tr>
                             <th>Id</th>         <th>Name</th>     
                </tr>
                     
            </thead>
                 
            <tbody>
                     
                <tr th:each="Customer : ${customers}">
                             <td th:text="${customer.id}">Id</td>         <td th:text="${customer.name}">Name</td>     
                </tr>
                     
            </tbody>
             
        </table>
         
    </body>
     
</html>

When you generate the application, the main entry point class is created, as shown here:

package com.example.demogroovy;
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class DemoApplication {
  static void main(String[] args) {
    SpringApplication.run(DemoApplication, args)
  }
}

You can initialize the database with sample data using a SQL script in src/main/resources/data.sql

insert into customer(id, name, email) values (1,'john','john@gmail.com'), (2,'frank','frank@gmail.com'), (3,'test','test@gmail.com'); 

Finally, include in your src/main/resources/application.properties the following property that will auto-generate the customer table at start-up:

spring.jpa.hibernate.ddl-auto = update 

Now you can run the application by executing:

 ./mvnw spring-boot:run

If you point your browser to http://localhost:8080/, you should be able to see customer details.