借助JdbcTemplate類的
execute()
方法,我們可以使用Spring JdbcTemplate執(zhí)行參數(shù)化查詢。要使用參數(shù)化查詢,我們在execute方法中傳遞
PreparedStatementCallback
的實(shí)例。
public T execute(String sql,PreparedStatementCallback<T>);
它處理輸入?yún)?shù)和輸出結(jié)果。在這種情況下,您不必關(guān)心單引號和雙引號。
PreparedStatementCallback接口的方法它只有一個方法doInPreparedStatement。該方法的語法如下:
public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException
我們假設(shè)您已在Oracle10g數(shù)據(jù)庫中創(chuàng)建了下表。
create table employee( id number(10), name varchar2(100), salary number(10) );
Employee.java
此類包含3個帶有構(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
它包含一個屬性jdbcTemplate和一個方法saveEmployeeByPreparedStatement。您必須了解匿名類的概念才能了解該方法的代碼。
package com.nhooo; import java.sql.PreparedStatement; import java.sql.SQLException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; public class EmployeeDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public boolean saveEmployeeByPreparedStatement(final Employee e){ String query="insert into employee values(?,?,?)"; return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){ @Override public boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setInt(1,e.getId()); ps.setString(2,e.getName()); ps.setfloat(3,e.getSalary()); return ps.execute(); } }); } }
applicationContext.xml
DriverManagerDataSource 用于包含有關(guān)數(shù)據(jù)庫的信息,例如驅(qū)動程序類名稱,連接URL,用戶名和密碼。
DriverManagerDataSource類型的JdbcTemplate類中有一個名為
datasource
的屬性。因此,我們需要在JdbcTemplate類中為數(shù)據(jù)源屬性提供DriverManagerDataSource對象的引用。
在這里,我們在EmployeeDao類中使用了JdbcTemplate對象,因此我們通過setter方法傳遞了它,但是您也可以使用構(gòu)造函數(shù)。
<?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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="edao" class="com.nhooo.EmployeeDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
Test.java
此類從applicationContext.xml文件獲取Bean,并調(diào)用saveEmployeeByPreparedStatement()方法。
package com.nhooo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeDao dao=(EmployeeDao)ctx.getBean("edao"); dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000)); } }