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数据访问模板:在数据库操作 ...
随机推荐
- 剑指offer——和为s的两个数字VS和为s的连续正数序列
两种方法都类似于快排的变形. #include <iostream> #include <string> using namespace std; bool FindNumbe ...
- CDH- 集群时间同步ntp问题解决
在CDH集群中发现有两台机器获取不到心跳(),导致监控不了机器状态,出现告警 可以使用ntpstat检查与ntp 服务器的时间偏差状态 使用 ntpstat 发现没有同步到ntp时间服务器,运行 nt ...
- artDialog 简单使用!
简介 artDialog是一个轻巧且高度兼容的javascript对话框组件,可让你的网页交互拥有桌面软件般的用户体验. 功能: 支持锁定屏幕(遮罩).模拟alert和confirm.多窗口弹出.静止 ...
- Centos7部署NFS
server1:192.168.1.189 ###客户端 server2:192.168.1.190 ##服务端 1.首先创建共享目录. mkdir -p /data/share 安装nfs ...
- Centos7搭建Mysql-5.6.38,及主从复制。
Server1:192.168.1.189 (主) Server2:192.168.1.190 (从) 1.关闭默认的firewalld防火墙,安装iptables. systemctl disa ...
- Hibernate - 配置c3p0
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuratio ...
- 1147. Heaps (30)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- 删除文件时提示 你需要来自system的权限才能对此文件夹进行更改
问题描述: 我的计算机是Win7 x64操作系统,在我的计算机的F盘中,不知道什么时候多了个“12e4k69m762nzcgt8zx”这样一个文件夹,应该是某个软件自己创建并留下的文件夹,想删除掉则提 ...
- pip3 更改安装源
经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...
- WCF中WcfSvcHost.exe如何禁止自动启动
今天同事问在一个WCF server的解决方案里调试时如何禁止Server自动启动. 经过调查发现, VS的工具WcfSvcHost会在调试时自动扫描工程里的WCF server, 然后启动起来. 如 ...