Spring4.3.1 JDBCTemplate操作数据库
个人总结,转载请注明出处:http://www.cnblogs.com/lidabnu/p/5679354.html
基于Spring4.3.1官方文档总结,官方文档链接http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jdbc
Spring JDBC能干什么,如下图所示,可见,其目的还是“简化开发”,把通用的业务无关的操作都在框架中做了。
先思考一下使用JDBC操作数据库需要解决哪些问题:
- 如何配置数据库连接;
- 执行查询:根据返回值类型可分为查询出对象,查询出对象列表,查询出数量等标量;然后再有带条件的查询。
- 执行删除、修改和插入;
- 批量操作数据库。
先简要介绍下Spring4.3.1中的JDBC操作相关类:
- JDBCTemplate:最经典也是最流行的类,其他类都基于JDBCTemplate封装;JDBCTemplat是线程安全的,这也是多个Dao共用一个JDBCTemplate的前提
- NamedParameterJdbcTemplate: 封装JDBCTemplate提供可命名的参数赋值方法,来代替JDBCTemplate的传统占位符"?“,当SQL中有多个待赋值参数时更方便。
- SimpleJDBCInsert和SimpleJDBCCall:利用数据库的元数据简化配置,使用它们时,仅需提供数据库中的表名或者存储过程名称以及一组与列名匹配的参数。需要数据库提供足够的元数据支持。
- RDBMSObject:略
一个个看要解决的问题:
如何配置数据库连接:
Spring有三种方式配置数据源,即:使用JNDI来利用外部数据源(例如Tomcat等WEB容器)、利用DBCP管理的数据库连接池、以及使用Spring自带的DriveManagerDataSource(这个不支持连接池,因此不能用于生产环境)。这里仅仅介绍DBCP。其使用方式十分简单,就是在配置文件中配置一个DBCP数据源即可。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/stock"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
当然,一般情况下,数据库的URL以及用户名、密码等均会在额外的配置文件中配置,所以可以利用SpEL表达式来弄。需要使用context:property-placeholder来配置“配置文件的路径”。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> -->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> -->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/stock"></property> -->
<!-- <property name="username" value="root"></property> -->
<!-- <property name="password" value="123456"></property> -->
<!-- <property name="initialSize" value="5"></property> -->
<!-- <property name="maxActive" value="10"></property> -->
<!-- </bean> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/stock"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="stockDao" class="testjdbc.dao.StockDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- <context:component-scan base-package="stock.dao"></context:component-scan> --> </beans>
如何执行查询:
查询标量:
int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject(
"select count(*) from t_actor where first_name = ?", Integer.class, "Joe"); String lastName = this.jdbcTemplate.queryForObject(
"select last_name from t_actor where id = ?",
new Object[]{1212L}, String.class);
查询对象:使用queryForObject方法,输入参数使用Object数组,重载定义“行-对象”转换对象RowMapper
Stock queryStock(String code)
{
return getJdbcTemplate().queryForObject("select * from stock where code=?",new Object[]{code}, new RowMapper<Stock>()
{ public Stock mapRow(ResultSet rs, int arg1) throws SQLException {
Stock stock = new Stock();
stock.setCode(rs.getString("code"));
stock.setName(rs.getString("name"));
stock.setId(rs.getInt("id"));
return stock;
} });
}
查询一批对象:与queryForObject相同
List<Stock> queryStocks()//不带条件
{
return getJdbcTemplate().query(SELECT_SQL, new RowMapper<Stock>()
{ public Stock mapRow(ResultSet rs, int arg1) throws SQLException {
Stock stock = new Stock();
stock.setCode(rs.getString("code"));
stock.setName(rs.getString("name"));
stock.setId(rs.getInt("id"));
return stock;
} });
}
List<Stock> queryStocks(String codePrefix)//带条件
{
return getJdbcTemplate().query("select * from stock where code like ?",new Object[]{codePrefix+"%"}, new RowMapper<Stock>()
{ public Stock mapRow(ResultSet rs, int arg1) throws SQLException {
Stock stock = new Stock();
stock.setCode(rs.getString("code"));
stock.setName(rs.getString("name"));
stock.setId(rs.getInt("id"));
return stock;
} });
}
如何执行删除、修改和插入:
int insert(Stock stock)
{
return getJdbcTemplate().update("Insert into stock(id,code,name) value(?,?,?)", stock.getId(),stock.getCode(),stock.getName());
}
int delete(String stockCode)
{
return getJdbcTemplate().update("delete from stock where code=?", stockCode);
}
如何执行批量操作:
批量操作减少了Client端(应用程序)和数据库服务器之间的网络来回,因此可提升性能,下次再单独写一篇
Spring4.3.1 JDBCTemplate操作数据库的更多相关文章
- JdbcTemplate操作数据库
1.JdbcTemplate操作数据库 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中.同时,为了支 ...
- 编写DAO,通过JdbcTemplate操作数据库的实践
目的:编写DAO,通过Spring中的JdbcTemplate,对数据库中的学生数据进行增删改查操作. 要操作的数据库表结构为: 一.大体框架 1.要利用JdbcTemplate,首先要添加Sprin ...
- 使用JdbcTemplate操作数据库(二十九)
使用JdbcTemplate操作数据库 Spring的JdbcTemplate是自动配置的,你可以直接使用@Autowired来注入到你自己的bean中来使用. 举例:我们在创建User表,包含属性n ...
- 170623、springboot编程之JdbcTemplate操作数据库
使用JdbcTemplate操作mysql数据库! 1.在pom中引入jpa包 <dependency> <groupId>org.springframework.boot&l ...
- Spring Boot教程(二十九)使用JdbcTemplate操作数据库
使用JdbcTemplate操作数据库 Spring的JdbcTemplate是自动配置的,你可以直接使用@Autowired来注入到你自己的bean中来使用. 举例:我们在创建User表,包含属性n ...
- Spring Boot入门系列(十四)使用JdbcTemplate操作数据库,配置多数据源!
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查.如何实现事物控制.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/c ...
- Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库
整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...
- spring-boot-route(七)整合jdbcTemplate操作数据库
在一部分内容中,我们学习了Restful接口的编写,及接口文档的生成.我们需要将接口数据进行持久化存储,这一部分我们主要学习几种持久化框架将数据进行存储.本部分内容中,我们都将使用mysql为例来做为 ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
随机推荐
- delphi中EmbeddedWB网页html相互调用(二)
我们可以通过控件 EmbeddedWB_D5-D2010_Version_14.69.1 来响应html事件,还可以自定义html响应哪些html元素. 控件下载 点击下载 里面有demos文件夹大家 ...
- [LeetCode#277] Find the Celebrity
Problem: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...
- linux系统目录详解
Linux目录结构包括文件类型和一些重要的文件子目录. linux文件系统的最顶端是/,称为linux的root,所有的目录.文件.设备都在/之下.文件类型linux有四种基本文件系统类型:普通文件. ...
- [转]笔记本Ubuntu系统关闭独显+省电降温设置
[转载者按]最近装了Ubuntu 13.04 64 bits版操作系统玩玩,但是发现两个显卡都开着,所以上网查找资料,以在不需要3D的时候关闭Nvidia显卡.通过Bumblebee软件包可以达到这一 ...
- zoj3329 One Person Game
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very simple and ...
- Qt 对象间的父子关系
C++中只要有一个new就必须要有一个delete与之对应 但是Qt中的对象之间有特殊的关系 Qt 对象间的父子关系 每一个对象都保存有它所有子对象的指针 每一个对象都有一个指向其父对象的指针 par ...
- 直接通过浏览器打开Android App 应用
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式例如以下: <a href="[scheme]://[host]/[path]?[query]&quo ...
- HDU 1242 rescue and 优先级队列的条目
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- There is an error while getting planid. No Free partitions available
问题概述 Oracle Advanced Supply Chain Planning最初的设置职责的时候有点问题,不知是不是要打什么补丁或其它配置什么东东,, 这个提示,,但我查到的分区是还有可用分区 ...
- 一大波Java来袭(二)异常处理
概要解析: 本章的知识点能够记为:1图+5keyword+先逮小的.后逮大的 一.基础 (一)定义 1.异常 是指在程序执行的时候发生的一些异常事件.良好的程序设计应该在异常发生的时候提供处理异常的 ...