Spring JdbcTemplate详解及项目中的运用
1.Spring对不同的持久化支持:
Spring为各种支持的持久化技术,都提供了简单操作的模板和回调
| ORM持久化技术 | 模板类 |
| JDBC | org.springframework.jdbc.core.JdbcTemplate |
| Hibernate5.0 | org.springframework.orm.hibernate5.HibernateTemplate |
| IBatis(MyBatis) | org.springframework.orm.ibatis.SqlMapClientTemplate |
| JPA | org.springfrmaework.orm.jpa.JpaTemplate |
2.JdbcTemplate简介
Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。
JdbcTemplate位于
中。其全限定命名为org.springframework.jdbc.core.JdbcTemplate。要使用JdbcTemlate还需一个
这个包包含了一下事务和异常控制
3.JdbcTemplate主要提供以下五类方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
4.batchUpdate()批量插入、更新和删除方法

1 String sql="insert into user (name,deptid) values (?,?)";
2
3 List<Object[]> batchArgs=new ArrayList<Object[]>();
4 batchArgs.add(new Object[]{"caoyc",6});
5 batchArgs.add(new Object[]{"zhh",8});
6 batchArgs.add(new Object[]{"cjx",8});
7
8 jdbcTemplate.batchUpdate(sql, batchArgs);

