spring—JdbcTemplate使用
JdbcTemplate基本使用
01-JdbcTemplate基本使用-概述(了解)
JdbcTemplate是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。
更新操作:
jdbcTemplate.update (sql,params)
查询操作:
jdbcTemplate.query (sql,Mapper,params)jdbcTemplate.queryForObject(sql,Mapper,params)
JdbcTemplate基本使用
①导入spring-jdbc和spring-tx坐标
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.5.RELEASE</version></dependency>
②创建数据库表和实体
③创建JdbcTemplate对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
"><context:property-placeholder location="jdbc.properties"/><bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"/></bean><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean></beans>
④执行数据库操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringJunitTest {@AutowiredJdbcTemplate jdbcTemplate;@Testpublic void testDelte(){jdbcTemplate.update("delete from student where sno='12'");}public void testOneQuery(){Long student=jdbcTemplate.queryForObject("select count(*) from student",Long.class);System.out.println(student);}public void testQuery(){List<Student> list=jdbcTemplate.query("select * from student",new BeanPropertyRowMapper<Student>(Student.class));System.out.println(list);}public void testUpdate(){jdbcTemplate.update("update student set name='aa' where sno='12'");}public void testInsert(){jdbcTemplate.update("insert into student values (?,?)","12","kk");}}
spring—JdbcTemplate使用相关推荐
- Spring JdbcTemplate方法详解
2019独角兽企业重金招聘Python工程师标准>>> Spring JdbcTemplate方法详解 标签: springhsqldbjava存储数据库相关sql 2012-07- ...
- Spring JdbcTemplate的queryForList(String sql , ClassT elementType)易错使用--转载
原文地址: http://blog.csdn.net/will_awoke/article/details/12617383 一直用ORM,今天用JdbcTemplate再次抑郁了一次. 首先看下这个 ...
- 转载:为什么使用ibatis而不用spring jdbcTemplate
第一:ibatis仅仅是对jdbc薄薄的一层封装,完全不丧失sql的灵活性 第二:ibatis所有的sql都可以放在配置文件中,这样有利于sql的集中管理,特别是在sql tuning是很容易把得到所 ...
- Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)返回非映射实体类的解决方法
Spring JdbcTemplate的queryForList(String sql , Class elementType)返回非映射实体类的解决方法 参考文章: (1)Spring JdbcTe ...
- 【SSM框架系列】Spring - JdbcTemplate声明式事务
JdbcTemplate概述 以往使用jdbc时,每次都需要自己获取PreparedStatement,执行sql语句,关闭连接等操作.操作麻烦冗余,影响编码的效率. Spring把对数据库的操作在j ...
- spring jdbctemplate调用存储过程,返回list对象
注:本文来源于< spring jdbctemplate调用存储过程,返回list对象 > spring jdbctemplate调用存储过程,返回list对象 方法: /*** 调用存储 ...
- SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装
SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装 >>>>>>>& ...
- spring JdbcTemplate数据库查询实例
使用JdbcTemplate查询数据库的例子 配置等可以看前一篇文章: Spring JdbcTemplate实例 创建数据库 可以使用下面的SQL create table A( `id` INT ...
- Spring JdbcTemplate实例
简介 Spring JdbcTemplate类是Spring提供的简化数据库操作的一个类,这个类使用了模板方法模式,可以减少一些重复代码.这里主要演示一下 JdbcTemplate 的使用. 完整的代 ...
- Spring JdbcTemplate示例
Spring JdbcTemplate示例 Spring JdbcTemplate是Spring JDBC包中最重要的类. 目录[ 隐藏 ] 1 Spring JdbcTemplate 1.1 Spr ...
最新文章
- 算法面试经常需要你手写的三个排序算法(Python语言)
- matlab 自动控制仿真,Matlab在自动控制系统建模与仿真中的应用
- 【Python】9个必知的Python操作文件/文件夹方法
- 简单的Flash网络游戏源代码
- 资源共享的两阶段交叉效率DEA模型及matlab应用:地区科技投入产出效率案例分析,文后有网盘链接
- 微信支付商户证书cert.zip中确实rootca.pem文件解决方法
- 天轰穿Visual Studio2005入门.Net2.0系列视频教程
- cin.get()的用法
- 5G NGC — 关键技术 — 网络切片 — 网络切片管理器网元
- 对sin(x)^n dx or cos(x)^n dx的计算验证过程以及结论
- PowerDesigner一键导出数据库设计表结构
- 惠普omen测试软件,性能测试:高品质体验主流游戏
- 向量的点乘与叉乘的几何意义
- 后端人眼中的Vue(五)
- (附源码)计算机毕业设计SSM基于Java的图书馆座位预约系统
- 仿918回忆模特写真网整站数据库源码 zblog内核,
- 编辑引导扇区修复分区引导解决磁盘分区打不开
- 交通规划——基于TransCAD的线性参照和动态分段流程实现
- Ekho TTS网页改版,支持7种语言的在线demo
- 做数据分析,软件工具少不了,好用的数据分析软件工具
热门文章
- 剑指Offer09. 用两个栈实现队列
- linux网络编程九:splice函数,高效的零拷贝
- java开发属于后端吗,值得一读!
- 最强整理!字节跳动历年Android中高级面试题全收录!附超全教程文档
- LeetCode Largest Number
- configure: error: You need a C++ compiler for C++ support.
- python ==字符串
- Lua初学习 9-12 基础
- 关于内存的一些基础知识
- CentOS5.6环境安装oracle 10g(完整版)