ContactDaoSpring.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.contact;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import org.springframework.jdbc.core.PreparedStatementSetter;
  22. import org.springframework.jdbc.core.RowMapper;
  23. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  24. /**
  25. * Base implementation of {@link ContactDao} that uses Spring's JdbcTemplate.
  26. *
  27. * @author Ben Alex
  28. * @author Luke Taylor
  29. */
  30. public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
  31. // ~ Methods
  32. // ========================================================================================================
  33. public void create(final Contact contact) {
  34. getJdbcTemplate().update("insert into contacts values (?, ?, ?)",
  35. new PreparedStatementSetter() {
  36. public void setValues(PreparedStatement ps) throws SQLException {
  37. ps.setLong(1, contact.getId());
  38. ps.setString(2, contact.getName());
  39. ps.setString(3, contact.getEmail());
  40. }
  41. });
  42. }
  43. public void delete(final Long contactId) {
  44. getJdbcTemplate().update("delete from contacts where id = ?",
  45. new PreparedStatementSetter() {
  46. public void setValues(PreparedStatement ps) throws SQLException {
  47. ps.setLong(1, contactId);
  48. }
  49. });
  50. }
  51. public void update(final Contact contact) {
  52. getJdbcTemplate().update(
  53. "update contacts set contact_name = ?, address = ? where id = ?",
  54. new PreparedStatementSetter() {
  55. public void setValues(PreparedStatement ps) throws SQLException {
  56. ps.setString(1, contact.getName());
  57. ps.setString(2, contact.getEmail());
  58. ps.setLong(3, contact.getId());
  59. }
  60. });
  61. }
  62. public List<Contact> findAll() {
  63. return getJdbcTemplate().query(
  64. "select id, contact_name, email from contacts order by id",
  65. new RowMapper<Contact>() {
  66. public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
  67. return mapContact(rs);
  68. }
  69. });
  70. }
  71. public List<String> findAllPrincipals() {
  72. return getJdbcTemplate().queryForList(
  73. "select username from users order by username", String.class);
  74. }
  75. public List<String> findAllRoles() {
  76. return getJdbcTemplate().queryForList(
  77. "select distinct authority from authorities order by authority",
  78. String.class);
  79. }
  80. public Contact getById(Long id) {
  81. List<Contact> list = getJdbcTemplate().query(
  82. "select id, contact_name, email from contacts where id = ? order by id",
  83. new RowMapper<Contact>() {
  84. public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
  85. return mapContact(rs);
  86. }
  87. }, id);
  88. if (list.size() == 0) {
  89. return null;
  90. }
  91. else {
  92. return list.get(0);
  93. }
  94. }
  95. private Contact mapContact(ResultSet rs) throws SQLException {
  96. Contact contact = new Contact();
  97. contact.setId(rs.getLong("id"));
  98. contact.setName(rs.getString("contact_name"));
  99. contact.setEmail(rs.getString("email"));
  100. return contact;
  101. }
  102. }