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

spring学习实例之spring对jdbc的支持
1、包结构:


2、源代码:
Book.java
package domain;

public class Book {
	private long id;
	private String name;
	private double price;
	
	public Book(){
		
	}
	
	public Book(String name,double price){
		this.name=name;
		this.price=price;
	}
	
	public Book(long id,String name,double price){
		this(name,price);
		this.id=id;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public String toString(){
		return "id:"+id+"-name:"+name+"-price:"+price;
	}
	
}

BookDao.java
package dao;

import java.util.List;
import domain.Book;

public interface BookDao {
	void save(Book b);
	void update(Book b);
	void delete(long id);
	Book findById(long id);
	List<Book> findByPrice(double from,double to);
}

BookDaoImpl.java
package dao.jdbc;

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

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import dao.BookDao;
import domain.Book;

public class BookDaoImpl extends JdbcDaoSupport implements BookDao {

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

	public Book findById(long id) {
		String sql = "select * from book where id =?";
		ResultSetExtractor rse = null;
		rse = new ResultSetExtractor() {
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if (rs.next()) {
					return new Book(rs.getLong(1), rs.getString(2), rs
							.getDouble(3));
				} else {
					return null;
				}
			}
		};
		return (Book) this.getJdbcTemplate().query(sql, new Object[] { id },
				rse);
	}

	public List findByPrice(double from, double to) {
		String sql="select * from book where price between ? and ?";
		return this.getJdbcTemplate().query(sql, new Object[]{from,to},new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				return new Book(rs.getLong(1),rs.getString(2),rs.getDouble(3));
			}
		});
	}

	public void save(Book b) {
		String sql = "insert into book(id,name,price) values(?,?,?) ";
		this.getJdbcTemplate().update(sql,
				new Object[] { b.getId(), b.getName(), b.getPrice() });
	}

	public void update(Book b) {
		String sql="update book set name = ? ,  price = ?  where id = ?";
		this.getJdbcTemplate().update(sql,new Object[]{b.getName(),b.getPrice(),b.getId()});
	}
}

applicationContext_jdbc.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:myorcl"></property>
		<property name="username" value="myorcl"></property>
		<property name="password" value="embed"></property>
	</bean>
	
	<bean id="bookDao" class="dao.jdbc.BookDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>