Spring 3 JDBC在SimpleJdbcTemplate類的幫助下支持Java 5功能var-args(可變參數(shù))和自動(dòng)裝箱。
SimpleJdbcTemplate類包裝了JdbcTemplate類,并提供了可在其中傳遞任意數(shù)量參數(shù)的update方法。
int update(String sql,Object... 參數(shù))
我們應(yīng)該按照在參數(shù)化查詢中定義的順序在更新方法中傳遞參數(shù)值。
我們假設(shè)您已經(jīng)在Oracle10g數(shù)據(jù)庫(kù)中創(chuàng)建了下表。
create table employee( id number(10), name varchar2(100), salary number(10) );
Employee.java
此類包含3個(gè)帶有構(gòu)造函數(shù),setter和getter的屬性。
package com.nhooo; public class Employee { private int id; private String name; private float salary; //no-arg and parameterized constructors //getters and setters }
EmployeeDao.java
它包含一個(gè)屬性SimpleJdbcTemplate和一個(gè)方法更新。在這種情況下,update方法將僅更新對(duì)應(yīng)ID的名稱。如果要同時(shí)更新姓名和薪水,請(qǐng)注釋一下update方法的以上兩行代碼,并取消注釋下面給出的兩行代碼。
package com.nhooo; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; public class EmpDao { SimpleJdbcTemplate template; public EmpDao(SimpleJdbcTemplate template) { this.template = template; } public int update (Emp e){ String query="update employee set name=? where id=?"; return template.update(query,e.getName(),e.getId()); //String query="update employee set name=?,salary=? where id=?"; //return template.update(query,e.getName(),e.getSalary(),e.getId()); } }
applicationContext.xml
DriverManagerDataSource 用于包含有關(guān)數(shù)據(jù)庫(kù)的信息,例如驅(qū)動(dòng)程序類名稱,連接URL,用戶名和密碼。
DriverManagerDataSource類型的SimpleJdbcTemplate類中有一個(gè)名為
datasource
的屬性。因此,我們需要在SimpleJdbcTemplate類中為數(shù)據(jù)源屬性提供DriverManagerDataSource對(duì)象的引用。
在這里,我們?cè)贓mployeeDao類中使用SimpleJdbcTemplate對(duì)象,因此我們通過(guò)構(gòu)造函數(shù)傳遞它,但是您也可以使用setter方法。
<?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-3.0.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="oracle" /> </bean> <bean id="jtemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg ref="ds"></constructor-arg> </bean> <bean id="edao" class="com.nhooo.EmpDao"> <constructor-arg> <ref bean="jtemplate"/> </constructor-arg> </bean> </beans>
SimpleTest.java
此類從applicationContext.xml文件獲取Bean,并調(diào)用EmpDao類的更新方法。
package com.nhooo; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class SimpleTest { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); EmpDao dao=(EmpDao)factory.getBean("edao"); int status=dao.update(new Emp(23,"Tarun",35000)); System.out.println(status); } }