Spring JdbcTemplate+JdbcDaoSupport实例
1. 不使用JdbcTemplate示例
- 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示例
- 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 here
- public 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/beans
- http://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/beans
- http://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 JdbcTemplate+JdbcDaoSupport实例的更多相关文章
- ref:Spring JdbcTemplate+JdbcDaoSupport实例
ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...
- Spring JdbcTemplate+JdbcDaoSupport实例(和比较)
首先,数据库是这样的,很简单. 当然,要引入spring的包,这里我全部导入了,省事. applicationContext.xml是这样的: <?xml version="1.0&q ...
- 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数据访问模板:在数据库操作 ...
随机推荐
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
- sshd_config OpenSSH SSH 进程配置文件配置说明
名称 sshd_config – OpenSSH SSH 服务器守护进程配置文件 大纲 /etc/ssh/sshd_config 描述sshd 默认从 /etc/ssh/sshd_config 文件( ...
- 转载--void指针(void *的用法)
转自:jimmy 指针有两个属性:指向变量/对象的地址和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从 ...
- 使用CLion
CLion是JetBrains公司的一款C++的IDE.默认使用Cmake构建. ubuntu和fedora下的安装 在ubuntu下安装了CLion,和QtCreator相比: ibus输入法能输入 ...
- python开发学习-day06(模块拾忆、面向对象)
s12-20160130-day06 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- ASP.NET Zero--4.不使用谷歌字体,提升加载速度
jtable控件样式中会使用到谷歌字体,每次访问都特别慢 1.打开jtable.css文件 [..\MyCompanyName.AbpZeroTemplate.Web\libs\jquery-jtab ...
- [实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩
写在前面 关于该项目,已经很久没更新了.实在是找不到一个好的ui,没办法就在网上找了一个还不错的,就凑合着先用着吧,先出第一版,以后的再想着去优化.最近更新与网盘项目相关的内容是准备在项目中使用一个美 ...
- loadrunner生成随机uuid的方法
在globals.h中定义方法: 方法: 1.将生成GUID方法放在新建的GUID.h文件中: 2.把这个文件放入脚本保存处: 3.在globals.h中增加函数头“#include "GU ...
- LoadRunner配置方案
1.配置方案运行时设置 选择“Tools”>“Options”.在“Options”对话框有“Run-Time Settings”(运行时设置).“Timeout”(超时).“Run-Time ...
- HDU 6030 Happy Necklace
矩阵快速幂. 因为任意素数长度都要满足,所以$3$必须满足,$3$一旦满足,其余的肯定满足,也就是说只要考虑字符串末尾两位即可,$dp$一下就可以算方案数了.$n$较大,可以矩阵加速. #includ ...