[JAVA] Spring Boot, Doma2, Gradle initial setting summary

At the beginning

When developing with Eclipse, I'm always addicted to the initial settings of Spring Boot + Doma2 + Gradle, so I will summarize it for myself.

environment

Setup steps

(Optional) Create an application template with Spring Initializer

image-01.png

image-02.png

Add a dependency on Doma2

build.gradle


dependencies {
  // ...abridgement
  implementation 'org.seasar.doma.boot:doma-spring-boot-starter:1.4.0'
  annotationProcessor 'org.seasar.doma:doma-processor:2.35.0'
}

Set up Eclipse

Add plugins for Eclipse settings to plugins in build.gradle.

build.gradle


plugins {
  // ...abridgement
  id 'com.diffplug.eclipse.apt' version '3.23.0'
}

Execute gradle eclipse to reflect the settings.

$ ./gradlew eclipse
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 16s
5 actionable tasks: 5 executed

You can confirm that the Eclipse settings (annotation processing) have been completed.

image-03.png

image-04.png

Create a simple API

Follow the README of https://github.com/domaframework/doma-spring-boot/tree/1.4.0 And basically create it.

Define Entity

Reservation.java


package com.example.demo;

import org.seasar.doma.Entity;

import lombok.Data;

@Data
@Entity
public class Reservation {

	private Integer id;

	private String name;
}

Define Dao Interface

ReservationDao.java


package com.example.demo;

import java.util.List;

import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.boot.ConfigAutowireable;
import org.springframework.transaction.annotation.Transactional;

@ConfigAutowireable
@Dao
public interface ReservationDao {

	@Select
	public List<Reservation> selectAll();

	@Insert
	@Transactional
	public int insert(Reservation reservation);
}

Hover over ReservationDao # selectAll and right click> Doma> Jump to Sql File to generate an empty SQL file. (If you have added the Doma Tools plugin to Eclipse)

Write a query in a SQL file

selectAll.sql


SELECT
  id,
  name
FROM reservation
ORDER BY name ASC

Define Service and Controller classes

ReservationService.java


package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReservationService {

	@Autowired
	private ReservationDao reservationDao;

	public List<Reservation> selectAll() {
		return reservationDao.selectAll();
	}

	public int insert(Reservation reservation) {
		return reservationDao.insert(reservation);
	}
}

ReservationController.java


package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReservationController {

	@Autowired
	private ReservationService reservationService;

	@GetMapping(path = "/")
	public List<Reservation> selectAll() {
		return reservationService.selectAll();
	}

	@PostMapping(path = "/")
	public int insert(@RequestBody Reservation reservation) {
		return reservationService.insert(reservation);
	}

}

Create a Reservation table at startup

We use a Java in-memory DB called HSQLDB. If you place schema.sql directly under src / main / resources, you can run the table creation script at startup.

schema.sql


CREATE TABLE reservation (
  id   IDENTITY,
  NAME VARCHAR(50)
);

API operation check

POST (registration)

POST http://localhost:8080 HTTP/1.1
Content-Type: application/json

{
  "id": 1,
  "name": "Sample A"
}

response;

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 12 Jul 2020 15:09:30 GMT
Connection: close

1

GET (Get all)

GET http://localhost:8080 HTTP/1.1

response;

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 12 Jul 2020 15:10:20 GMT
Connection: close

[
  {
    "id": 1,
    "name": "Sample A"
  }
]

(Supplement) When a DOMA4019 error occurs

If you get a DOMA4019 error that the absolute path of the SQL file is not what you expected, as shown below.

[DOMA4019] The file "META-INF/com/example/demo/ReservationDao/selectAll.sql" is not found in the classpath. The absolute path is "C:\Git\springboot-doma2-sample\bin\main\META-INF\com\example\demo\ReservationDao\selectAll.sql".

Right-click> Properties> Modify Java build path on the target project.

image-05.png

Project default output folder ⇒ Modified to a specific output folder (bin / main).

image-06-01.png

At the end

The created application is stored in Repository. See if you want to see the full amount of build.gradle.

reference

Recommended Posts

Spring Boot, Doma2, Gradle initial setting summary
Spring Boot 2.2 Document Summary
Spring Boot 2 multi-project in Gradle
Spring Boot External setting priority
◆ Spring Boot + gradle environment construction memo
Database linkage with doma2 (Spring boot)
Implement Spring Boot application in Gradle
Spring Boot gradle build with Docker
Build Spring Boot + Docker image in Gradle
(IntelliJ + gradle) Hello World with Spring Boot
Add spring boot and gradle to eclipse
Summary of what I learned about Spring Boot
Run Scala applications with Spring Boot through Gradle
Challenge Spring Boot
Build Spring Boot project by environment with Gradle
Java tips-Create a Spring Boot project in Gradle
Spring Boot Form
I wanted to gradle spring boot with multi-project
Spring Boot Memorandum
gae + spring boot
View the Gradle task in the Spring Boot project
A memorandum of addiction to Spring Boot2 x Doma2
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
Execution of initial processing using Spring Boot Command Line Runner