在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程。
在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前(无JdbcTemplate支持)和之后(含JdbcTemplate的支持)之间不同的例子。

1. 不使用JdbcTemplate示例

如果不用JdbcTemplate,必须创建大量的冗余代码(创建连接,关闭连接,处理异常)中的所有DAO数据库的操作方法 - 插入,更新和删除。它的效率并不是很高,容易出错和乏味。
  1. private DataSource dataSource;
  2.  
  3. public void setDataSource(DataSource dataSource) {
  4. this.dataSource = dataSource;
  5. }
  6.  
  7. public void insert(Customer customer){
  8.  
  9. 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. 使用JdbcTemplate示例

使用JdbcTemplate可节省大量的冗余代码,因为JdbcTemplate类会自动处理它。
  1. private DataSource dataSource;
  2. private JdbcTemplate jdbcTemplate;
  3.  
  4. public void setDataSource(DataSource dataSource) {
  5. this.dataSource = dataSource;
  6. }
  7.  
  8. public void insert(Customer customer){
  9.  
  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. }
看看有什么不同?

3. 使用JdbcDaoSupport示例

通过扩展 JdbcDaoSupport,设置数据源,并且 JdbcTemplate 在你的类中不再是必需的,只需要正确的数据源注入JdbcCustomerDAO。可以使用 getJdbcTemplate()方法得到 JdbcTemplate。

  1. public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO
  2. {
  3. //no need to set datasource here
  4. public void insert(Customer customer){
  5.  
  6. String sql = "INSERT INTO CUSTOMER " +
  7. "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
  8.  
  9. getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
  10. customer.getName(),customer.getAge()
  11. });
  12.  
  13. }
  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/yiibaijava" />
  11. <property name="username" value="root" />
  12. <property name="password" value="password" />
  13. </bean>
  14.  
  15. </beans>
  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.yiibai.customer.dao.impl.JdbcCustomerDAO">
  7. <property name="dataSource" ref="dataSource" />
  8. </bean>
  9.  
  10. </beans>
注: 在Spring JDBC开发,它总是建议使用,总是建议使用 JdbcTemplate和JdbcDaoSupport,而不使用自己的JDBC编程代码。
 

Spring JdbcTemplate+JdbcDaoSupport实例的更多相关文章

  1. ref:Spring JdbcTemplate+JdbcDaoSupport实例

    ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...

  2. Spring JdbcTemplate+JdbcDaoSupport实例(和比较)

    首先,数据库是这样的,很简单. 当然,要引入spring的包,这里我全部导入了,省事. applicationContext.xml是这样的: <?xml version="1.0&q ...

  3. Spring + JdbcTemplate + JdbcDaoSupport examples

    In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the over ...

  4. Spring JdbcTemplate batchUpdate() 实例

    在某些情况下,可能需要将一批记录插入到数据库中.如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行. 在上述情况下,你可以使用 JdbcTemplate BATCHUPDAT ...

  5. Spring JdbcTemplate查询实例

    这里有几个例子向您展示如何使用JdbcTemplate的query()方法来查询或从数据库提取数据.整个项目的目录结构如下: 1.查询单行数据 这里有两种方法来查询或从数据库中提取单行记录,并将其转换 ...

  6. 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析

    Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...

  7. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  8. 使用Spring JDBCTemplate简化JDBC的操作

    使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...

  9. JAVA入门[18]-JdbcTemplate简单实例

    一.关于JdbcTemplate JdbcTemplate是最基本的Spring JDBC模板,这个模板支持简单的JDBC数据库访问功能以及基于索引参数的查询. Spring数据访问模板:在数据库操作 ...

随机推荐

  1. acm专题---键树

    题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...

  2. sshd_config OpenSSH SSH 进程配置文件配置说明

    名称 sshd_config – OpenSSH SSH 服务器守护进程配置文件 大纲 /etc/ssh/sshd_config 描述sshd 默认从 /etc/ssh/sshd_config 文件( ...

  3. 转载--void指针(void *的用法)

    转自:jimmy 指针有两个属性:指向变量/对象的地址和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从 ...

  4. 使用CLion

    CLion是JetBrains公司的一款C++的IDE.默认使用Cmake构建. ubuntu和fedora下的安装 在ubuntu下安装了CLion,和QtCreator相比: ibus输入法能输入 ...

  5. python开发学习-day06(模块拾忆、面向对象)

    s12-20160130-day06 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  6. ASP.NET Zero--4.不使用谷歌字体,提升加载速度

    jtable控件样式中会使用到谷歌字体,每次访问都特别慢 1.打开jtable.css文件 [..\MyCompanyName.AbpZeroTemplate.Web\libs\jquery-jtab ...

  7. [实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩

    写在前面 关于该项目,已经很久没更新了.实在是找不到一个好的ui,没办法就在网上找了一个还不错的,就凑合着先用着吧,先出第一版,以后的再想着去优化.最近更新与网盘项目相关的内容是准备在项目中使用一个美 ...

  8. loadrunner生成随机uuid的方法

    在globals.h中定义方法: 方法: 1.将生成GUID方法放在新建的GUID.h文件中: 2.把这个文件放入脚本保存处: 3.在globals.h中增加函数头“#include "GU ...

  9. LoadRunner配置方案

    1.配置方案运行时设置 选择“Tools”>“Options”.在“Options”对话框有“Run-Time Settings”(运行时设置).“Timeout”(超时).“Run-Time  ...

  10. HDU 6030 Happy Necklace

    矩阵快速幂. 因为任意素数长度都要满足,所以$3$必须满足,$3$一旦满足,其余的肯定满足,也就是说只要考虑字符串末尾两位即可,$dp$一下就可以算方案数了.$n$较大,可以矩阵加速. #includ ...