spring3-annotation-JdbcDaoSupport

末鹿安然 提交于 2019-11-28 10:34:58
gkamal

You can use one of the below approaches. The first one - taking a dataSource is preferred / recommended as you don't expose a SpringFramework class in your public interface. Both will work.

@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{

  @Autowired
  TestDaoImpl(DataSource dataSource) {
    setDataSource(dataSource);
  }
}

Or

@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{

  @Autowired
  TestDaoImpl(JDBCTemplate template) {
    setJdbcTemplate(template);
  }
}

I even feel injecting datasource as a constructor to your DAO is a unnecessary coding step. Why not inject datasource in Spring config XML into JDBC template and just get jdbctTemplate object in every DAO.

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>
and let your DAO extend JDBCSupport class.

public class PersonDao extends JdbcDaoSupport{
public List<Person> selectAll(){
    String selectAllSql = "SELECT * FROM PERSON;";

    return getJdbcTemplate().query(selectAllSql, new PersonRowMapper());

........ }

}

Full example : http://www.studytrails.com/frameworks/spring/spring-jdbc-dao-support.jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!