Spring + JdbcTemplate + JdbcDaoSupport examples
来源:互联网 发布:python处理excel数据 编辑:程序博客网 时间:2024/06/05 06:40
如何传递参数;返回结果如何转换为model;
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.
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
With JdbcTemplate
, you save a lot of typing on the redundant codes, becuase JdbcTemplate
will handle it automatically.
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() }); }
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.
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/mkyongjava" /> <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.mkyong.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean></beans>
Note
In Spring JDBC development, it’s always recommended to useJdbcTemplate
andJdbcDaoSupport
, instead of coding JDBC code yourself.
- Spring + JdbcTemplate + JdbcDaoSupport examples
- Spring + JdbcTemplate + JdbcDaoSupport examples
- Spring + JdbcTemplate + JdbcDaoSupport examples
- Spring + JdbcTemplate + JdbcDaoSupport + HibernateDaoSupport examples
- Spring + JdbcTemplate + JdbcDaoSupport
- Spring + JdbcTemplate + JdbcDaoSupport
- Spring - JdbcTemplate 和 JdbcDaoSupport
- Spring + JdbcTemplate + JdbcDaoSupport。JdbcTemplate 和 JdbcDaoSupport的不同
- Spring JdbcTemplate Querying Examples
- Spring JdbcTemplate Querying examples
- Spring JdbcTemplate Querying examples
- Spring 和JdbcTemplate 和 JdbcDaoSupport方式对比
- Spring mvc中jdbcDaoSupport和jdbcTemplate的使用
- 18、(知识篇)Spring使用JDBCTemplate/JDBCDaoSupport/具名参数
- Spring框架学习(13):JdbcTemplate和JdbcDaoSupport
- Spring+JdbcDaoSupport
- Spring JdbcDaoSupport
- jdbcTemplate模板和JdbcDaoSupport工具
- 黑马程序员--网络编程协议
- Spring + JDBC example
- ANDROID之路-Git_入门笔记
- iOS开发 两个视图之间值传递的常用方法<二>
- mybatis实战教程(mybatis in action),mybatis入门到精通
- Spring + JdbcTemplate + JdbcDaoSupport examples
- Chapter 9 Sequential Containers
- Spring JdbcTemplate Querying examples
- U盘做启动盘后恢复原始容量
- Spring JdbcTemplate batchUpdate() example
- Spring SimpleJdbcTemplate Querying examples
- Spring SimpleJdbcTemplate batchUpdate() example
- Spring Named Parameters examples in SimpleJdbcTemplate
- 1021. Deepest Root (25) DFS & (a little idea )