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

Spring中jdbcTemplate使用样例
使用Spring的jdbcTemplate不需要写一下繁琐的try catch语句,同时Connection也由spring来管理,很方便。
一、创建数据库表
create table bookinfo
(
    id varchar(20) primary key,
    name varchar(20),
    author varchar(20)
)

二、创建数据库访问Dao类
package com.junge.spring.dao;

import java.util.List;

import com.junge.spring.bean.Book;

public interface BookDao
{
	List<Book> queryAll();

	List<Book> queryByAuthor(String author);

	void create(Book book);

	void update(Book book);

	void delete(String id);
}

package com.junge.spring.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

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

import com.junge.spring.bean.Book;
import com.junge.spring.dao.BookDao;

public class JdbcTemplateBookDao extends JdbcDaoSupport implements BookDao
{

	class BookRowMapper implements RowMapper<Book>
	{

		@Override
		public Book mapRow(ResultSet rs, int rowNum) throws SQLException
		{
			Book book = new Book();
			book.setId(rs.getString("id"));
			book.setName(rs.getString("name"));
			book.setAuthor("author");
			return book;
		}

	}

	@Override
	public List<Book> queryAll()
	{
		return getJdbcTemplate().query("select * from bookinfo",
				new BookRowMapper());

	}

	@Override
	public List<Book> queryByAuthor(String author)
	{
		return getJdbcTemplate().query("select * from bookinfo where author=?",
				new Object[] { author }, new BookRowMapper());

	}

	@Override
	public void create(Book book)
	{
		this.getJdbcTemplate()
				.update("insert into bookinfo (id,name,author) values(?,?,?)",
						new Object[] { book.getId(), book.getName(),
								book.getAuthor() });

	}

	@Override
	public void update(Book book)
	{
		this.getJdbcTemplate()
				.update("update bookinfo set name=?,author=? where id=?",
						new Object[] { book.getName(), book.getAuthor(),
								book.getId() });

	}

	@Override
	public void delete(String id)
	{
		this.getJdbcTemplate().update("delete from bookinfo where id=?",
				new Object[] { id });

	}

}


三、修改配置文件(jdbc_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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/bookstore" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>


	<bean id="jdbcTemplateBookDao" class="com.junge.spring.dao.impl.JdbcTemplateBookDao">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>



四、测试(JdbcTemplateTest.java)
package com.junge.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.junge.spring.bean.Book;
import com.junge.spring.dao.BookDao;

public class JdbcTemplateTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"jdbc_beans.xml");

		BookDao bookDao = (BookDao) context.getBean("jdbcTemplateBookDao");

		Book book = new Book();

		book.setName("haoshu");
		book.setAuthor("zhangsan");
		book.setId("6101212");

		bookDao.create(book);
	}

}