日期:2014-05-16  浏览次数:20378 次

Spring JDBC: Introduction to JdbcTemplate (III)--JdbcTemplate Derivatives

1. JdbcDaoSupport

public abstract class JdbcDaoSupport extends DaoSupport {
	private JdbcTemplate jdbcTemplate;
	public final void setDataSource(DataSource dataSource) {
		if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
			this.jdbcTemplate = createJdbcTemplate(dataSource);
			initTemplateConfig();
		}
	}
	public final JdbcTemplate getJdbcTemplate() {
	  return this.jdbcTemplate;
	}
}

As we can see from the source code that DaoSupport simply added a JdbcTemplate property.

And when we extends JdbcDaoSupport, we don't need to write the redundancy code of setDataSource().

When we need get jdbcTemplate, we simply call getJdbcTemplate() method.

package edu.xmu.jdbc.dao;

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

import edu.xmu.jdbc.bean.Student;

public class JdbcSupportDao extends JdbcDaoSupport {

    public void createStudent(Student student) {
	String sql = "insert into student(name, age) value(?, ?)";
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	jdbcTemplate.update(sql, student.getName(), student.getAge());
    }
}

?

2. NamedParameterDaoSupport

public class NamedParameterJdbcDaoSupport extends JdbcDaoSupport {

	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
	  return namedParameterJdbcTemplate;
	}
}

As we can see, when our dao need NamedParameterJdbcTemplate instead of JdbcTemplate,

we can extends NamedParameterDaoSupport.

3. NamedParameterJdbcTemplate

When we execute preparedStatements, we need to use ? as place holder.

But NamedParameterJdbcTemplate offers a mechanism that we can use specific name as holder.

package edu.xmu.jdbc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import edu.xmu.jdbc.bean.Student;

public class NamedParameterJdbcTemplateDao extends NamedParameterJdbcDaoSupport {

    /**
     * In this method, we use MapSqlParameterSource for placeholder value
     * mapping
     * 
     * @param student
     */
    public void createStudent(Student student) {
	String sql = "insert into student(name, age) value(:name, :age)";
	NamedParameterJdbcTemplate jdbcTemplate = getNamedParameterJdbcTemplate();

	MapSqlParameterSource namedParameters = new MapSqlParameterSource(
		"name", student.getName());
	namedParameters.addValue("age", student.getAge());
	jdbcTemplate.update(sql, namedParameters);
    }

    /**
     * In this method, we use BeanPropertySqlParameterSource for placeholder
     * value mapping
     * 
     * @param student
     */
    public void createStudent2(Student student) {
	String sql = "insert into student(name, age) value(:name, :age)";
	NamedParameterJdbcTemplate jdbcTemplate = getNamedParameterJdbcTemplate();

	SqlParameterSource parameterSo