最近新接触一个小的项目中用到了Spring JdbcTemplate框架,用起来感觉和DBUTILS差不多,仿制可能以后需要用到,因此记录一下。
概念
JdbcTemplate 类执行 SQL 查询、更新语句和存储过程调用,执行迭代结果集和提取返回参数值。它也捕获 JDBC 异常并转换它们到 org.springframework.dao 包中定义的通用类、更多的信息、异常层次结构。
JdbcTemplate 类的实例是线程安全配置的。所以你可以配置 JdbcTemplate 的单个实例,然后将这个共享的引用安全地注入到多个 DAOs 中。
使用 JdbcTemplate 类时常见的做法是在你的 Spring 配置文件中配置数据源,然后共享数据源 bean 依赖注入到 DAO 类中,并在数据源的设值函数中创建了 JdbcTemplate。
执行SQL 语句
在表中插入一行:
String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update( SQL, name, age);
//jdbcTemplateObject.update( SQL, new Object[]{"Zara", 11} );
从表中删除一行:
String SQL = "delete Student where id = ?";
jdbcTemplateObject.update( SQL, new Object[]{20} );
更新表中的一行:
String SQL = "update Student set name = ? where id = ?";
jdbcTemplateObject.update( SQL, new Object[]{"Zara", 10} );
查询一个整数类型:
String SQL = "select count(*) from Student";
int rowCount = jdbcTemplateObject.queryForInt( SQL );
查询一个 long 类型:
String SQL = "select count(*) from Student";
long rowCount = jdbcTemplateObject.queryForLong( SQL );
一个使用绑定变量的简单查询:
String SQL = "select age from Student where id = ?";
int age = jdbcTemplateObject.queryForInt(SQL, new Object[]{10});
查询字符串:
String SQL = "select name from Student where id = ?";
String name = jdbcTemplateObject.queryForObject(SQL, new Object[]{10}, String.class);
查询并返回一个对象:
String SQL = "select * from Student where id = ?";
Student student = jdbcTemplateObject.queryForObject(SQL,
new Object[]{10}, new StudentMapper());
public class StudentMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setID(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}
查询并返回多个对象:
String SQL = "select * from Student";
List<Student> students = jdbcTemplateObject.query(SQL,
new StudentMapper());
public class StudentMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setID(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}
插入数据,获取自增组件的ID:
public int addStudent(final Student student){
final String sql="insert into t_student(name,sex) values(?,?)";
//创建一个主键持有者
KeyHolder keyHolder=new GeneratedKeyHolder();
jdbcTemplateObject.update(new PreparedStatementCreator(){
public PreparedStatement createPreparedStatement(Conection con) throws SQLException{
PreparedStatement preState=con.prepareStatement(sql);
preState.setString(1,student.getName());
preState.setString(2,student.getSex());
return preState;
}
},keyHolder);
//从主键持有者中获得主键值
return keyHolder.getKey().intValue();
}
批量插入数据:
public void testBatchUpdate() {
String sql = "INSERT INTO employees(last_name, email, dept_id) VALUES(?,?,?)";
List<Object[]> batchArgs = new ArrayList<>();
batchArgs.add(new Object[]{"AA", "aa@atguigu.com", 1});
batchArgs.add(new Object[]{"BB", "bb@atguigu.com", 2});
batchArgs.add(new Object[]{"CC", "cc@atguigu.com", 3});
batchArgs.add(new Object[]{"DD", "dd@atguigu.com", 3});
batchArgs.add(new Object[]{"EE", "ee@atguigu.com", 2});
jdbcTemplate.batchUpdate(sql, batchArgs);
}