Spring提供了另一種通過命名參數(shù)插入數(shù)據(jù)的方法。這樣,我們使用名稱代替?(問號)。因此,最好記住該列的數(shù)據(jù)。
insert into employee values (:id,:name,:salary)
在此示例中,我們將僅調(diào)用NamedParameterJdbcTemplate類的execute方法。該方法的語法如下:
pubic T execute(String sql,Map map,PreparedStatementCallback psc)
我們假設(shè)您已經(jīng)在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和一個方法保存。
package com.nhooo;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import java.util.*;
public class EmpDao {
NamedParameterJdbcTemplate template;
public EmpDao(NamedParameterJdbcTemplate template) {
this.template = template;
}
public void save (Emp e){
String query="insert into employee values (:id,:name,:salary)";
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",e.getId());
map.put("name",e.getName());
map.put("salary",e.getSalary());
template.execute(query,map,new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return ps.executeUpdate();
}
});
}
}
applicationContext.xml
DriverManagerDataSource 用于包含有關(guān)數(shù)據(jù)庫的信息,例如驅(qū)動程序類名稱,連接URL,用戶名和密碼。
DriverManagerDataSource類型的NamedParameterJdbcTemplate類中有一個名為
datasource 的屬性。因此,我們需要在NamedParameterJdbcTemplate類中為數(shù)據(jù)源屬性提供DriverManagerDataSource對象的引用。
這里,我們在EmployeeDao類中使用NamedParameterJdbcTemplate對象,因此我們通過構(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.namedparam.NamedParameterJdbcTemplate">
<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)用save方法。
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");
dao.save(new Emp(23,"sonoo",50000));
}
}