@Test public void testAddForum() { Forum forum = new Forum(); forum.setForumName("1二手市场"); forum.setForumDesc("1二手货物的交流论坛。"); forumDao.addForum(forum); System.out.println(forum.getForumId()); }
public void addForum(final Forum forum) { final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue()); }
ForumDao { public void addForum(final Forum forum) { final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue()); } }
ForumDao { public void addForum(final Forum forum) { final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue()); }  }
ForumDao { public void addForum(final Forum forum) { final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue()); }  @Autowired void setJdbcTemplate(JdbcTemplate jdbcTemplate); @Autowired void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate); void initDb(); void addForum(final Forum forum); void addForumByNamedParams(final Forum forum); void addForums(final List<Forum> forums); Forum getForum(final int forumId); List<Forum> getForums(final int fromId, final int toId); }
ForumDao { public void addForum(final Forum forum) { final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue()); }  @Autowired void setJdbcTemplate(JdbcTemplate jdbcTemplate); @Autowired void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate); void initDb(); void addForum(final Forum forum); void addForumByNamedParams(final Forum forum); void addForums(final List<Forum> forums); Forum getForum(final int forumId); List<Forum> getForums(final int fromId, final int toId);  }