Spring + JdbcTemplate + JdbcDaoSupport examples

在Spring JDBC开发 可以使用 的JdbcTemplate JdbcDaoSupport 类来 简化 整个数据库的 操作 流程。
在本教程中 我们 将重用 去年春天 + JDBC 例如,要查看 之前 (无 JdbcTemplate的 支持)和 之间的 不同 (与 JdbcTemplate的 支持 )的例子。 JdbcTemplate的必须创建许多冗余代码(创建连接,关闭连接处理异常)在所有DAO数据库的操作方法 - 插入,更新和删除。它只是没有效率的,丑陋的容易出错繁琐。
1. Example Without JdbcTemplate

    没有

Java代码 复制代码 收藏代码
  1. private DataSource dataSource; 
  2.  
  3. public void setDataSource(DataSource dataSource) { 
  4.     this.dataSource = dataSource; 
  5.  
  6. public void insert(Customer customer){ 
  7.  
  8.     String sql = "INSERT INTO CUSTOMER "
  9.             "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"
  10.     Connection conn = null
  11.  
  12.     try
  13.         conn = dataSource.getConnection(); 
  14.         PreparedStatement ps = conn.prepareStatement(sql); 
  15.         ps.setInt(1, customer.getCustId()); 
  16.         ps.setString(2, customer.getName()); 
  17.         ps.setInt(3, customer.getAge()); 
  18.         ps.executeUpdate(); 
  19.         ps.close(); 
  20.  
  21.     } catch (SQLException e) { 
  22.         throw new RuntimeException(e); 
  23.  
  24.     } finally
  25.         if (conn != null) { 
  26.             try
  27.                 conn.close(); 
  28.             } catch (SQLException e) {} 
  29.         } 
  30.     } 
	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、Example With JdbcTemplate

    有了JdbcTemplate的节省大量的冗余代码输入很多因为的JdbcTemplate自动处理

Java代码 复制代码 收藏代码
  1. private DataSource dataSource; 
  2. private JdbcTemplate jdbcTemplate; 
  3.  
  4. public void setDataSource(DataSource dataSource) { 
  5.     this.dataSource = dataSource; 
  6.  
  7. public void insert(Customer customer){ 
  8.  
  9.     String sql = "INSERT INTO CUSTOMER "
  10.         "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"
  11.  
  12.     jdbcTemplate = new JdbcTemplate(dataSource); 
  13.  
  14.     jdbcTemplate.update(sql, new Object[] { customer.getCustId(), 
  15.         customer.getName(),customer.getAge()   
  16.     }); 
  17.  
	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、Example With JdbcDaoSupport

    通过继承JdbcDaoSupport,设置数据源你的类的JdbcTemplate不再需要你只需要正确的数据源注入JdbcCustomerDAO而你可以通过使用getJdbcTemplate)方法的JdbcTemplate

Java代码 复制代码 收藏代码
  1. public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO 
  2.    //no need to set datasource here 
  3.    public void insert(Customer customer){ 
  4.  
  5.     String sql = "INSERT INTO CUSTOMER "
  6.         "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"
  7.  
  8.     getJdbcTemplate().update(sql, new Object[] { customer.getCustId(), 
  9.             customer.getName(),customer.getAge()   
  10.     }); 
  11.  
	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()  
		});
 
	}

Xml代码 复制代码 收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans" 
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  4.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  5.   
  6.     <bean id="dataSource"  
  7.          class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
  8.   
  9.         <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
  10.         <property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" /> 
  11.         <property name="username" value="root" /> 
  12.         <property name="password" value="password" /> 
  13.     </bean> 
  14.   
  15. </beans> 

Xml代码 复制代码 收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans" 
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  4.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  5.   
  6.     <bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO"> 
  7.         <property name="dataSource" ref="dataSource" /> 
  8.     </bean> 
  9.   
  10. </beans> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值