Spring-jdbcTemplate

发布于 2024-12-07 06:17:55 字数 498 浏览 0 评论 0原文

我刚刚开始使用 Spring 框架。我也在使用 DBCP 池,但我仍然不确定如何正确使用 jdbcTemplate。

最佳实践是在多个 DAO 之间重用创建/注入的 jdbcTemplate 实例,或者为每个 DAO 创建 jdbcTemplate 是正确的吗?

我目前正在使用注释方法:

public class FooDAO  {

  private JdbcTemplate jdbcTemplate;

  @Autowired
  public void setDatasource( DataSource dataSource ) {
     this.jdbcTemplate = new JdbcTemplate( dataSource );
  }
}

我知道 JdbcDaoSupport,但我不知道如何注入数据源,因为方法 setDatasource 被标记为最终方法。

但我仍然不确定重用创建的 jdbcTemplate 是否是最佳实践。

I'm just beginning with Spring framework. I'm also using DBCP pooling and i'm still not sure how to work right with jdbcTemplate.

It is best practice to reuse created/injected jdbcTemplate instance between multiple DAOs or it is right to create jdbcTemplate for each DAO ?

I'm currently using annotation approach:

public class FooDAO  {

  private JdbcTemplate jdbcTemplate;

  @Autowired
  public void setDatasource( DataSource dataSource ) {
     this.jdbcTemplate = new JdbcTemplate( dataSource );
  }
}

I'm aware about JdbcDaoSupport, but I don't know how to inject datasource, because method setDatasource is marked as final.

But still, I'm not sure if is best practice to reuse created jdbcTemplate or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌入人心 2024-12-14 06:17:55

将其注入并分享。不要称之为“新”;它将控制权从 Spring bean 工厂手中夺走。

Inject it in and share it. Don't call "new"; that takes control out of the hands of the Spring bean factory.

安人多梦 2024-12-14 06:17:55

我知道 JdbcDaoSupport,但我不知道如何注入数据源,因为方法 setDatasource 被标记为最终方法。

public class JdbcDaoSupportTest extends JdbcDaoSupport {

    public void insert() { 
        this.getJdbcTemplate().execute("insert into  tb_test1 values(1,'ycl','123')");
         System.out.println("complete...");
    }
}

Spring调用set方法,不关心该方法是否是final。

<bean id="jdbcDaoSupportTest" class="com.xxxxx.JdbcDaoSupportTest">
        <property name="dataSource" ref="dataSource" />
</bean>

然后在你的 JdbcDaoSupportTest 中,你可以调用 this.getJdbcTemplate() 来获取 JdbcTemplate
任何运营商。

I'm aware about JdbcDaoSupport, but I don't know how to inject datasource, because method setDatasource is marked as final.

public class JdbcDaoSupportTest extends JdbcDaoSupport {

    public void insert() { 
        this.getJdbcTemplate().execute("insert into  tb_test1 values(1,'ycl','123')");
         System.out.println("complete...");
    }
}

Spring call set Method, don't care whether the method is final or not.

<bean id="jdbcDaoSupportTest" class="com.xxxxx.JdbcDaoSupportTest">
        <property name="dataSource" ref="dataSource" />
</bean>

then in your JdbcDaoSupportTest, you can call this.getJdbcTemplate() to get JdbcTemplate do
any operator.

为人所爱 2024-12-14 06:17:55
try {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "select user.id as id,user.roll_no as createdId,user.name as name,user.type as company,role.role as year "
            + "from user_role join user on  user.id=user_role.user_id "
            + "join role on role.id=user_role.role_id "
            + "where (user.status='ACTIVE' or user.status='ADMIN') AND user.username='" + userName + "'";
    UserVo userDetails = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserVo>(UserVo.class));
    or
    Long company = jdbcTemplate.queryForObject(sql, Long.class);
    or
    List<UserVo> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserVo>(UserVo.class));
    logger.info("Retrieve user details by username");
    return userDetails;
} catch (Exception e) {
    logger.error("error in  getting UserDetails using UserName", e);
}
try {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "select user.id as id,user.roll_no as createdId,user.name as name,user.type as company,role.role as year "
            + "from user_role join user on  user.id=user_role.user_id "
            + "join role on role.id=user_role.role_id "
            + "where (user.status='ACTIVE' or user.status='ADMIN') AND user.username='" + userName + "'";
    UserVo userDetails = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserVo>(UserVo.class));
    or
    Long company = jdbcTemplate.queryForObject(sql, Long.class);
    or
    List<UserVo> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserVo>(UserVo.class));
    logger.info("Retrieve user details by username");
    return userDetails;
} catch (Exception e) {
    logger.error("error in  getting UserDetails using UserName", e);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文