zoukankan      html  css  js  c++  java
  • Spring JdbcTemplate+JdbcDaoSupport实例

    在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/yiibaijava" />
    		<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.yiibai.customer.dao.impl.JdbcCustomerDAO">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	
    </beans>
    注: 在Spring JDBC开发,它总是建议使用,总是建议使用 JdbcTemplate和JdbcDaoSupport,而不使用自己的JDBC编程代码。
     
  • 相关阅读:
    8.10
    今日头条笔试题 1~n的每个数,按字典序排完序后,第m个数是什么?
    Gym 100500B Conference Room(最小表示法,哈希)
    CodeForces 438D The Child and Sequence(线段树)
    UVALIVE 6905 Two Yachts(最小费用最大流)
    Gym Conference Room (最小表示法,哈希)
    hdu 2389 Rain on your Parade(二分图HK算法)
    Codeforces Fox And Dinner(最大流)
    zoj 3367 Counterfeit Money(dp)
    ZOJ3370. Radio Waves(2-sat)
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367550.html
Copyright © 2011-2022 走看看