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&amp;useUnicode=true&amp;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&amp;useUnicode=true&amp;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详解及项目中的运用的更多相关文章

  1. Spring JdbcTemplate详解

    为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架. 作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的 ...

  2. Spring JdbcTemplate详解(转)

    原文地址:http://www.cnblogs.com/caoyc/p/5630622.html   尊重原创,请访问原文地址 JdbcTemplate简介 Spring对数据库的操作在jdbc上面做 ...

  3. 【转载】Spring JdbcTemplate详解

    JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...

  4. Spring JdbcTemplate详解(9)

    JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...

  5. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  6. 转:Spring AOP详解

    转:Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  7. Spring Bean 详解

    Spring Bean 详解 Ioc实例化Bean的三种方式 1 创建Bean 1 使用无参构造函数 这也是我们常用的一种.在默认情况下,它会通过反射调⽤⽆参构造函数来创建对象.如果类中没有⽆参构造函 ...

  8. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  9. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

随机推荐

  1. iOS define 宏定义 和 const定义常量区别

    const   const 是c++中的修饰符.  c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1.  对于co ...

  2. oracle 数据库运维知识

    1.在数据库中连接用某个用户连接数据库报错: Product: DbVisualizer Pro 9.1.1 Build: #2063 (2013/10/01 12:27) Java VM: Java ...

  3. 编译debian内核

    玩腻了开发板,在pc上编译linux内核. debian 官方的内核文档见http://kernel-handbook.alioth.debian.org 我选择编译与当前内核版本对应的linux内核 ...

  4. Dictionary and KeyValuePair关系

    简单一句话: Dictionary 是 由 KeyValuePair结构 组成的集合 The Dictionary<TKey, TValue>.Enumerator.Current pro ...

  5. matlab中的try...catch...end

    我们知道,matlab的代码是按行执行的,如果碰到错误行,则程序中断.try..catch可以使得可能出错代码不影响后面代码的继续执行,也可以检查,排查,解决程序的一些错误,增强代码的鲁棒性和可靠性. ...

  6. TYOI 1015 Game:博弈 结论【步数之和的奇偶性】

    题意: 明明和亮亮在玩一个游戏. 桌面上一行有n个格子,一些格子中放着棋子. 明明和亮亮轮流选择如下方式中的一种移动棋子(图示中o表示棋子,*表示空着的格子): (1)当一枚棋子的右边是空格子的话,可 ...

  7. 企业安全建设之搭建开源SIEM平台(上)

    前言 SIEM(security information and event management),顾名思义就是针对安全信息和事件的管理系统,针对大多数企业是不便宜的安全系统,本文结合作者的经验介绍 ...

  8. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

  9. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...

  10. 高性能MySQL之【第十六章MySQL用户工具】学习记录

    接口工具:      Msql Workbench   http://www.mysql.com/products/workbench      SQLyog  http://www.webyog.c ...