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"?><beansxmlns="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" 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" ref="dataSource"></property></bean></beans>
BookDaoTest.java
package test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.BookDao;import domain.Book;public class BookDaoTest {public static void main(String[]args){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_jdbc.xml");BookDao dao=(BookDao)ac.getBean("bookDao");dao.save(new Book(10,"三个火枪手",44.9));List<Book> books=dao.findByPrice(30, 60);for(Book book:books){System.out.println(book);}}}