轉載自:http://1194867672-qq-com.iteye.com/blog/1455814
使用的Spring是3.1版本,不是3.0版本。兩者還是有區別的,其中一點就是:SimpleJdbcTemplate在3.1版本被標記為過時了,而SimpleJdbcTemplate的一些方法,被JdbcTemplate吸收了。所以,個人推薦使用3.1版本.
需要的JAR檔案:
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.test-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.junit-4.7.0.jar
ojdbc14.jar
資料庫指令碼: Sql代碼 --建立商品表 create table product( id varchar2(255) primary key, name varchar2(255), author varchar2(255), price number(6,2), quantity number, description varchar2(255) );
實體類: Java代碼 package org.chendl.springjdbc.domain; public class Product /*implements Serializable*/{ //暫不序列化 private String id; private String name; private String author; private double price; private int quantity; private String description; public Product() { } // getter and setter @Override public String toString() { return "Product [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", quantity=" + quantity + ", description=" + description + "]\n"; } }
正題:
BaseDao介面不提供了,附件中會有,直接給出BaseDaoImpl
Java代碼 package org.chendl.springjdbc.dao.impl; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.sql.Types; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.chendl.springjdbc.dao.BaseDao; import org.chendl.springjdbc.util.QueryResult; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; /** * 使用情境 * * 資料庫是Oracle * 主鍵列名為id * 主鍵由程式提供(這裡用的是UUID) * 實體類名和資料庫表名一致 比如:類名Product 表名product、PRODUCT、Produc 都可以 t_product 不可以 */ public