When developing with Eclipse, I'm always addicted to the initial settings of Spring Boot + Doma2 + Gradle, so I will summarize it for myself.
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'
}
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.
Follow the README of https://github.com/domaframework/doma-spring-boot/tree/1.4.0 And basically create it.
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;
}
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)
selectAll.sql
SELECT
id,
name
FROM reservation
ORDER BY name ASC
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);
}
}
Reservation
table at startupWe 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)
);
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"
}
]
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.
Project default output folder ⇒ Modified to a specific output folder (bin / main
).
The created application is stored in Repository. See if you want to see the full amount of build.gradle
.
Recommended Posts