首先,数据库是这样的,很简单。

当然,要引入spring的包,这里我全部导入了,省事。

applicationContext.xml是这样的:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="test" class="jdbc.Test">
  7. <property name="dataSource" ref="dataSource"></property>
  8. </bean>
  9. <bean id="dataSource"
  10. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://localhost:3306/testspring" />
  13. <property name="username" value="root" />
  14. <property name="password" value="" />
  15. </bean>
  16. </beans>

User.Java是这样的:

  1. package jdbc;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String password;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. }

下面来对比一下三种写法:

1、spring

  1. package jdbc;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import javax.sql.DataSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. public class Test {
  9. private DataSource dataSource;
  10. public void setDataSource(DataSource dataSource) {
  11. this.dataSource = dataSource;
  12. }
  13. public void insert(User u) {
  14. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  15. Connection conn = null;
  16. try {
  17. conn = dataSource.getConnection();
  18. PreparedStatement ps = conn.prepareStatement(sql);
  19. ps.setString(1, u.getName());
  20. ps.setString(2, u.getPassword());
  21. ps.executeUpdate();
  22. ps.close();
  23. } catch (SQLException e) {
  24. throw new RuntimeException(e);
  25. } finally {
  26. if (conn != null) {
  27. try {
  28. conn.close();
  29. } catch (SQLException e) {
  30. }
  31. }
  32. }
  33. }
  34. public static void main(String[] args) {
  35. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  36. "applicationContext.xml");
  37. Test t = (Test) ctx.getBean("test");
  38. User u = new User();
  39. u.setName("dd");
  40. u.setPassword("dd");
  41. t.insert(u);
  42. }
  43. }

运行结果是这样的:

2、JdbcTemplate

  1. package jdbc;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import javax.sql.DataSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.jdbc.core.JdbcTemplate;
  9. public class Test {
  10. private DataSource dataSource;
  11. public void setDataSource(DataSource dataSource) {
  12. this.dataSource = dataSource;
  13. }
  14. public void insert(User u) {
  15. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  16. JdbcTemplate template = new JdbcTemplate(dataSource);
  17. template.update(sql, new Object[]{u.getName(), u.getPassword()});
  18. }
  19. public static void main(String[] args) {
  20. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  21. "applicationContext.xml");
  22. Test t = (Test) ctx.getBean("test");
  23. User u = new User();
  24. u.setName("dd");
  25. u.setPassword("dd");
  26. t.insert(u);
  27. }
  28. }

可以看得出简单了很多,不用Connection了。

3、JdbcDaoSupport

这个更简单,不用new JdbcTemplate了。

  1. package jdbc;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  5. public class Test extends JdbcDaoSupport {
  6. //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了
  7. //不用重写也不能重写
  8. public void insert(User u) {
  9. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  10. this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
  11. }
  12. public static void main(String[] args) {
  13. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  14. "applicationContext.xml");
  15. Test t = (Test) ctx.getBean("test");
  16. User u = new User();
  17. u.setName("dd");
  18. u.setPassword("dd");
  19. t.insert(u);
  20. }
  21. }

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 JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...

  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. 图文了解 Kafka 的副本复制机制

    让分布式系统的操作变得简单,在某种程度上是一种艺术,通常这种实现都是从大量的实践中总结得到的.Apache Kafka 的受欢迎程度在很大程度上归功于其设计和操作简单性.随着社区添加更多功能,开发者们 ...

  2. springmvc对象映射个数超过256个

    /** 由于Spring在接受前台传入的List时,就会出现256的IndexOutOfBoundsException异常 设置setAutoGrowCollectionLimit为1024 @par ...

  3. The Quantum L3 router and floating IPs

    This post shows how the Quantum L3 Agent uses the Linux IP stack to implement the Quantum L3 Routing ...

  4. redis实现session共享,哨兵

    一.Redis介绍 1.redis是key-value的存储系统,属于非关系型数据库 2.特点:支持数据持久化,可以让数据在内存中保存到磁盘里(memcached:数据存在内存里,如果服务重启,数据会 ...

  5. 分享知识-快乐自己:SpringBoot集成热部署配置(一)

    摘要: 热部署与热加载: ava热部署与Java热加载的联系和区别: 1):Java热部署与热加载的联系: 1.不重启服务器编译/部署项目 2.基于Java的类加载器实现 2):Java热部署与热加载 ...

  6. Java集合类--->入门下篇

    HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...

  7. Log4j_学习_00_资源帖

    一.log4j2 1. log4j使用教程详解(怎么使用log4j2) 2.Log4j2的基本使用 二.log4j 1.[转]最详细的Log4J使用教程 2.最详细的Log4j使用教程 3.log4j ...

  8. QListWidget列表控件:当鼠标选中某行时,系统会自动设置选中的行的行号,用currentRow()返回回来,没有setCheck或setSelect类似函数

    列表控件的设计思路: 只有QListWidgetItem自己能改变自己的状态(如checked,selected,颜色等)状态,QListWidget是无法改变其项的状态的. 列表控件是被动接受子项的 ...

  9. linux命令学习笔记(1):ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单 如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查 看li ...

  10. 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】

    当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...