一个spring jdbc实例
时间:2022-03-10 17:13
一、使用示例
(1)springJdbcContext.xml
- @Resource(name = "lobHandler")
- private LobHandler lobHandler;
- @Resource(name = "jdbcTemplate")
- private JdbcTemplate jdbcTemplate;
- public void savePost(final Post post) {
- String sql = " INSERT INTO t_post(post_id,user_id,post_text,post_attach)"
- + " VALUES(?,?,?,?)";
- jdbcTemplate().execute(sql,
- new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
- protected void setValues(PreparedStatement ps,
- LobCreator lobCreator) throws SQLException {
- ps.setInt(1, incre.nextIntValue());
- ps.setInt(2, post.getUserId());
- lobCreator.setClobAsString(ps, 3, post.getPostText());
- lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());
- }
- });
- }
- public List findAttachs(final int userId){
- String sql = "SELECT post_id,post_attach FROM t_post where user_id =? and post_attach is not null";
- return jdbcTemplate().query( sql, new Object[] {userId},
- new RowMapper() {
- public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
- Post post = new Post();
- int postId = rs.getInt(1);
- byte[] attach = lobHandler.getBlobAsBytes(rs, 2);
- post.setPostId(postId);
- post.setPostAttach(attach);
- return post;
- }
- });
- }
一个spring jdbc实例,布布扣,bubuko.com