您的位置:首页 > 博客中心 > 数据库 >

一个spring jdbc实例

时间:2022-03-10 17:13

一、使用示例
(1)springJdbcContext.xml

Java代码  gxlsystem.com,布布扣
    1. @Resource(name = "lobHandler")     
    2. private LobHandler lobHandler;     
    3.     
    4. @Resource(name = "jdbcTemplate")     
    5. private  JdbcTemplate jdbcTemplate;     
    6.     
    7. public void savePost(final Post post) {       
    8.    String sql = " INSERT INTO t_post(post_id,user_id,post_text,post_attach)"    
    9.      + " VALUES(?,?,?,?)";     
    10.    jdbcTemplate().execute(sql,     
    11.      new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {     
    12.          protected void setValues(PreparedStatement ps,     
    13.               LobCreator lobCreator) throws SQLException {     
    14.                   ps.setInt(1, incre.nextIntValue());      
    15.                   ps.setInt(2, post.getUserId());      
    16.                   lobCreator.setClobAsString(ps, 3, post.getPostText());     
    17.                   lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());     
    18.         }  
    19.      });     
    20. }     
    21.     
    22. public List findAttachs(final int userId){     
    23.    String sql = "SELECT post_id,post_attach FROM t_post where user_id =? and post_attach is not null";     
    24.    return jdbcTemplate().query( sql, new Object[] {userId},     
    25.       new RowMapper() {     
    26.           public Object mapRow(ResultSet rs, int rowNum) throws SQLException {     
    27.           Post post = new Post();     
    28.           int postId = rs.getInt(1);     
    29.           byte[] attach = lobHandler.getBlobAsBytes(rs, 2);     
    30.           post.setPostId(postId);     
    31.           post.setPostAttach(attach);     
    32.           return post;     
    33.       }    
    34.    });    
    35. }   

一个spring jdbc实例,布布扣,bubuko.com

热门排行

今日推荐

热门手游