[刘阳Java]_JdbcTemplate用法_第11讲

JdbcTemplate模板提供操作数据库的方法应用,下面我们来说一下它的用法(注意:建议大家结合Spring API文档学习效果更好,因为下面的代码只是“抱砖引玉”

1. 遵循常见的数据库操作(增,删,改,查),我们可以把JdbcTemplate的功能应用大致分成下几种

  • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句
  • update方法:update方法用于执行新增、修改、删除等语句
  • batchUpdate方法:用于执行批处理相关语句
  • query方法、queryForXXX方法:用于执行查询相关语句
  • call方法:用于执行存储过程、函数相关语句

2. queryForXXX方法

introwCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual"); //返回一个int值

intcountOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
        "select count(0) from t_actors where first_name = ?", new Object[]{"Joe"}); //通过参数的绑定来返回int值

String surname = (String) this.jdbcTemplate.queryForObject(
        "select surname from t_actor where id = ?", 
        new Object[]{new Long(1212)}, String.class); //返回一个String类型

Actor actor = (Actor) this.jdbcTemplate.queryForObject(
    "selectfirst_name, surname from t_actor where id = ?",
    new Object[]{new Long(1212)},
    new RowMapper() {
        public Object mapRow(ResultSetrs, introwNum) throws SQLException {
            Actor actor = new Actor();
       actor.setFirstName(rs.getString("first_name"));
       actor.setSurname(rs.getString("surname"));
            return actor;
        }
    }); //返回一个domain对象(JavaBean对象)

2. update方法

this.jdbcTemplate.update(
        "insert into t_actor (first_name, surname) values (?, ?)", 
        new Object[] {"Leonor", "Watling"}); //插入操作

this.jdbcTemplate.update(
        "updatet_actor set weapon = ? where id = ?", 
        new Object[] {"Banjo", new Long(5276)}); //更新操作

this.jdbcTemplate.update(
        "delete from actor where id = ?",
        new Object[] {new Long.valueOf(actorId)}); //删除操作

3. execute方法

this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))"); //执行一个DDL语句

4. 批量SQL语句操作

@Repository
public class JdbcActorDao {

    @Autowried
    private JdbcTemplatejdbcTemplate;

    public int[] batchUpdate(final List actors) {
    int[] updateCounts = jdbcTemplate.batchUpdate(
                "updatet_actor set first_name = ?, last_name = ? where id = ?",
                new BatchPreparedStatementSetter() {
                    public void setValues(PreparedStatementps, int i) throws SQLException {
                  ps.setString(1, ((Actor)actors.get(i)).getFirstName());
                  ps.setString(2, ((Actor)actors.get(i)).getLastName());
                  ps.setLong(3, ((Actor)actors.get(i)).getId().longValue());
                    }

                    public intgetBatchSize() {
                        return actors.size();
                    }
                } );
        return updateCounts;
    }

}

 5. JdbcDaoSupport支持类应用,它需要让Dao层中的类去继承。然后就可以在Dao层中不用去定义JdbcTemplate,但是在Dao层中的类还是需要依赖注入DataSource

package com.spring.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class SpringJdbcDao extends JdbcDaoSupport {
    
    public void getTbUsers() {
        String sql = "select * from tb_user";
        List<Map> list = getJdbcTemplate().queryForList(sql);
        for (Map m : list) {
            System.out.println(m.get("USER_ID")+"	"+m.get("USER_NAME"));
        }
    }
}
<bean id="springjdbcdao" class="com.spring.dao.SpringJdbcDao">
    <property name="dataSource" ref="dataSource"></property>
</bean>
原文地址:https://www.cnblogs.com/liuyangjava/p/6690017.html