个性化阅读
专注于IT技术分析

Spring SimpleJdbcTemplate示例详解

点击下载
  1. SpringSimpleJdbcTemplate
  2. SimpleJdbcTemplate的示例

Spring 3 JDBC在SimpleJdbcTemplate类的帮助下支持Java 5功能var-args(可变参数)和自动装箱。

SimpleJdbcTemplate类包装JdbcTemplate类, 并提供了update方法, 我们可以在其中传递任意数量的参数。

SimpleJdbcTemplate类的更新方法的语法

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 com.srcmini;

public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
}

EmployeeDao.java

它包含一个属性SimpleJdbcTemplate和一个方法更新。在这种情况下, update方法将仅更新对应ID的名称。如果要同时更新姓名和薪水, 请注释一下update方法的以上两行代码, 并取消注释下面给出的两行代码。

package com.srcmini;

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, 用户名和密码。

DriverManagerDataSource类型的SimpleJdbcTemplate类中有一个名为datasource的属性。因此, 我们需要在SimpleJdbcTemplate类中为数据源属性提供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="com.srcmini.EmpDao">
<constructor-arg>
<ref bean="jtemplate"/>
</constructor-arg>
</bean>

</beans>

SimpleTest.java

此类从applicationContext.xml文件获取Bean, 并调用EmpDao类的update方法。

package com.srcmini;

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);
}
}

下载此示例(使用MyEclipse IDE开发)

下载此示例(使用Eclipse IDE开发)

赞(0)
未经允许不得转载:srcmini » Spring SimpleJdbcTemplate示例详解

评论 抢沙发

评论前必须登录!