`

Spring JDBC笔记

 
阅读更多

Table 12.1. Spring JDBC - who does what?

Action Spring You
Define connection parameters. X
Open the connection. X  
Specify the SQL statement.   X
Declare parameters and provide parameter values   X
Prepare and execute the statement. X  
Set up the loop to iterate through the results (if any). X  
Do the work for each iteration.   X
Process any exception. X  
Handle transactions. X  
Close the connection, statement and resultset. X  


  • JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.

  • NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.

  • SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.

  • SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.

  • RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.

 

  • JdbcTemplate - 这是经典的也是最常用的Spring对于JDBC访问的方案。这也是最低级别的封装, 其他的工作模式事实上在底层使用了JdbcTemplate作为其底层的实现基础。

  • NamedParameterJdbcTemplate - 对JdbcTemplate做了封装,提供了更加便捷的基于命名参数的使用方式而不是传统的JDBC所使用的“?”作为参数的占位符。这种方式在你需要为某个SQL指定许多个参数时,显得更加直观而易用。

  • SimpleJdbcTemplate - 这个类结合了JdbcTemplate和NamedParameterJdbcTemplate的最常用的功能,同时它也利用了一些Java 5的特性所带来的优势,例如泛型、varargs和autoboxing等,从而提供了更加简便的API访问方式。需要工作在Java 5以上的环境中。

  • SimpleJdbcInsert 和 SimpleJdbcCall - 这两个类可以充分利用数据库元数据的特性来简化配置。通过使用这两个类进行编程,你可以仅仅提供数据库表名或者存储过程的名称以及一个Map作为参数。其 中Map的key需要与数据库表中的字段保持一致。这两个类通常和SimpleJdbcTemplate配合使用。这两个类需要工作在JDK 5以上,同时数据库需要提供足够的元数据信息。

  • RDBMS 对象包括MappingSqlQuery, SqlUpdate and StoredProcedure - 这种方式允许你在初始化你的数据访问层时创建可重用并且线程安全的对象。该对象在你定义了你的查询语句,声明查询参数并编译相应的Query之后被模型化。一旦模型化完成,任何执行函数就可以传入不同的参数对之进行多次调用。

0
1
分享到:
评论

相关推荐

    SpringJDBC笔记

    内容详细讲解了SpringJDBC所有内容,以及使用,适合新手

    Spring_JDBC模板笔记

    Spring_JDBC模板笔记Spring_JDBC模板笔记Spring_JDBC模板笔记Spring_JDBC模板笔记Spring_JDBC模板笔记

    马士兵Spring课堂笔记(超级详细版).pdf

    马士兵Spring课堂笔记(超级详细版) 本资源主要讲解了Spring框架的基础概念和应用,涵盖了面向接口编程、IOC/DI、AOP、Spring应用IOC/DI、Spring应用AOP、Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合、Spring...

    spring-note spring 读书笔记

    详细介绍了Spring 与 IOC,Aop,还有Spring与JDBC,HIBERNATE,STRUTS,Transaction 进行集成,配置文件的编写

    Spring学习笔记

    Spring学习笔记Spring spring的配置 IOC 依赖注入 基于Xml的注入 基于注释的注入 Spring的自动注入和属性自动注入 AOP 静态代理 动态代理 使用spring实现AOP 基于Annotation实现AOP 基于XML实现AOP ...

    Spring学习笔记.zip

    Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control: 反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring MVC 和持久层 Spring JDBC 以及业务层事务...

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    Spring框架学习笔记文档

    Spring框架的学习笔记,包括了springIOC,spirng注入,springAoP,SpringJdbc模板的使用,Spring声明式事务。

    Spring源代码解析.rar

    Spring源代码解析3:Spring JDBC .doc Spring源代码解析4:Spring MVC .doc Spring源代码解析5:Spring AOP获取Proxy .doc Spring源代码解析6:Spring声明式事务处理 .doc Spring源代码解析7:Spring AOP中对拦截器...

    使用 Spring MVC + JDBC Template 实现筛选、检索功能(maven)

    使用 Spring MVC + JDBC Template 实现筛选、检索功能(maven),图文教程地址:https://blog.csdn.net/qq_40147863/article/details/86187642

    Java相关课程系列笔记之九Servlet学习笔记

    Java相关课程系列笔记之四JDBC学习笔记 Java相关课程系列笔记之六HTML学习笔记 Java相关课程系列笔记之七CSS学习笔记 Java相关课程系列笔记之八JavaScript学习笔记 Java相关课程系列笔记之九Servlet学习笔记 Java...

    Spring 各种例子:Spring Data Elasticsearch,JDBC,JPA,LDAP,MongoDB

    - immutables:展示了使用Immutables的Spring Data JDBC用法。 4. Spring Data JPA: - eclipselink:展示了如何在Spring Boot和Eclipselink中使用Spring Data JPA的示例项目。 - example:包含了各种示例包,...

    J2EE框架_笔记_b

    49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate...

    J2EE三大框架_笔记_a

    49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate...

    J2EE框架_笔记_c

    49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + Spring + Hibernate...

    spring 学习小结笔记

    spring学习笔记 包括: spring + hiberante 访问数据库 JDBC 操作的JAVA 的对象化: (sql对表操作) AOP 面向方面编程,实现"切面

    java SSH框架架构学习_笔记.struts,spring,hibernate. IOC,AOP,JDBC,编写个留言管理系统网站

    java SSH框架架构学习_笔记.zip java SSH框架架构学习_笔记.struts,spring,hibernate. IOC,AOP,JDBC,编写个留言管理系统网站

    Java相关课程系列笔记

    Java相关课程系列笔记之十五Spring学习笔记(建议用WPS打开) Java相关课程系列笔记之十一Ajax学习笔记(建议用WPS打开) Java相关课程系列笔记之四JDBC学习笔记(建议用WPS打开) Java相关课程系列笔记之五XML学习...

Global site tag (gtag.js) - Google Analytics