开发者社区 问答 正文

SpringJDBC可以单独使用吗?

我现在在做一个应用,需要用到数据库,我之前使用过spring和springJDBC,然后感觉springJDBC实在是太方便了 - - 但是现在感觉无法熟练驾驭Spring,所以不打算使用它,想单独使用SpringJDBC。

请问这样可以吗?当然还会使用一个数据库连接池,c3p0什么的。

hibernate太重量级了,暂时没考虑;

mybatis没用过 - -

展开
收起
a123456678 2016-03-17 09:43:39 2999 分享 版权
1 条回答
写回答
取消 提交回答
  • Here is a simple query for getting the number of rows in a relation:
    
    int rowCount = this.jdbcTemplate.queryForInt("select count(*) from t_actor");
    A simple query using a bind variable:
    
    int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt( "select count(*) from t_actor where first_name = ?", "Joe");
    Querying for a String:
    
    String lastName = this.jdbcTemplate.queryForObject( "select last_name from t_actor where id = ?", new Object[]{1212L}, String.class);
    Querying and populating a single domain object:
    
    Actor actor = this.jdbcTemplate.queryForObject( "select first_name, last_name from t_actor where id = ?", new Object[]{1212L}, new RowMapper<Actor>() { public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Actor actor = new Actor();
                    actor.setFirstName(rs.getString("first_name"));
                    actor.setLastName(rs.getString("last_name")); return actor;
                }
            });
    Querying and populating a number of domain objects:
    
    List<Actor> actors = this.jdbcTemplate.query( "select first_name, last_name from t_actor", new RowMapper<Actor>() { public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Actor actor = new Actor();
                    actor.setFirstName(rs.getString("first_name"));
                    actor.setLastName(rs.getString("last_name")); return actor;
                }
            });
    2019-07-17 19:04:31
    赞同 展开评论
问答标签:
问答地址: