[JAVA] Build a WEB system with Spring + Doma + H2DB Part 2

Introduction

Like last time, I will make it with reference to here.

Introducing Spring Tool Suite (STS) and Doma Tools

Introduce it because it is convenient for development

Introduction of STS

  1. Search for STS in Help → ʻEclipse Marketplace` and install it.
  2. Restart Eclipse after the installation is complete

Introduction of Doma Tools

  1. Select http://dl.bintray.com/domaframework/eclipse/ in Help Install new software. Enter
  2. Check Doma Tools and Next
  3. Restart Eclipse after installation is complete

Create a simple Select and Insert process

Creating Entity

  1. Create an entity package
  2. Create TestEntity in entity package

TestEntity.java


package com.tharao.createdesigndocuments.entity;

import org.seasar.doma.Entity;
import org.seasar.doma.GeneratedValue;
import org.seasar.doma.GenerationType;
import org.seasar.doma.Id;

@Entity
public class TestEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer id;
	public String name;

}

Create Dao

  1. Create dao package
  2. Create TestEntityDao in dao package

TestEntityDao.java


package com.tharao.createdesigndocuments.dao;

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;

import com.tharao.createdesigndocuments.entity.TestEntity;

@ConfigAutowireable
@Dao
public interface TestEntityDao {

	@Select
	List<TestEntity> selectAll();
	
	@Insert
	@Transactional
	int insert(TestEntity reservation);

}

Creating a Service

  1. Create a service package
  2. Create TestService in service package

TestService.java


package com.tharao.createdesigndocuments.service;

import java.util.List;

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

import com.tharao.createdesigndocuments.dao.TestEntityDao;
import com.tharao.createdesigndocuments.entity.TestEntity;

@Service
@Transactional
public class TestService {

	@Autowired
	TestEntityDao dao;

	public List<TestEntity> getAllEntities() {
		return dao.selectAll();
	}

}

Creating a Controller

  1. Create controller package
  2. Create a TestController in the controlle package

TestService.java


package com.tharao.createdesigndocuments.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tharao.createdesigndocuments.entity.TestEntity;
import com.tharao.createdesigndocuments.service.TestService;

@RestController
public class TestController {

	@Autowired
	TestService service;

	@RequestMapping(value = "test", method = RequestMethod.GET)
	public List<TestEntity> getEntities() {
		return service.getAllEntities();
	}

}

Creating a SQL file

  1. Right-click on the selectAll method of the Dao interface and select DomaJump to Slq File to create it.
  2. Add selectAll.sql

selectAll.sql


select
	id
	,name
from
	TestEntity
order by
	id asc

Creating an application

Enter initial data

CreateDesignDocumentsApplication.java


package com.tharao.createdesigndocuments;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.tharao.createdesigndocuments.dao.TestEntityDao;
import com.tharao.createdesigndocuments.entity.TestEntity;

@SpringBootApplication
public class CreateDesignDocumentsApplication {

	public static void main(String[] args) {
		SpringApplication.run(CreateDesignDocumentsApplication.class, args);
	}

	@Autowired
	TestEntityDao testEntityDao;

	//Reservation Dao at startup#Insert initial data with insert
	@Bean
	CommandLineRunner runner() {
		return args -> Arrays.asList("spring", "spring boot", "doma").forEach(s -> {
			TestEntity r = new TestEntity();
			r.name = s;
			testEntityDao.insert(r);
		});
	}

}

Then set the SQL dialect in ʻapplication.properties`

application.properties


doma.dialect=h2

apt (annotation processing) settings

Follow the official Doma setup and set up apt.

  1. View the Eclipse project properties and select Java CompilerAnnotation Processing

  2. Check Allow project-specific settings

  3. Set the generated source directory to .apt_generated

  4. Add the following to processor options

    ey value
    ao.subpackage impl
  5. Select Java CompilerAnnotation Processing Factory Path

  6. Check Allow project-specific settings

  7. Add the doma jar to the annotation processor included plugin and JAR and press ʻOK`

Try to move

When I accessed [http: // localhost: 8080 / test](http: // localhost: 8080 / test), the data was safely displayed in JSON format. test.png

Where I was addicted

I created the SQL files under src / main / resources, but all the files are set in the exclusion setting of src / main / resources in the Java build path of the project, created by DaoImpl Was not done. When I set the exclusion of src / main / resources to none, DaoImpl was successfully created and compiled.

Referenced URL Use Spring Boot + Doma2

Recommended Posts

Build a WEB system with Spring + Doma + H2DB Part 2
Build a WEB system with Spring + Doma + H2DB
Build a WEB system with Spring + Doma + H2DB + Thymeleaf
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
Create a web api server with spring boot
Build Doma1 with Ant
The first WEB application with Spring Boot-Making a Pomodoro timer-
Create a Hello World web app with Spring framework + Jetty
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Until you create a Web application with Servlet / JSP (Part 1)
Implement a simple Web REST API server with Spring Boot + MySQL
Build a Java project with Gradle
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Database linkage with doma2 (Spring boot)
Spring Boot gradle build with Docker
Creating a java web application development environment with docker for mac part1
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Create a java web application development environment with docker for mac part2
Create a simple web application with Dropwizard
Build a PureScript development environment with Docker
Create a simple on-demand batch with Spring Batch
Start web application development with Spring Boot
Extract a part of a string with Ruby
Build a Wordpress development environment with Docker
De-cron! Build a job scheduler with Rundeck
Run WEB application with Spring Boot + Thymeleaf
I tried to clone a web application full of bugs with Spring Boot
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple search app with Spring Boot
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
How to use built-in h2db with spring boot
Get Body part of HttpResponse with Filter of Spring
[Win10] Build a JSF development environment with NetBeans
Creating a common repository with Spring Data JPA
Create a Spring Boot development environment with docker
The road to creating a Web service (Part 1)
Build a Java development environment with VS Code
A story that stumbled when deploying a web application created with Spring Boot to EC2
I made a function to register images with API in Spring Framework. Part 1 (API edition)