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

Spring JDBC: Introduction to JdbcTemplate (II)--API Usage

1. queryForMap

public Map<String, Object> queryForMap(String sql);
public Map<String, Object> queryForMap(String sql, Object... args);
public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes);
// The methods above are meant to be s single row query.

As we can see from API Doc:

1) Return type is Map<String, Object>. One entry for each column, using the column name as the key.

2) As we cannot have ?duplicate keys, this query is meant to be a single row query.

3) This methods is appropriate when you don't have a domain model,?

? ? Otherwise, consider using one of the queryForObject() methods.

4) If the row count that matches the sql is bigger than one, IncorrectResultSizeDataAccessException will be thrown.

package edu.xmu.jdbc.dao;

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

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import edu.xmu.jdbc.bean.Student;

public class JdbcTemplateDao {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
	this.dataSource = dataSource;
	this.jdbcTemplate = new JdbcTemplate(this.dataSource);

    }

    public void prepareData(List<Student> studentList) {
	String sql = "insert into student(name, age) values (?, ?)";
	for (Student student : studentList) {
	    jdbcTemplate
		    .update(sql,
			    new Object[] { student.getName(), student.getAge() },
			    new int[] { java.sql.Types.VARCHAR,
				    java.sql.Types.INTEGER });
	}
    }

    public Map<String, Object> queryForMap() {
	String sql = "select id, name, age from student";
	return jdbcTemplate.queryForMap(sql);
    }

    public Map<String, Object> queryForMap2(int id) {
	String sql = "select id, name, age from student where id=?";
	return jdbcTemplate.queryForMap(sql, new Object[] { id });
    }

    public Map<String, Object> queryForMap3(int id) {
	String sql = "select id, name, age from student where id<=?";
	return jdbcTemplate.queryForMap(sql, new Object[] { id },
		new int[] { java.sql.Types.INTEGER });
    }

    public void truncateTable() {
	String sql = "truncate table student";
	jdbcTemplate.execute(sql);
    }
}
package edu.xmu.jdbc.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import edu.xmu.jdbc.bean.Student;

public class JdbcTemplateTest {
    private DriverManagerDataSource dataSource;
    private String url = "jdbc:mysql://localhost:3306/jdbctest";
    private String username = "root";
    private String password = "root";

    private JdbcTemplateDao dao;

    @Before
    public void setUp() {
	dataSource = new DriverManagerDataSource(url, username, password);
	dataSource.setDriverClassName("com.mysql.jdbc.Driver");

	dao = new JdbcTemplateDao();
	dao.setDataSource(dataSource);

	List<Student> studentList = new ArrayList<Student>();
	Student student = new Student("Davy", 24);
	studentList.add(student);

	dao.prepareData(studentList);
    }

    @Test
    public void queryForMapTest() {
	Map<String, Object> resultMap = dao.queryForMap();

	Set<Map.Entry<String, Object>> entrySet = resultMap.entrySet();
	for (Entry<String, Object> entryMap : entrySet) {
	    System.out.println(entryMap.getKey() + "=" + entryMap.getValue());
	}
    }