### Postgresにデータ挿入
psql -U root test
CREATE TABLE syain (
id SERIAL NOT NULL,
name varchar(255),
romaji varchar(255),
PRIMARY KEY(id)
);
INSERT INTO syain (name, romaji) VALUES (‘鈴木’,’suzuki’);
INSERT INTO syain (name, romaji) VALUES (‘田中’,’tanaka’);
INSERT INTO syain (name, romaji) VALUES (‘佐藤’,’sato’);
test=> select * from syain;
id | name | romaji
—-+——+——–
1 | 鈴木 | suzuki
2 | 田中 | tanaka
3 | 佐藤 | sato
(3 rows)
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
application.properties
spring.jpa.database=POSTGRESQL spring.datasource.url=jdbc:postgresql://localhost:5432/test spring.datasource.username=root spring.datasource.password= spring.jpa.hibernate.ddl-auto=validate spring.jpa.properties.hibernate.format_sql=true spring.datasource.sql-script-encoding=UTF-8
Syain.java
package com.example.demo; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Id; @Entity @Table(name="syain") public class Syain { @Id private Integer id; private String name; private String romaji; public Integer getId() { return id; } public String getName() { return name; } public String getRomaji() { return romaji; } }
SyainRepository.java
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; public interface SyainRepository extends JpaRepository<Syain, Integer>{ }
MainController.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.RestController; @RestController public class MainController { @Autowired private SyainRepository syainRepository; @GetMapping("/test1") public List<Syain> get() { return syainRepository.findAll(); } }
うおおおおおおおおおおおおおおおおお
きたあああああああああああああああああああああああ