Spring 专题
专题目录
您的位置:java > Spring专题 > Spring JdbcTemplate+JdbcDaoSupport实例
Spring JdbcTemplate+JdbcDaoSupport实例
作者:--    发布时间:2019-11-20
在spring jdbc开发中,可以使用 jdbctemplate 和 jdbcdaosupport 类来简化整个数据库的操作过程。
在本教程中,我们将重复上一篇文章 spring+jdbc例子,看之前(无jdbctemplate支持)和之后(含jdbctemplate的支持)之间不同的例子。

1. 不使用jdbctemplate示例

如果不用jdbctemplate,必须创建大量的冗余代码(创建连接,关闭连接,处理异常)中的所有dao数据库的操作方法 - 插入,更新和删除。它的效率并不是很高,容易出错和乏味。
private datasource datasource;
		
	public void setdatasource(datasource datasource) {
		this.datasource = datasource;
	}
		
	public void insert(customer customer){
			
		string sql = "insert into customer " +
				"(cust_id, name, age) values (?, ?, ?)";
		connection conn = null;
			
		try {
			conn = datasource.getconnection();
			preparedstatement ps = conn.preparestatement(sql);
			ps.setint(1, customer.getcustid());
			ps.setstring(2, customer.getname());
			ps.setint(3, customer.getage());
			ps.executeupdate();
			ps.close();
				
		} catch (sqlexception e) {
			throw new runtimeexception(e);
				
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (sqlexception e) {}
			}
		}
	}

2. 使用jdbctemplate示例

使用jdbctemplate可节省大量的冗余代码,因为jdbctemplate类会自动处理它。
private datasource datasource;
	private jdbctemplate jdbctemplate;
		
	public void setdatasource(datasource datasource) {
		this.datasource = datasource;
	}

	public void insert(customer customer){
			
		string sql = "insert into customer " +
			"(cust_id, name, age) values (?, ?, ?)";
				 
		jdbctemplate = new jdbctemplate(datasource);
				
		jdbctemplate.update(sql, new object[] { customer.getcustid(),
			customer.getname(),customer.getage()  
		});
				
	}
看看有什么不同?

3. 使用jdbcdaosupport示例

通过扩展 jdbcdaosupport,设置数据源,并且 jdbctemplate 在你的类中不再是必需的,只需要正确的数据源注入jdbccustomerdao。可以使用 getjdbctemplate()方法得到 jdbctemplate。

public class jdbccustomerdao extends jdbcdaosupport implements customerdao
	{
	   //no need to set datasource here
	   public void insert(customer customer){
			
		string sql = "insert into customer " +
			"(cust_id, name, age) values (?, ?, ?)";
				 
		getjdbctemplate().update(sql, new object[] { customer.getcustid(),
				customer.getname(),customer.getage()  
		});
				
	}
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<bean id="datasource" 
         class="org.springframework.jdbc.datasource.drivermanagerdatasource">

		<property name="driverclassname" value="com.mysql.jdbc.driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/h3java" />
		<property name="username" value="root" />
		<property name="password" value="password" />
	</bean>
	
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerdao" class="com.h3.customer.dao.impl.jdbccustomerdao">
		<property name="datasource" ref="datasource" />
	</bean>
	
</beans>
注: 在spring jdbc开发,它总是建议使用,总是建议使用 jdbctemplate和jdbcdaosupport,而不使用自己的jdbc编程代码。

下载代码  – http://pan.baidu.com/s/1bnigdir

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册