Spring教程-Spring SimpleJdbcTemplate 示例
Spring SimpleJdbcTemplate 示例
Spring 3 JDBC 通过 SimpleJdbcTemplate 类支持 Java 5 的 var-args(可变参数)和自动装箱功能。
SimpleJdbcTemplate 类封装了 JdbcTemplate 类,并提供了 update 方法,我们可以在其中传递任意数量的参数。
SimpleJdbcTemplate 类的 update 方法语法
int update(String sql,Object... parameters)我们应该按照参数化查询中定义的顺序在 update 方法中传递参数值。
SimpleJdbcTemplate 类的示例 我们假设您已在 Oracle10g 数据库中创建了以下表。
create table employee(
id number(10),
name varchar2(100),
salary number(10)
); Employee.java 此类包含3个属性,带有构造函数和 setter 和 getter。
package cn.javatiku;
public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
} EmployeeDao.java 它包含一个 SimpleJdbcTemplate 属性和一个 update 方法。在这种情况下,update 方法将仅更新相应 id 的名称。如果您想同时更新名称和工资,请注释 update 方法的上述两行代码,并取消注释下面给出的2行代码。
package cn.javatiku;
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 用于包含有关数据库的信息,例如驱动程序类名、连接 URL、用户名和密码。
SimpleJdbcTemplate 类的 DriverManagerDataSource 类型有一个名为 datasource 的属性。因此,我们需要在 SimpleJdbcTemplate 类中为 datasource 属性提供 DriverManagerDataSource 对象的引用。
在这里,我们在 EmployeeDao 类中使用 SimpleJdbcTemplate 对象,所以我们通过构造函数传递它,但您也可以使用 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="cn.javatiku.EmpDao">
<constructor-arg>
<ref bean="jtemplate"/>
</constructor-arg>
</bean>
</beans> SimpleTest.java 此类从 applicationContext.xml 文件获取 bean,并调用 EmpDao 类的 update 方法。
package cn.javatiku;
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);
}
}