Spring从菜鸟到高手(四)(上)使用JdbcTemplate类实现用户登陆验证、批量更新

看了我前面几篇文章的朋友我相信基础已经有了,不知道大家在使用JDBC连接数据库的时候是不是厌倦了,不停的捕获异常不停的try catch 反正我是厌倦了,代码没写多少,一大半是异常的抓取,Spring的高手们也知道我们厌倦了,所以他们给我们一个JdbcTemplate类这个类把所有的异常抓取代码封装在类的内部,我们要做的只是处理业务逻辑就行了,好了,我来给大家介绍一下吧

org.springframework.jdbc.core
Class JdbcTemplate

java.lang.Object
  extended by org.springframework.jdbc.support.JdbcAccessor
      extended by org.springframework.jdbc.core.JdbcTemplate
All Implemented Interfaces:
InitializingBean, JdbcOperations

JdbcTemplate类的一个方法

 Objectexecute(PreparedStatementCreator psc, PreparedStatementCallback action)
          Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.

用于对PreparedStatement对象的处理可是该如何处理呢?看看PreparedStatementCreator类吧


org.springframework.jdbc.core
Interface PreparedStatementCreator

 


这个接口只有一个方法

 

 PreparedStatementcreatePreparedStatement(Connection con)
          Create a statement in this connection.

这个方法对一个Connection连接进行处理返回一个 PreparedStatement对象给JdbcTemplate

那么execute方法的另一个参数PreparedStatementCallback又是干什么的呢?


org.springframework.jdbc.core
Interface PreparedStatementCallback

All Known Implementing Classes:
AbstractLobCreatingPreparedStatementCallback

 这个接口也只有一个方法

 ObjectdoInPreparedStatement(PreparedStatement ps)
          Gets called by JdbcTemplate.execute with an active JDBC PreparedStatement.

这个方法对PreparedStatement对象进行处理返回一个结果,这下大家可能有一点了解了,不了解也没事,因为一开始我就不太明白,看看我的代码吧

现在还要向大家介绍JdbcTemplate类的另一个方法

 int[]batchUpdate(String sql, BatchPreparedStatementSetter pss)
          Issue multiple updates on a single PreparedStatement, using JDBC 2.0 batch updates and a BatchPreparedStatementSetter to set values.

批量更新这个方法接受一个BatchPreparedStatementSetter类对象


org.springframework.jdbc.core
Interface BatchPreparedStatementSetter


 intgetBatchSize()
          Return the size of the batch.
 voidsetValues(PreparedStatement ps, int i)
          Set values on the given PreparedStatement.

这个接口有两个方法一个是返回一个整数告诉JdbcTemplate需要更新几条信息,另一个方法是对PreparedStatemten对象设置值,看代码...

 

 对字符串进行拆分赋值

10e71b13bac.jpg

          Junit测试运行类10e71ac0f3c.jpg

          核心类10e71ad0f1a.jpg

          运行结果

10e71aa97e5.jpg

         我的MyPreparedStatementCreator类10e71aaf62e.jpg

         我的MYPreparedStatementCallback类10e71aa303d.jpg

         添加的XML110e71a93a0a.jpg

         添加的XML210e71a6d7c3.jpg

 

          XML配置文件

10e71a53813.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值