Spring2.0久违了的SQL语句参数支持

本文介绍了Spring2.0中的新特性,特别是针对数据库操作的支持,包括参数支持、使用Java5特性的代码示例,以及这些改进如何提高开发效率和减少错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天随便翻了翻spring2.0的文档,发现了许多非常近似.net的功能,这大大提高了对数据操作的准确性
看看下面这段代码

public   int  countOfActorsByFirstName(String firstName)  {

    String sql 
= "select count(0) from T_ACTOR where first_name =?";

    Object[] obj
=new Object[1];
    
    obj[
0]=firstName;

    
return template.queryForInt(sql, obj);
}

这段代码是没有参数支持前的用法。"?"作为参数时需要你在object中格外小心参数的顺序,这一点很令人讨厌

public   int  countOfActorsByFirstName(String firstName)  {

    String sql 
= "select count(0) from T_ACTOR where first_name = :first_name";

    NamedParameterJdbcTemplate template 
= new NamedParameterJdbcTemplate(this.getDataSource());
    Map namedParameters 
= new HashMap();
    namedParameters.put(
"first_name", firstName);

    
return template.queryForInt(sql, namedParameters);
}

看看Spring2.0中新添加的参数支持功能,这个在java中可望而不可及的功能被实现了!我觉得这一点真的很重要,不但加快了开发速度而且还大大的减少了错误的发生机率。

接下来的代码则是用了java5的特性给我们带来的方便

public  Actor findActor( long  id)  {
    String sql 
= "select id, first_name, last_name from T_ACTOR where id = ?";

    ParameterizedRowMapper
<Actor> mapper = new ParameterizedRowMapper()<Actor> {
    
        
// notice the return type (c.f. Java 5 covariant return types)
        public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
            Actor actor 
= new Actor();
            actor.setId(rs.getLong(
"id"));
            actor.setFirstName(rs.getString(
"first_name"));
            actor.setLastName(rs.getString(
"last_name"));
            
return actor;
        }

    }
;

    
// again, normally this would be dependency injected of course...
    SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(this.getDataSource());

    
return simpleJdbcTemplate.queryForObject(sql, mapper, id);
}

而没有这个特性以前的代码则是这个样子的

public  Actor findActor( long  id)  {
    String sql 
= "select id, first_name, last_name from T_ACTOR where id = ?";
    
    RowMapper mapper 
= new RowMapper() {
    
        
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            Actor actor 
= new Actor();
            actor.setId(rs.getLong(Long.valueOf(rs.getLong(
"id"))));
            actor.setFirstName(rs.getString(
"first_name"));
            actor.setLastName(rs.getString(
"last_name"));
            
return actor;
        }

    }
;
    
    
// normally this would be dependency injected of course...
    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.getDataSource());
    
    
// notice the cast, and the wrapping up of the 'id' argument
    
// in an array, and the boxing of the 'id' argument as a reference type
    return (Actor) jdbcTemplate.queryForObject(sql, mapper, new Object[] {Long.valueOf(id)});
}

使用了 SimpleJdbcTemplate,这样代码显得更加清晰。
Spring2.0真的很好
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值