Note
--Enable PostgreSQL with Spring Boot --Make a simple app
--SpringToolSuite 3.8.4 (hereinafter STS)
--Spring Tool Suite installed --PostgreSQL has already been installed (this time, the one installed in the host OS is used)
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);
Create a project using Spring Initializr
Press the Generate Project
button to download the project folder.
Then ** unzip ** and leave it wherever you like
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
Access Spring IO platform 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-->
~~~
/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
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;
}
}
java:/src/main/resources/com.takahashi.PersonRepository.java
@Repository
public interface PersonRepository extends JpaRepository<Person,Integer>{
}
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"));
}
}
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();
}
}
Select a project and right click
Run as -> Spring Boot App
If it is displayed as above, it is OK
https://www.slideshare.net/shintanimoto/spring-boot10 http://dev.classmethod.jp/server-side/java/using_spring_boot_2/
https://github.com/aiue1500/bootpostgres
Recommended Posts