From 6561af5cd2c138d96a013dee999a1ff7a6126e8c Mon Sep 17 00:00:00 2001 From: linhvovn Date: Mon, 16 Apr 2018 03:49:12 +0800 Subject: [PATCH] Bael 1684 - Move to spring-jpa (#4015) * [tlinh2110] BAEL 1684 Init * [BAEL1684-tlinh2110] Move to spring-jpa * [BAEL-1684 tlinh2110] Remove unused file --- .../autogenkey/config/PersistenceConfig.java | 28 +++++++++++ .../MessageRepositoryJDBCTemplate.java | 38 ++++++++++++++ .../MessageRepositorySimpleJDBCInsert.java | 29 +++++++++++ .../src/main/resources/autogenkey-schema.sql | 5 ++ .../jdbc/autogenkey/GetAutoGenKeyByJDBC.java | 49 +++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/config/PersistenceConfig.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositoryJDBCTemplate.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositorySimpleJDBCInsert.java create mode 100644 persistence-modules/spring-jpa/src/main/resources/autogenkey-schema.sql create mode 100644 persistence-modules/spring-jpa/src/test/java/com/baeldung/jdbc/autogenkey/GetAutoGenKeyByJDBC.java diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/config/PersistenceConfig.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/config/PersistenceConfig.java new file mode 100644 index 0000000000..99f50abf4c --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/config/PersistenceConfig.java @@ -0,0 +1,28 @@ +package com.baeldung.jdbc.autogenkey.config; + +import javax.sql.DataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +@Configuration +public class PersistenceConfig { + + @Bean + public DataSource dataSource(Environment env) { + return new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("autogenkey-schema.sql") + .build(); + } + + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositoryJDBCTemplate.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositoryJDBCTemplate.java new file mode 100644 index 0000000000..cf0dbe4681 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositoryJDBCTemplate.java @@ -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 }); + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositorySimpleJDBCInsert.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositorySimpleJDBCInsert.java new file mode 100644 index 0000000000..022ea29ed6 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/jdbc/autogenkey/repository/MessageRepositorySimpleJDBCInsert.java @@ -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 parameters = new HashMap(1); + parameters.put("message", message); + Number newId = messageInsert.executeAndReturnKey(parameters); + return (long) newId; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/resources/autogenkey-schema.sql b/persistence-modules/spring-jpa/src/main/resources/autogenkey-schema.sql new file mode 100644 index 0000000000..925b27143c --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/resources/autogenkey-schema.sql @@ -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) +); \ No newline at end of file diff --git a/persistence-modules/spring-jpa/src/test/java/com/baeldung/jdbc/autogenkey/GetAutoGenKeyByJDBC.java b/persistence-modules/spring-jpa/src/test/java/com/baeldung/jdbc/autogenkey/GetAutoGenKeyByJDBC.java new file mode 100644 index 0000000000..aa4d061997 --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/java/com/baeldung/jdbc/autogenkey/GetAutoGenKeyByJDBC.java @@ -0,0 +1,49 @@ +package com.baeldung.jdbc.autogenkey; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +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 + @ComponentScan(basePackages = { "com.baeldung.jdbc.autogenkey" }) + 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); + } + +}