Spring JdbcTemplate+JdbcDaoSupport实例(和比较)
首先,数据库是这样的,很简单。
当然,要引入spring的包,这里我全部导入了,省事。
applicationContext.xml是这样的:
- <?xml version="1.0" encoding="UTF-8"?>
- <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="test" class="jdbc.Test">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <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/testspring" />
- <property name="username" value="root" />
- <property name="password" value="" />
- </bean>
- </beans>
User.Java是这样的:
- package jdbc;
- public class User {
- private int id;
- private String name;
- private String password;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
下面来对比一下三种写法:
1、spring
- package jdbc;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- private DataSource dataSource;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, u.getName());
- ps.setString(2, u.getPassword());
- ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- }
- }
- }
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
运行结果是这样的:
2、JdbcTemplate
- package jdbc;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- public class Test {
- private DataSource dataSource;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- JdbcTemplate template = new JdbcTemplate(dataSource);
- template.update(sql, new Object[]{u.getName(), u.getPassword()});
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
可以看得出简单了很多,不用Connection了。
3、JdbcDaoSupport
这个更简单,不用new JdbcTemplate了。
- package jdbc;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- public class Test extends JdbcDaoSupport {
- //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了
- //不用重写也不能重写
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
Spring JdbcTemplate+JdbcDaoSupport实例(和比较)的更多相关文章
- ref:Spring JdbcTemplate+JdbcDaoSupport实例
ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...
- Spring JdbcTemplate+JdbcDaoSupport实例
在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...
- Spring + JdbcTemplate + JdbcDaoSupport examples
In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the over ...
- Spring JdbcTemplate batchUpdate() 实例
在某些情况下,可能需要将一批记录插入到数据库中.如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行. 在上述情况下,你可以使用 JdbcTemplate BATCHUPDAT ...
- Spring JdbcTemplate查询实例
这里有几个例子向您展示如何使用JdbcTemplate的query()方法来查询或从数据库提取数据.整个项目的目录结构如下: 1.查询单行数据 这里有两种方法来查询或从数据库中提取单行记录,并将其转换 ...
- 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析
Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...
- Spring JdbcTemplate 的使用与学习(转)
紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...
- 使用Spring JDBCTemplate简化JDBC的操作
使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...
- JAVA入门[18]-JdbcTemplate简单实例
一.关于JdbcTemplate JdbcTemplate是最基本的Spring JDBC模板,这个模板支持简单的JDBC数据库访问功能以及基于索引参数的查询. Spring数据访问模板:在数据库操作 ...
随机推荐
- 图文了解 Kafka 的副本复制机制
让分布式系统的操作变得简单,在某种程度上是一种艺术,通常这种实现都是从大量的实践中总结得到的.Apache Kafka 的受欢迎程度在很大程度上归功于其设计和操作简单性.随着社区添加更多功能,开发者们 ...
- springmvc对象映射个数超过256个
/** 由于Spring在接受前台传入的List时,就会出现256的IndexOutOfBoundsException异常 设置setAutoGrowCollectionLimit为1024 @par ...
- 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 ...
- redis实现session共享,哨兵
一.Redis介绍 1.redis是key-value的存储系统,属于非关系型数据库 2.特点:支持数据持久化,可以让数据在内存中保存到磁盘里(memcached:数据存在内存里,如果服务重启,数据会 ...
- 分享知识-快乐自己:SpringBoot集成热部署配置(一)
摘要: 热部署与热加载: ava热部署与Java热加载的联系和区别: 1):Java热部署与热加载的联系: 1.不重启服务器编译/部署项目 2.基于Java的类加载器实现 2):Java热部署与热加载 ...
- Java集合类--->入门下篇
HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...
- Log4j_学习_00_资源帖
一.log4j2 1. log4j使用教程详解(怎么使用log4j2) 2.Log4j2的基本使用 二.log4j 1.[转]最详细的Log4J使用教程 2.最详细的Log4j使用教程 3.log4j ...
- QListWidget列表控件:当鼠标选中某行时,系统会自动设置选中的行的行号,用currentRow()返回回来,没有setCheck或setSelect类似函数
列表控件的设计思路: 只有QListWidgetItem自己能改变自己的状态(如checked,selected,颜色等)状态,QListWidget是无法改变其项的状态的. 列表控件是被动接受子项的 ...
- linux命令学习笔记(1):ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单 如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查 看li ...
- 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...