日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring JdbcTemplate+JdbcDaoSupport实例

發布時間:2024/9/20 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 herepublic 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/beanshttp://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/beanshttp://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編程代碼。 下載代碼 ?–?http://pan.baidu.com/s/1bnIGdiR

總結

以上是生活随笔為你收集整理的Spring JdbcTemplate+JdbcDaoSupport实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。