spring 事务
首先建两个实体类:
Account 持股票
Stock 股票
实现这两个接口
public class StockImpl extends JdbcDaoSupport implements StockDao { public int addstock(Stock stock) { String sql="insert into stock(sname,counts) values(?,?)"; int stouccount = getJdbcTemplate().update(sql, stock.getStockname(), stock.getStockmoney()); return stouccount; } public int updatestock(int sid, int stockmoney, boolean isMoreOrLess) { String sql=""; if(isMoreOrLess){ //ture 销售股票 sql="update stock set counts=counts-? where sid=?"; }else { sql="update stock set counts=counts+? where sid=?"; } int count = getJdbcTemplate().update(sql,stockmoney,sid); return count; } public void setJdbcTemplate2(JdbcTemplate jdbcTemplate) { super.setJdbcTemplate(jdbcTemplate); } }
public class AccountImpl extends JdbcDaoSupport implements AccountDao { public int Addcount(Account account) { String sql="insert into account(aname,balance) values(?,?)"; int count = getJdbcTemplate().update(sql, account.getAccountname(), account.getAccountyu()); return count; } public int updateAccount(int add, int accountyu, boolean isMoreOrLess) { String sql=""; if(isMoreOrLess){ //余额增加 销售股票 sql="update account set balance=balance+? where aid=?"; }else{ sql="update account set balance=balance-? where aid=?"; } int count=getJdbcTemplate().update(sql,accountyu,add); return count; } public void setJdbcTemplate2(JdbcTemplate jdbcTemplate) { super.setJdbcTemplate(jdbcTemplate); } }
service接口
public interface IAccountService { public int Addcount(Account account); public int addstock(Stock stock); public int update(int aid,int money,int sid,int stockmoney); }
实现service
public class StockImpl extends JdbcDaoSupport implements StockDao { public int addstock(Stock stock) { String sql="insert into stock(sname,counts) values(?,?)"; int stouccount = getJdbcTemplate().update(sql, stock.getStockname(), stock.getStockmoney()); return stouccount; } public int updatestock(int sid, int stockmoney, boolean isMoreOrLess) { String sql=""; if(isMoreOrLess){ //ture 销售股票 sql="update stock set counts=counts-? where sid=?"; }else { sql="update stock set counts=counts+? where sid=?"; } int count = getJdbcTemplate().update(sql,stockmoney,sid); return count; } public void setJdbcTemplate2(JdbcTemplate jdbcTemplate) { super.setJdbcTemplate(jdbcTemplate); } }
public class AccountImpl extends JdbcDaoSupport implements AccountDao { public int Addcount(Account account) { String sql="insert into account(aname,balance) values(?,?)"; int count = getJdbcTemplate().update(sql, account.getAccountname(), account.getAccountyu()); return count; } public int updateAccount(int add, int accountyu, boolean isMoreOrLess) { String sql=""; if(isMoreOrLess){ //余额增加 销售股票 sql="update account set balance=balance+? where aid=?"; }else{ sql="update account set balance=balance-? where aid=?"; } int count=getJdbcTemplate().update(sql,accountyu,add); return count; } public void setJdbcTemplate2(JdbcTemplate jdbcTemplate) { super.setJdbcTemplate(jdbcTemplate); } }