[tlinh2110] BAEL 1684 Init (#3937)
This commit is contained in:
parent
1977fe28f0
commit
64cadbffeb
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.jdbc.autogenkey.repository;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||||
|
import org.springframework.jdbc.support.KeyHolder;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class MessageRepositoryJDBCTemplate {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
final String INSERT_MESSAGE_SQL = "insert into sys_message (message) values(?) ";
|
||||||
|
|
||||||
|
public long insert(final String message) {
|
||||||
|
|
||||||
|
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||||
|
|
||||||
|
jdbcTemplate.update(connection -> {
|
||||||
|
PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL);
|
||||||
|
ps.setString(1, message);
|
||||||
|
return ps;
|
||||||
|
}, keyHolder);
|
||||||
|
|
||||||
|
return (long) keyHolder.getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
final String SELECT_BY_ID = "select message from sys_message where id = ?";
|
||||||
|
|
||||||
|
public String getMessageById(long id) {
|
||||||
|
return this.jdbcTemplate.queryForObject(SELECT_BY_ID, String.class, new Object[] { id });
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.jdbc.autogenkey.repository;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class MessageRepositorySimpleJDBCInsert {
|
||||||
|
|
||||||
|
SimpleJdbcInsert messageInsert;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MessageRepositorySimpleJDBCInsert(DataSource dataSource) {
|
||||||
|
messageInsert = new SimpleJdbcInsert(dataSource).withTableName("sys_message").usingGeneratedKeyColumns("id");
|
||||||
|
}
|
||||||
|
|
||||||
|
public long insert(String message) {
|
||||||
|
Map<String, Object> parameters = new HashMap<String, Object>(1);
|
||||||
|
parameters.put("message", message);
|
||||||
|
Number newId = messageInsert.executeAndReturnKey(parameters);
|
||||||
|
return (long) newId;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
spring-5/src/main/resources/autogenkey-db.properties
Normal file
9
spring-5/src/main/resources/autogenkey-db.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.jpa.hibernate.ddl-auto=create
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
|
||||||
|
spring.datasource.initialize=true
|
||||||
|
spring.datasource.schema=classpath:autogenkey-schema.sql
|
5
spring-5/src/main/resources/autogenkey-schema.sql
Normal file
5
spring-5/src/main/resources/autogenkey-schema.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS sys_message (
|
||||||
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
message varchar(100) NOT NULL,
|
||||||
|
PRIMARY KEY (id)
|
||||||
|
);
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.jdbc.autogenkey;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.jdbc.autogenkey.repository.MessageRepositoryJDBCTemplate;
|
||||||
|
import com.baeldung.jdbc.autogenkey.repository.MessageRepositorySimpleJDBCInsert;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
public class GetAutoGenKeyByJDBC {
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@PropertySource("classpath:autogenkey-db.properties")
|
||||||
|
@ComponentScan(basePackages = { "com.baeldung.jdbc.autogenkey.repository" })
|
||||||
|
public static class SpringConfig {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MessageRepositorySimpleJDBCInsert messageRepositorySimpleJDBCInsert;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MessageRepositoryJDBCTemplate messageRepositoryJDBCTemplate;
|
||||||
|
|
||||||
|
final String MESSAGE_CONTENT = "Test";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insertJDBC_whenLoadMessageByKey_thenGetTheSameMessage() {
|
||||||
|
long key = messageRepositoryJDBCTemplate.insert(MESSAGE_CONTENT);
|
||||||
|
String loadedMessage = messageRepositoryJDBCTemplate.getMessageById(key);
|
||||||
|
|
||||||
|
assertEquals(MESSAGE_CONTENT, loadedMessage);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insertSimpleInsert_whenLoadMessageKey_thenGetTheSameMessage() {
|
||||||
|
long key = messageRepositorySimpleJDBCInsert.insert(MESSAGE_CONTENT);
|
||||||
|
String loadedMessage = messageRepositoryJDBCTemplate.getMessageById(key);
|
||||||
|
|
||||||
|
assertEquals(MESSAGE_CONTENT, loadedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
5
spring-5/src/test/resources/logback-test.xml
Normal file
5
spring-5/src/test/resources/logback-test.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||||
|
<logger name="org.springframework" level="INFO"/>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user