@Test public void testgetAttachs() throws Throwable{ Post post = new Post(); post.setUserId(2); ClassPathResource res = new ClassPathResource("temp.jpg"); byte[] mockImg = FileCopyUtils.copyToByteArray(res.getFile()); post.setPostAttach(mockImg); post.setPostText("测试帖子的内容"); postDao.addPost(post); List<Post> list = postDao.getAttachs(3); for (Post tempPost:list) { System.out.println(tempPost.getPostId()+";"+post.getPostAttach().length); } }
public List<Post> getAttachs(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<Post>() { public Post mapRow(ResultSet rs, int rowNum) throws SQLException { int postId = rs.getInt(1); byte[] attach = lobHandler.getBlobAsBytes(rs, 2); Post post = new Post(); post.setPostId(postId); post.setPostAttach(attach); return post; } }); }
PostDao { public List<Post> getAttachs(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<Post>() { public Post mapRow(ResultSet rs, int rowNum) throws SQLException { int postId = rs.getInt(1); byte[] attach = lobHandler.getBlobAsBytes(rs, 2); Post post = new Post(); post.setPostId(postId); post.setPostAttach(attach); return post; } }); } }
PostDao { public List<Post> getAttachs(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<Post>() { public Post mapRow(ResultSet rs, int rowNum) throws SQLException { int postId = rs.getInt(1); byte[] attach = lobHandler.getBlobAsBytes(rs, 2); Post post = new Post(); post.setPostId(postId); post.setPostAttach(attach); return post; } }); }  }
PostDao { public List<Post> getAttachs(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<Post>() { public Post mapRow(ResultSet rs, int rowNum) throws SQLException { int postId = rs.getInt(1); byte[] attach = lobHandler.getBlobAsBytes(rs, 2); Post post = new Post(); post.setPostId(postId); post.setPostAttach(attach); return post; } }); }  @Autowired void setJdbcTemplate(JdbcTemplate jdbcTemplate); @Autowired void setLobHandler(LobHandler lobHandler); @Autowired void setIncre(DataFieldMaxValueIncrementer incre); void addPost(final Post post); void getNativeConn(); List<Post> getAttachs(final int userId); void getAttach(final int postId, final OutputStream os); }
PostDao { public List<Post> getAttachs(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<Post>() { public Post mapRow(ResultSet rs, int rowNum) throws SQLException { int postId = rs.getInt(1); byte[] attach = lobHandler.getBlobAsBytes(rs, 2); Post post = new Post(); post.setPostId(postId); post.setPostAttach(attach); return post; } }); }  @Autowired void setJdbcTemplate(JdbcTemplate jdbcTemplate); @Autowired void setLobHandler(LobHandler lobHandler); @Autowired void setIncre(DataFieldMaxValueIncrementer incre); void addPost(final Post post); void getNativeConn(); List<Post> getAttachs(final int userId); void getAttach(final int postId, final OutputStream os);  }