JdbcTemplate 中用BeanPropertyRowMapper<> 自动装配查询结果 List
Dao中直接 注入 JdbcTemplate
@Service("eosDao")
public class EosDao {
@Resource
private JdbcTemplate jdbcTemplate;
查询一个list
public int addAccount(EosAccount eosAccount){ String sql = "insert into account(pk,accountname) values (?,?)"; try { int res = jdbcTemplate.update(sql,eosAccount.getPk(),eosAccount.getAccountname()); return res ; }catch (Exception e){ LOG.error("updateWithDraw error!",e); return 0; } }
拓展:
-- 使用Spring的JdbcTemplate和BeanPropertyRowMapper完成的JDBC - 我的IT技术杂谈 - ITeye博客
https://zmx.iteye.com/blog/373454
因为本人上面 是springboot 项目 不用类似下方这样原始设定 new JdbcTemplate(SQLConnUtil.getDataSource());
public class StuDaoImple implements StuDaointer { private JdbcTemplate jdbctemp = null; public StuDaoImple() { jdbctemp = new JdbcTemplate(SQLConnUtil.getDataSource()); } // 所有添,删,改的方法都可以用jdbctemp.update();方法 public void addStu(Stu stu) { String sql = "insert into stu values(?,?,?)"; Object[] obj = new Object[] { stu.getSname(), stu.getSsex(),new java.sql.Date(stu.getSbrith().getTime()) }; jdbctemp.update(sql, obj);// 可以传两个参数,第一个参数是SQL语句,第二个参数是SQL语句的参数值 }
---- BeanPropertyRowMapper使用注意事项 - chinaifne - 博客园
https://www.cnblogs.com/chinafine/articles/1822109.html

浙公网安备 33010602011771号