Alternatively, create an own implementation of
JdbcDaoSupport
class, and do whatever you want. Dive inside the source code ofJdbcDaoSupport
, it’s just a simple helper class to create ajdbcTemplate
.
我创建了一个自己的类,扩展了JdbcDaoSupport类:
package demo1;import javax.annotation.PostConstruct;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class JdbcDaoSupportExtend extends JdbcDaoSupport { @Autowired private DataSource dataSource; @PostConstruct public void initialize() { setDataSource(dataSource); }}
在自己的Dao中继承了这个自己扩展的类:
package demo1;import org.springframework.stereotype.Repository;@Repository(value = "userDao")public class UserDaoImpl extends JdbcDaoSupportExtend implements UserDaoI { @Override public void outMoney(Integer id, Double money) { getJdbcTemplate().update("update user set money = money - ? where id = ?", money, id); } @Override public void inMoney(Integer id, Double money) { getJdbcTemplate().update("update user set money = money + ? where id = ?", money, id); }}