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

Spring之SimpleJdbcTemplate的使用

1.数据库脚本

create table user
(
	id		int	not null,
	name	varchar(20),
	primary key(id)
);

?

2.实体类

package org.monday.springjdbc;

public class User {

	private int id;
	private String name;

	public User() {
	}

	public User(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}

}

?

3.Dao接口

package org.monday.springjdbc;

import java.util.List;

public interface UserDao {

	public void insert(User user);

	public void update(User user);

	public void delete(User user);

	public void delete(int id);

	public User findById(int id);

	public int findByName(String name);

	public List<User> findAll();

	public int count();

	public void batchInsert(List<User> users);
}

?

4.Dao的实现类

package org.monday.springjdbc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class UserDaoImpl extends SimpleJdbcDaoSupport implements UserDao {

	@Override
	public void insert(User user) {
		String sql = "insert into user(id,name)values(?,?)";
		getSimpleJdbcTemplate().update(sql, user.getId(), user.getName());

	}

	@Override
	public void update(User user) {
		String sql = "update user set name=? where id=?";
		getSimpleJdbcTemplate().update(sql, user.getName(), user.getId());
	}

	@Override
	public void delete(User user) {
		String sql = "delete from user where id=?";
		getSimpleJdbcTemplate().update(sql, user.getId());
	}

	@Override
	public void delete(int id) {
		String sql = "delete from user where id=?";
		getSimpleJdbcTemplate().update(sql, id);

	}

	@Override
	public User findById(int id) {
		String sql = "select * from user where id=?";
		return getSimpleJdbcTemplate().queryForObject(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(User.class), id);

		// 下面的方法只能返回一列,所以行不通
		// return getSimpleJdbcTemplate().queryForObject(sql, User.class, id);

	}

	@Override
	public int findByName(String name){
		String sql = "select id from user where name=?";
		return getSimpleJdbcTemplate().queryForObject(sql, Integer.class, name);
		// Integer.class 改成int.class 不行
		// 貌似只能用包装类
		
	}

	@Override
	public List<User> findAll() {
		String sql = "select * from user";
		return getSimpleJdbcTemplate().query(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(User.class));
	}

	@Override
	public int count() {
		String sql = "select count(*) from user";
		return getSimpleJdbcTemplate().queryForInt(sql);
	}

	@Override
	public void batchInsert(List<User> users) {
		String sql = "insert into user(id,name)values(?,?)";
		List<Object[]> parameters = new ArrayList<Object[]>();
		for (User u : users) {
			parameters.add(new Object[] { u.getId(), u.getName() });
		}
		getSimpleJdbcTemplate().batchUpdate(sql, parameters);

	}

}

?

5.beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- 引入外部文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>