JdbcDaoSupport的作用是什么? [英] What is JdbcDaoSupport used for?

查看:503
本文介绍了JdbcDaoSupport的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中,当我们插入数据库时​​,是否可以使用JdbcDaoSupport.我的问题是,使用它的好处是什么?在什么情况下应该使用它?

In Spring, when we are inserting into the database, we can use JdbcDaoSupport or not. My question is, what are the advantages in using it and in which circumstances we should use it?

推荐答案

根据以下答案:

  • Proper way to inject parent class dependencies with Spring annotations
  • spring3-annotation-JdbcDaoSupport
  • NamedParameterJdbcDaoSupport datasource autowire?

JdbcDaoSupport NamedParameterJdbcDaoSupport SimpleJdbcDaoSupport 是不必要的,是精神上的尘埃.它们不保存任何代码行,因为您需要将数据源或模板注入其中.

JdbcDaoSupport, NamedParameterJdbcDaoSupport, SimpleJdbcDaoSupport are unnecessary and are mental dust. They doesn't save any line of code because you need to inject data-source or template into.

我的建议-根据文档在XML/class config中为每个数据源创建模板并重用/注入它们,因为根据文档,模板是线程安全的:

What I recommend - to create templates in XML/class config per data source and reuse/inject them as templates are thread safe according to docs:

配置后,JdbcTemplate实例是线程安全的.如果您的应用程序访问多个数据库(这需要多个数据源,然后需要多个不同配置的JdbcTemplates),则可能需要多个JdbcTemplate实例.

Once configured, a JdbcTemplate instance is threadsafe. You may want multiple JdbcTemplate instances if your application accesses multiple databases, which requires multiple DataSources, and subsequently multiple differently configured JdbcTemplates.

比较applicationContext.xml:

<bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" 
      class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>

YourDaoImpl.java:

public class YourDaoImpl implements YourDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public int tableExists(String table) {
        String sql = "select count(*) from all_tables"
                + "  where table_name = :tbl";
        return jdbcTemplate.queryForObject(
                    sql, new MapSqlParameterSource("tbl", table), Integer.class);
    }
}

JdbcDaoSupport.java:

public class YourDaoImpl extends NamedParameterJdbcDaoSupport implements YourDao {

   @Autowired
    public void setDs(DataSource dataSource) {
        setDataSource(dataSource);
    }

    @Override
    public int tableExists(String table) {
        String sql = "select count(*) from all_tables"
                + "  where table_name = :tbl";
        return getNamedParameterJdbcTemplate()
               .queryForObject(
                       sql,
                       new MapSqlParameterSource("tbl", table), Integer.class);
    }

}

更新有关JdbcTemplate/NamedParameterJdbcTemplate的无状态(以及线程安全)的官方声明,请参见此处

UPDATE Official statement about stateless (and so thread safety) of JdbcTemplate/NamedParameterJdbcTemplate here https://jira.springsource.org/browse/SPR-11478

这篇关于JdbcDaoSupport的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