HibernateはRed Hat開発のフリーソフト
pom.xml
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.5.6-Final</version> </dependency>
application.propertiesは変更なし
src/main/java/hibernate-config.java
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">1</property> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration>
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; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setRomaji(String romaji) { this.romaji = romaji; } }
SyainRepository.java
package com.example.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.springframework.stereotype.Repository; @Repository public class SyainRepository { @SuppressWarnings("unchecked") public List<Syain> findAll(){ List<Syain> syainList = null; SessionFactory sf = new Configuration().configure("hibernate-config.xml").addAnnotatedClass(Syain.class) .buildSessionFactory(); Session session = sf.getCurrentSession(); try { session.beginTransaction(); syainList = session.createQuery("from Syain").list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { sf.close(); } return syainList; } }
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(); } }
http://localhost:8080/test1
[{“id”:1,”name”:”鈴木”,”romaji”:”suzuki”},{“id”:2,”name”:”田中”,”romaji”:”tanaka”},{“id”:3,”name”:”佐藤”,”romaji”:”sato”},{“id”:4,”name”:”武田”,”romaji”:”takeda”}]
jdbc使えるなら、Hibernateを使う理由がイマイチわからんが、とにかくOK