For the time being, I used Spring to connect to the DB. From here, we will use a full-fledged DB operation framework called JPA.
In addition, what is required for JPA ** ← From here ** ・ Spring Data JPA library ・ Entity class -Persistence.xml file (Java EJB function) -Bean configuration file (xml) -Execution class with main method
Add library as usual. When you use new features in Spring, you usually edit the library.
pom.xml
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
After saving ** Maven → update Project **
Click here for how to create an entity class from a table. wrap up Right click and convert from configuration to JPA project. When it becomes a JPA project, an entity will be automatically generated from the table.
https://qiita.com/shibafu/items/39f3f5d6e63dda16bb12
It's really hard to see,
When the Entity is created, add an annotation such as @Id and pick up the error. You may want to use ** lombok ** and omit the getter setter in @Data
Auto-implemented entity class
Create a META-INF folder in src / main / resources and a persistance.xml file in it. It seems to be a Java-EE function called ** persistence file ** (If you created it with JPA, it is created in src / main / java, so move it)
Sketch src/main/resources/ | |---META-INF | | | |--persistance.xml |---bean.xml |---database.properties
persitance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="persistance-unit">
<!--HibernatePersistance definition-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>model.Mypersonaldata</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create" />
<!--Driver properties-->
<property name="javax.persistance.jdbc.driver"
value="${jdbc.driverClassname}" />
<property name="javax.persistance.jdbc.url"
value="${jdbc.url}" />
<property name="javax.persistance.jdbc.user"
value="${jdbc.username}" />
<property name="javax.persistance.jdbc.password"
value="${jdbc.password}" />
</properties>
</persistence-unit>
</persistence>
Finally, create a bean configuration file. Namespace xmlns = http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/spring-jpa.xsd Add Register the entity manager with the DI container.
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--Settings for embedded database>
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:script.sql"/>
</jdbc:embedded-database-->
<!--Setting database properties-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties" />
</bean>
<!--EntityManager settings-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<!--Settings for DB connection-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--Creating a JDBC template bean-->
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<!--JPA Repository settings-->
<jpa:repositories base-package="com.TsugaruInfo.repository" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Let's put the entity manager in the main class and perform JPA functions!
App.java
public class App{
public static void main(String args[]) {
//Creating a template using annotations
//context = new AnnotationConfigApplicationContext(DataSourceConfig.class);
//JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean(JdbcTemplate.class);
//Get context
// context = new ClassPathXmlApplicationContext("bean.xml");
// jdbcTemplate = context.getBean(JdbcTemplate.class);
context = new ClassPathXmlApplicationContext("bean.xml");
LocalContainerEntityManagerFactoryBean factory =
context.getBean(LocalContainerEntityManagerFactoryBean.class);
manager = factory.getNativeEntityManagerFactory().createEntityManager();
Mypersonaldata data = manager.find(Mypersonaldata.class, 1);
System.out.println(data);
}
}
Run this from Java Application
At the bottom, you can see the data read from postgresql.
Well, with Spring-Boot, this procedure is all done automatically, I couldn't keep the basics down and it stopped working, so for reference.
Recommended Posts