title: Generic DAO pattern Spring JDBC Template
body: <p>It's been a while since had to implement this pattern from the beginning so I am a bit confused on what I am missing about using this pattern with spring jdbc.  Here is a snippet from my BaseDao:</p>
  
  <pre><code>    protected BaseDao(RowMapper&lt;T&gt; rowMapper, String tableName, JdbcTemplate jdbcTemplate)
     {
        this.rowMapper = rowMapper;
        this.tableName = tableName;
        this.findByIdSql = "SELECT * FROM " + tableName + " WHERE id = ?";
        this.jdbcTemplate = jdbcTemplate;
     }
  
     public T findById(final Integer id)
     {
        if (id == null)
        {
           return null;
        }
  
        Object[] params = { id };
        return jdbcTemplate.queryForObject(findByIdSql, params, rowMapper);
     }
  
     public T save(final T dto)
     {
        if (dto == null)
        {
           return null;
        }
  
        try
        {
           if (dto.isNew())
           {
              final Integer newId = jdbcTemplate.update("INSERT INTO " + tableName);
           }
        }
        catch (UncategorizedSQLException e)
        {
           logger.error("****************" + e.getMessage(), e);
           throw new RuntimeException("Could not persist: " + e.getMessage());
        }
     }
  
     public void delete(final T dto)
     {
        if (dto == null)
        {
           final String errorMessage = "Can't delete a null object";
           logger.warn(errorMessage);
           throw new RuntimeException(errorMessage);
        }
  
        if (!dto.cameFromDatabase())
        {
           throw new RuntimeException("Can't delete an object that didn't come from the database");
        }
  
        try
        {
  
        }
        catch (JdbcUpdateAffectedIncorrectNumberOfRowsException e)
        {
           final String message = "The delete failed on " + dto + ".  " + e.getMessage();
           logger.error(message);
           throw new RuntimeException(message, e);
  
        }
     }
  </code></pre>
  
  <p>As you can see I got my findById working and able to handle any class thrown at it.  The trouble is I don't recall on how to "generify" the save (handles insert and update) and delete using simply the jdbctemplate.  Can this not be accomplished?  Does every DAO need to define these two methods itself?  I don't see how jdbc template can flex to writing seperate insert, update, delete statements.  Browsing the web I see tons of examples using hibernate or an entitymanager.  Is this the route I should take?  Or what is the plainly obvious step I am missing here?</p>
  
  <p>I know the save and delete are not filled out but I'm just wondering how to handle the line </p>
  
  <pre><code>final Integer newId = jdbcTemplate.update("INSERT INTO " + tableName);
  </code></pre>
  
  <p>To handle any DTO thrown at it.</p>
  
  <p>Thank you kindly!</p>