Guest User

Untitled

a guest
Mar 8th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package spittr.db.jdbc;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. import org.springframework.jdbc.core.RowMapper;
  11. import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
  12.  
  13. import spittr.db.SpitterRepository;
  14. import spittr.domain.Spitter;
  15.  
  16. public class JdbcSpitterRepository implements SpitterRepository {
  17.  
  18. private JdbcTemplate jdbcTemplate;
  19.  
  20. public JdbcSpitterRepository(JdbcTemplate jdbcTemplate) {
  21. this.jdbcTemplate = jdbcTemplate;
  22. }
  23.  
  24. public long count() {
  25. return jdbcTemplate.queryForLong("select count(id) from Spitter");
  26. }
  27.  
  28. public Spitter save(Spitter spitter) {
  29. Long id = spitter.getId();
  30. if (id == null) {
  31. long spitterId = insertSpitterAndReturnId(spitter);
  32. return new Spitter(spitterId, spitter.getUsername(), spitter.getPassword(), spitter.getFullName(), spitter.getEmail(), spitter.isUpdateByEmail());
  33. } else {
  34. jdbcTemplate.update("update Spitter set username=?, password=?, fullname=?, email=?, updateByEmail=? where id=?",
  35. spitter.getUsername(),
  36. spitter.getPassword(),
  37. spitter.getFullName(),
  38. spitter.getEmail(),
  39. spitter.isUpdateByEmail(),
  40. id);
  41. }
  42. return spitter;
  43. }
  44.  
  45. /**
  46. * Inserts a spitter using SimpleJdbcInsert.
  47. * Involves no direct SQL and is able to return the ID of the newly created Spitter.
  48. * @param spitter a Spitter to insert into the databse
  49. * @return the ID of the newly inserted Spitter
  50. */
  51. private long insertSpitterAndReturnId(Spitter spitter) {
  52. SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("Spitter");
  53. jdbcInsert.setGeneratedKeyName("id");
  54. Map<String, Object> args = new HashMap<String, Object>();
  55. args.put("username", spitter.getUsername());
  56. args.put("password", spitter.getPassword());
  57. args.put("fullname", spitter.getFullName());
  58. args.put("email", spitter.getEmail());
  59. args.put("updateByEmail", spitter.isUpdateByEmail());
  60. long spitterId = jdbcInsert.executeAndReturnKey(args).longValue();
  61. return spitterId;
  62. }
  63.  
  64. /**
  65. * Inserts a spitter using a simple JdbcTemplate update() call.
  66. * Does not return the ID of the newly created Spitter.
  67. * @param spitter a Spitter to insert into the database
  68. */
  69. @SuppressWarnings("unused")
  70. private void insertSpitter(Spitter spitter) {
  71. jdbcTemplate.update(INSERT_SPITTER,
  72. spitter.getUsername(),
  73. spitter.getPassword(),
  74. spitter.getFullName(),
  75. spitter.getEmail(),
  76. spitter.isUpdateByEmail());
  77. }
  78.  
  79. public Spitter findOne(long id) {
  80. return jdbcTemplate.queryForObject(
  81. SELECT_SPITTER + " where id=?", new SpitterRowMapper(), id);
  82. }
  83.  
  84. public Spitter findByUsername(String username) {
  85. return jdbcTemplate.queryForObject("select id, username, password, fullname, email, updateByEmail from Spitter where username=?", new SpitterRowMapper(), username);
  86. }
  87.  
  88. public List<Spitter> findAll() {
  89. return jdbcTemplate.query("select id, username, password, fullname, email, updateByEmail from Spitter order by id", new SpitterRowMapper());
  90. }
  91.  
  92. private static final class SpitterRowMapper implements RowMapper<Spitter> {
  93. public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException {
  94. long id = rs.getLong("id");
  95. String username = rs.getString("username");
  96. String password = rs.getString("password");
  97. String fullName = rs.getString("fullname");
  98. String email = rs.getString("email");
  99. boolean updateByEmail = rs.getBoolean("updateByEmail");
  100. return new Spitter(id, username, password, fullName, email, updateByEmail);
  101. }
  102. }
  103.  
  104. private static final String INSERT_SPITTER = "insert into Spitter (username, password, fullname, email, updateByEmail) values (?, ?, ?, ?, ?)";
  105.  
  106. private static final String SELECT_SPITTER = "select id, username, password, fullname, email, updateByEmail from Spitter";
  107.  
  108. }
Add Comment
Please, Sign In to add comment