autogen key code cleanup
This commit is contained in:
-38
@@ -1,38 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user