增:
AspDaoUtil.getJdbcTemplate().executeUpdate(INSERT_SQL,new Object[]{
pkid,field_main.get("mdjlx")
});
删:
AspDaoUtil.getJdbcTemplate().executeUpdate(DELETE_SQL,new Object[]{
pkid
});
改:
AspDaoUtil.getJdbcTemplate().executeUpdate(update_electronicarchives_sql,new Object[]{
dzwjh,dzwjh,pk_id
});
查1:查询条件为主键,数据唯一
FieldList field_main = AspDaoUtil.getJdbcTemplate().queryField(QUERY_SQL, new Object[]{ pkid });
查2:查询条件不为主键,数据可能出现多条
RowList rowList_gdpz = AspDaoUtil.getJdbcTemplate().queryRowList(gdpz_query_sql, new Object[]{pk_id,mdjlx});
for (int r = 0; r < rowList_gdpz.size(); r++) {
FieldList field_gdpz = rowList_gdpz.get(r);
int xh = r + 1;
fjmc = field_gdpz.get("FCAPTION")+xh;
fileurl = field_gdpz.get("fpath");
AspDaoUtil.getJdbcTemplate().executeUpdate(gdpz_insert_sql,new Object[]{
pk_id,qlsxbm,qlsxmc,bmmc,bgqx,sxbbh,fjmc,field_gdpz.get("sfby"),fzxx2,"",bmbm,qlsxlx,fileurl
});
}
5.事务
事务开启: AspDaoUtil.getJdbcTemplate().startTransaction();
执行事务: AspDaoUtil.getJdbcTemplate().commitTransaction();
回滚事务: AspDaoUtil.getJdbcTemplate().rollbackTransaction();
6.项目模板:
public class XXXXX {
private static final String QUERY_BJ_SQL = "SELECT * FROM XXXX WHERE projid = ? ";
private static final String UPDATE_CG_SQL = "UPDATE XXXXX SET tszt=?,tsjg=? WHERE projid=? ";
protected static Logger log = Logger.getRootLogger();
public void finish(String projid){
AspDaoUtil.getJdbcTemplate().startTransaction();
try{
XXXXXX
}catch (Exception e){
e.printStackTrace();
AspDaoUtil.getJdbcTemplate().rollbackTransaction();
}
}
}
7.选择自己的数据源
7.1数据源配置 global-datasource.xml
<?xml version="1.0" encoding="gb2312"?>
<datasource-config>
<!-- 数据库连接配置-Mysql版本-->
<datasource name="XXX" class="jos.framework.jdbc.datasource.DruidDataSource" load="true" default="true" dialect="mysql">
<!-- <property name="driver">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://ip地址:端口号/数据库名称?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8</property>
<property name="username">用户名</property>
<property name="password">密码</property> -->
<property name="initialPoolSize">2</property>
<property name="maxPoolSize">200</property>
<property name="minPoolSize">5</property>
<property name="maxIdleTime">25000</property>
<property name="maxStatements">0</property>
<property name="preferredTestQuery">SELECT 1 FROM C3P0TESTTABLE</property>
<property name="testConnectionOnCheckin">false</property>
<property name="testConnectionOnCheckout">true</property>
<property name="acquireIncrement">5</property>
<property name="idleConnectionTestPeriod">18000</property>
<property name="poolPreparedStatements">true</property>
</datasource>
<!-- 数据库连接配置-Mysql版本-->
<datasource name="XXX" class="jos.framework.jdbc.datasource.DruidDataSource" load="true" default="false" dialect="mysql">
<property name="driver">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://ip地址:端口号/数据库名称?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8</property>
<property name="username">用户名</property>
<property name="password">密码</property>
<property name="initialPoolSize">2</property>
<property name="maxPoolSize">200</property>
<property name="minPoolSize">5</property>
<property name="maxIdleTime">25000</property>
<property name="maxStatements">0</property>
<property name="preferredTestQuery">SELECT 1 FROM C3P0TESTTABLE</property>
<property name="testConnectionOnCheckin">false</property>
<property name="testConnectionOnCheckout">true</property>
<property name="acquireIncrement">5</property>
<property name="idleConnectionTestPeriod">18000</property>
<property name="poolPreparedStatements">true</property>
</datasource>
</datasource-config>
7.2代码JdbcTemplate jdbcTemplate = new JdbcTemplate("XXX");
7.2.1开启事务:jdbcTemplate.startTransaction();
执行事务:jdbcTemplate.commitTransaction();
回滚事务:jdbcTemplate.rollbackTransaction();
Spring JdbcTemplate详解及项目中的运用的更多相关文章
- Spring JdbcTemplate详解
为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架. 作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的 ...
- Spring JdbcTemplate详解(转)
原文地址:http://www.cnblogs.com/caoyc/p/5630622.html 尊重原创,请访问原文地址 JdbcTemplate简介 Spring对数据库的操作在jdbc上面做 ...
- 【转载】Spring JdbcTemplate详解
JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...
- Spring JdbcTemplate详解(9)
JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...
- Spring AOP详解及简单应用
Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...
- 转:Spring AOP详解
转:Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...
- Spring Bean 详解
Spring Bean 详解 Ioc实例化Bean的三种方式 1 创建Bean 1 使用无参构造函数 这也是我们常用的一种.在默认情况下,它会通过反射调⽤⽆参构造函数来创建对象.如果类中没有⽆参构造函 ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
- spring配置文件详解--真的蛮详细
spring配置文件详解--真的蛮详细 转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...
随机推荐
- Cocoapods的安装以及使用
在网上看博客,看了好多次,都没有学会cocoapods,今天上午浪费了一上午的时间,终于算是学会了.其实也是很简单的. iOS 新版 CocoaPods 安装流程 1.换掉现有Ruby默认源(由于好多 ...
- Database: coursera assignment 1
q.1: Find the titles of all movies directed by Steven Spielberg. select title from moviewhere direct ...
- appium不支持Android7.0系统设备解决办法
1. 找到appium的安装目录下的adb.js文件. 2. 打开adb.js,手动修改该文件下的内容: Adb.prototype.getPIDsByName=function(name,cb){ ...
- Adding Form Fields to a MS Word Document
Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...
- Hadoop- MapReduce在实际应用中常见的调优
1.Reduce Task Number 通常来说一个block就对应一个map任务进行处理,reduce任务如果人工不去设置干预的话就一个reduce.reduce任务的个数可以通过在程序中设置 ...
- Hadoop- 分布式资源管理YARN架构讲解
YARN是分布式资源管理,每一台机器都要去管理该台计算机的资源,Yarn负责为MapReduce程序分配运算硬件资源.每一台机器的管理者叫 NodeManager,整个集群的管理者管理着整个集群的No ...
- Zookeeper- Error contacting service. It is probably not running解决方案和原理
搭建启动Zookeeper集群出现Error contacting service. It is probably not running解决方案和原理 1.关闭防火墙 [root@srv01 bi ...
- T61
你参加了这次科学讨论会,有什么体会?What have you learned from the symposium?那墙有点斜.The wall is a little out of the per ...
- listen and translation exercise 53
It was hard work and there weren't any interesting things for him. You should be an expert with comp ...
- javascript基础知识整理(不定时更新)
1.js中真与假的定义: 真:true,非零数字,非空字符串,非空对象 假:false,数字零,空字符串,空对象(null),undefined 2.使用for循环对json进行循环操作 for(va ...