[JAVA] How to set Spring Boot + PostgreSQL

Note

Overview

--Enable PostgreSQL with Spring Boot --Make a simple app

environment

--SpringToolSuite 3.8.4 (hereinafter STS)

Premise

--Spring Tool Suite installed --PostgreSQL has already been installed (this time, the one installed in the host OS is used)

procedure

  1. DB settings
  2. Create a project
  3. Project settings
  4. Create a model controller repository service
  5. Run

Preparation

Create a DB in advance before creating a project

PostgreSQL


CREATE DATABASE testdb;
 (Don't forget to switch databases)
CREATE TABLE person (
id SERIAL PRIMARY KEY
, name VARCHAR(16)
, age INT);
 
INSERT INTO person VALUES
 (1,'taro',19)
 , (2,'tadokoro',24)
 , (3,'hanako',14);

Introduction

Create a new project

Create a project using Spring Initializr 1.png

Press the Generate Project button to download the project folder. Then ** unzip ** and leave it wherever you like

Project import

Select the [Directory] button from the File-> Open Projects From File Systems .. on the STS tab and select the project folder you just unzipped. Make sure Import as is Maven and select Finish

Environmental setting

Rewriting pom.xml

Access Spring IO platform springioplatform.png Copy the settings description in QuickStart and add it to your pom.xml

/bootpostgres/pom.xml


~~~
</properties>

<!--from here-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.spring.platform</groupId>
            <artifactId>platform-bom</artifactId>
            <version>Brussels-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<!--So far-->

<dependencies>
  <!--Also add the following-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
  </dependency>
  <!--Add up to here-->
~~~

Add connection information to PostgreSQL

/src/main/resources/application.properties


spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=hogehoge

That's all for environment settings

Creating a model

java:src/main/java/com.takahashi.Person.java/


@Entity
@Table(name = "person")
public class Person {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  
  @Column(name="name")
  private String name;
  
  @Column(name="age")
  private int age;

  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

Creating a repository

java:/src/main/resources/com.takahashi.PersonRepository.java


@Repository
public interface PersonRepository extends JpaRepository<Person,Integer>{

}

Creating a service

java:/src/main/resources/com.takahashi.PersonService.java


@Service
@Transactional
public class PersonService {

  @Autowired
  PersonRepository repository;
  
  public List<Person> findAll(){
    return repository.findAll(new Sort(Sort.Direction.ASC,"id"));
  } 
}

Creating a controller

java:/src/main/resources/com.takahashi.PersonController.java


@RestController
@RequestMapping("/person")
public class PersonController {
  
  @Autowired
  PersonService service;
  
  @RequestMapping(value="/",method=RequestMethod.GET)
  public List<Person> index(){
    return service.findAll();
  }
  
}

Execute

Select a project and right click Run as -> Spring Boot App zikkoukekka.png

If it is displayed as above, it is OK

Reference site

https://www.slideshare.net/shintanimoto/spring-boot10 http://dev.classmethod.jp/server-side/java/using_spring_boot_2/

Sample project

https://github.com/aiue1500/bootpostgres

Recommended Posts

How to set Spring Boot + PostgreSQL
Spring Boot --How to set session timeout time
How to use ModelMapper (Spring boot)
How to split Spring Boot message file
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to add a classpath in Spring Boot
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
Set context-param in Spring Boot
Introduction to Spring Boot ① ~ DI ~
How to set Docker nginx
Introduction to Spring Boot ② ~ AOP ~
How to set Java constants
Set cookies with Spring Boot
Introduction to Spring Boot Part 1
How to write a unit test for Spring Boot 2
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to use CommandLineRunner in Spring Batch of Spring Boot
How to boot by environment with Spring Boot of Maven
How to set environment variables in the properties file of Spring boot application
How to use Lombok in Spring
How to unit test Spring AOP
Spring Boot + PostgreSQL error resolution method
How to change application.properties settings at boot time in Spring boot
How to use Spring Data JDBC
[How to install Spring Data Jpa]
How to call and use API in Java (Spring Boot)
Upgrade spring boot from 1.5 series to 2.0 series
How to set Lombok in Eclipse
How to control transactions in Spring Boot without using @Transactional
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to create your own Controller corresponding to / error with Spring Boot
How to set and use profile in annotation-based Configuration in Spring framework
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
Set Spring Security authentication result to JSON
[Introduction to Spring Boot] Form validation check
How to include Spring Tool in Eclipse 4.6.3?
Story when moving from Spring Boot 1.5 to 2.1
How to set up and use kapt
[Java] How to set the Date time to 00:00:00
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
How to set JAVA_HOME with Maven appassembler-maven-plugin
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
[Spring MVC] How to pass path variables
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to write Spring AOP pointcut specifier
Add spring boot and gradle to eclipse
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
[Spring Boot] I investigated how to implement post-processing of the received request.
How to read Body of Request multiple times with Spring Boot + Spring Security
How to deploy
Challenge Spring Boot
[Spring Boot Actuator] How to manually register your own health check process
How to not start Flyway when running unit tests in Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot