Dashboard > SpringSide Wiki > ... > DAO > JdbcTemplate
  SpringSide Wiki Log In | Sign Up   View a printable version of the current page.  
  JdbcTemplate
Added by SpringSideTeam, last edited by SpringSideTeam on 2006-12-26  (view change)
Labels: 

Spring的JDBCTemplate

当hql等查询方式不能满足性能或灵活性的要求,必须使用SQL时,大家有三种选择:

第一、使用Hibernate 的sql 查询函数,将查询结果对象转为Entity对象。

第二、使用Hibernate Session的getConnection 获得JDBC Connection,然后进行纯JDBC API操作;

第三、选择把Spring的JDBCTemplate作为一种很不错的JDBC Utils来使用。

     JDBCTemplate的使用很简单,只要在ApplicationContext文件里定义一个jdbcTemplate节点,POJO获得注入后可以直接执行操作,不需要继承什么基类,详见JDBCTemplate参考文档

     AplicationContext定义:

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

实际使用: 

SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, params);

Tips1: jdbcTemplate有很多的ORM化回调操作将返回结果转为对象列表,但很多时候还是需要返回ResultSet,Spring有提供一个类似ResultSet的 Spring SqlRowSet对象。

         

Tips2:.注意jdbcTemplate尽量只执行查询操作,莫要进行更新,否则很容易破坏Hibernate的二级缓存体系。

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 2006-09-07) - Bug/feature request - Contact Administrators