I think it's a unique way of writing Spring Framework, but I do and wear Java such as property files and xml files. I think it's quite a beginner killing, so I summarized the use of JPA.
In addition, what is required for JPA ・ Spring Data JPA library ・ Entity class -Persistence.xml file (Java EJB function) -Bean configuration file (xml) -Execution class with main method
Check the console to see if the query arrived in the database. Use on the web later.
pom settings.
Add to pom and add dependencies. I'm using Spring IO pratform, so I won't describe the version!
pom.xml
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- postgresql database -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
After drawing, right click ** Maven → updateProject **
I will give Spring information about the PostgreSQL driver. Create a property file of ** database.properties ** in src / main / resource.
database.properties
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/sample
jdbc.username=postgres
jdbc.password=password
If it's PostgreSQL, it looks like this, according to the DB you use.
Register the DataSource and JdbcTemplate in the DI container. Create ** bean.xml ** in src / main / resource
You read the value from the property file and register it in the bean.
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/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>
<!--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>
Let's take this out of DI and run it Access the database with ** execute ** and ** queryForList ** of JdbcTemplate!
App.java
@Component
@ComponentScan
public class App {
//private static JdbcTemplate jdbcTemplate;
private static ApplicationContext context;
private static JdbcTemplate jdbcTemplate;
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);
List<Map<String, Object>> list = jdbcTemplate
.queryForList("SELECT * FROM mytable");
for(Map<String, Object> obj : list) {
System.out.println(obj);
}
}
}
Do this and watch
At the bottom you can see that the columns have been retrieved from the database.
Recommended Posts