1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package springjdbc; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper;
import java.util.List;
public class AccountDaoImpl implements AccountDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate=jdbcTemplate; }
@Override public int addAccount(Account account) { String sql="insert into account(username,balance) value(?,?)"; Object[] obj=new Object[] { account.getUsername(), account.getBalance() }; int num=this.jdbcTemplate.update(sql,obj); return num; }
@Override public int updateAccount(Account account) { String sql="update account set username=?,balance=? where id =?"; Object[] params=new Object[] { account.getUsername(), account.getBalance(), account.getId() }; int num=this.jdbcTemplate.update(sql,params); return num; }
@Override public int deleteAccount(int id) { String sql="delete from account where id=?"; int num=jdbcTemplate.update(sql,id); return num; }
@Override public Account findAccountById(int id) { String sql="select * from account where id=?"; RowMapper<Account> rowMapper=new BeanPropertyRowMapper<Account>(Account.class); return this.jdbcTemplate.queryForObject(sql,rowMapper,id); }
@Override public List<Account> findAllAcount() { String sql="select * from account"; RowMapper<Account> rowMapper=new BeanPropertyRowMapper<Account>(Account.class); return this.jdbcTemplate.query(sql,rowMapper); }
@Override public void transfer(String outUser,String inUser,Double money) { this.jdbcTemplate.update("update account set balance = balance +? where username= ?",money,inUser); int i=1/0; this.jdbcTemplate.update("update account set balance = balance-? where username= ?",money,outUser); } }
|