使用Spring + JdbcTemplate + JdbcDaoSupport的例子

摘要:使用Spring + JdbcTemplate + JdbcDaoSupport的例子

In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes.

In this tutorial, we will reuse the last Spring + JDBC example, to see the different between a before (No JdbcTemplate support) and after (With JdbcTemplate support) example.

 

1. Example Without JdbcTemplate

Witout JdbcTemplate, you have to create many redundant codes (create connection , close connection , handle exception) in all the DAO database operation methods – insert, update and delete. It just not efficient, ugly, error prone and tedious.

01 private DataSource dataSource;
02  
03     public void setDataSource(DataSource dataSource) {
04         this.dataSource = dataSource;
05     }
06  
07     public void insert(Customer customer){
08  
09         String sql = "INSERT INTO CUSTOMER " +
10                 "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
11         Connection conn = null;
12  
13         try {
14             conn = dataSource.getConnection();
15             PreparedStatement ps = conn.prepareStatement(sql);
16             ps.setInt(1, customer.getCustId());
17             ps.setString(2, customer.getName());
18             ps.setInt(3, customer.getAge());
19             ps.executeUpdate();
20             ps.close();
21  
22         catch (SQLException e) {
23             throw new RuntimeException(e);
24  
25         finally {
26             if (conn != null) {
27                 try {
28                     conn.close();
29                 catch (SQLException e) {}
30             }
31         }
32     }

2. Example With JdbcTemplate

With JdbcTemplate, you save a lot of typing on the redundant codes, becuase JdbcTemplate will handle it automatically.

 

01 private DataSource dataSource;
02     private JdbcTemplate jdbcTemplate;
03  
04     public void setDataSource(DataSource dataSource) {
05         this.dataSource = dataSource;
06     }
07  
08     public void insert(Customer customer){
09  
10         String sql = "INSERT INTO CUSTOMER " +
11             "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
12  
13         jdbcTemplate = new JdbcTemplate(dataSource);
14  
15         jdbcTemplate.update(sql, new Object[] { customer.getCustId(),
16             customer.getName(),customer.getAge() 
17         });
18  
19     }

See the different?

 

 

3. Example With JdbcDaoSupport

By extended the JdbcDaoSupport, set the datasource and JdbcTemplate in your class is no longer required, you just need to inject the correct datasource into JdbcCustomerDAO. And you can get the JdbcTemplate by using a getJdbcTemplate() method.

01 public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO
02     {
03        //no need to set datasource here
04        public void insert(Customer customer){
05  
06         String sql = "INSERT INTO CUSTOMER " +
07             "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
08  
09         getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
10                 customer.getName(),customer.getAge() 
11         });
12  
13     }

 

 

01 <beans xmlns="http://www.springframework.org/schema/beans"
02     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03     xsi:schemaLocation="http://www.springframework.org/schema/beans
04     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
05  
06     <bean id="dataSource"
07          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
08  
09         <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>
01 <beans xmlns="http://www.springframework.org/schema/beans"
02     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03     xsi:schemaLocation="http://www.springframework.org/schema/beans
04     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
05  
06     <bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
07         <property name="dataSource" ref="dataSource" />
08     </bean>
09  
10 </beans>
Note In Spring JDBC development, it’s always recommended to use JdbcTemplate and JdbcDaoSupport, instead of coding JDBC code yourself.
參考url:http://my.oschina.net/forrest420/blog/101850