publicclassAccountServiceImplimplementsAccountService{privateAccountDaoaccountDao;privateTransactionManagertManager;publicvoidsettManager(TransactionManagertManager){this.tManager=tManager;}publicvoidsetAccountDao(AccountDaoaccountDao){this.accountDao=accountDao;}@OverridepublicList<Account>findAll(){try{//1.开启事务tManager.beginTransaction();//2.执行操作List<Account>accounts=accountDao.findAll();//3.提交事务tManager.commitTransaction();//4.返回结果returnaccounts;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@OverridepublicAccountfindById(Integerid){try{//1.开启事务tManager.beginTransaction();//2.执行操作AccountbyId=accountDao.findById(id);//3.提交事务tManager.commitTransaction();//4.返回结果returnbyId;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@OverridepublicvoidsaveAccount(Accountaccount){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.saveAccount(account);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicvoidupdateAccount(Accountaccount){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.updateAccount(account);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicvoiddeleteAccount(Integerid){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.deleteAccount(id);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicintfindCount(){try{//1.开启事务tManager.beginTransaction();//2.执行操作intcount=accountDao.findCount();//3.提交事务tManager.commitTransaction();//4.返回结果returncount;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@Overridepublicvoidtransfer(StringsourceName,StringtargetName,Floatmoney){try{tManager.beginTransaction();//1.根据名称查询转出账户、转入账户Accountsource=accountDao.findByName(sourceName);Accounttarget=accountDao.findByName(targetName);//2.转出账户减钱,转入账户加钱source.setMoney(source.getMoney()-money);target.setMoney(target.getMoney()+money);//3.更新转出账户、转入账户accountDao.updateAccount(source);//用来模拟问题// int i = 2 / 0;accountDao.updateAccount(target);tManager.commitTransaction();}catch(Exceptione){System.out.println("转账失败!开始回滚");tManager.rollbackTransaction();}finally{tManager.release();}}}
publicclassAccountDaoImplimplementsAccountDao{privateQueryRunnerrunner;privateConnectionUtilsconnectionUtils;publicvoidsetConnectionUtils(ConnectionUtilsconnectionUtils){this.connectionUtils=connectionUtils;}publicvoidsetRunner(QueryRunnerrunner){this.runner=runner;}@OverridepublicList<Account>findAll(){try{returnrunner.query(connectionUtils.getThreadConnection(),"select * from account",newBeanListHandler<Account>(Account.class));}catch(SQLExceptione){//因为sqlexception是编译时期异常,必须处理。把他作为运行时异常,就可以不用处理了。thrownewRuntimeException();}}@OverridepublicAccountfindById(Integerid){try{returnrunner.query(connectionUtils.getThreadConnection(),"select * from account where id=?",newBeanHandler<Account>(Account.class),id);}catch(SQLExceptione){thrownewRuntimeException();}}@OverridepublicvoidupdateAccount(Accountaccount){try{runner.update(connectionUtils.getThreadConnection(),"update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}catch(SQLExceptione){e.printStackTrace();}}@OverridepublicvoiddeleteAccount(Integerid){try{runner.update(connectionUtils.getThreadConnection(),"delete from account where id=?",id);}catch(SQLExceptione){e.printStackTrace();}}/**
* 重点:保存并返回id
* @param account
*/@OverridepublicvoidsaveAccount(Accountaccount){try{runner.update(connectionUtils.getThreadConnection(),"insert into account(name,money) values(?,?)",account.getName(),account.getMoney());//形参是对象时,实际传的是地址。改变形参会影响实参。account.setId(Integer.parseInt(runner.query("select last_insert_id() id",newMapHandler()).get("id").toString()));}catch(SQLExceptione){e.printStackTrace();}}/**
* 重点:聚合查询
* @return
*/@OverridepublicintfindCount(){try{// 法一// return Integer.parseInt(runner.query("select count(*) count from account", new MapHandler()).get("count").toString());// 法二returnInteger.parseInt(runner.query(connectionUtils.getThreadConnection(),"select count(*) from account",newScalarHandler<>(1)).toString());}catch(SQLExceptione){e.printStackTrace();thrownewRuntimeException();}}@OverridepublicAccountfindByName(Stringname){try{List<Account>list=runner.query(connectionUtils.getThreadConnection(),"select * from account where name=?",newBeanListHandler<Account>(Account.class),name);if(list==null||list.size()==0){returnnull;}if(list.size()>1){thrownewRuntimeException("结果集不唯一,数据有问题");}returnlist.get(0);}catch(SQLExceptione){e.printStackTrace();thrownewRuntimeException();}}}
publicclassAccountServiceImplimplementsAccountService{privateAccountDaoaccountDao;privateTransactionManagertManager;publicvoidsettManager(TransactionManagertManager){this.tManager=tManager;}publicvoidsetAccountDao(AccountDaoaccountDao){this.accountDao=accountDao;}@OverridepublicList<Account>findAll(){try{//1.开启事务tManager.beginTransaction();//2.执行操作List<Account>accounts=accountDao.findAll();//3.提交事务tManager.commitTransaction();//4.返回结果returnaccounts;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@OverridepublicAccountfindById(Integerid){try{//1.开启事务tManager.beginTransaction();//2.执行操作AccountbyId=accountDao.findById(id);//3.提交事务tManager.commitTransaction();//4.返回结果returnbyId;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@OverridepublicvoidsaveAccount(Accountaccount){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.saveAccount(account);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicvoidupdateAccount(Accountaccount){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.updateAccount(account);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicvoiddeleteAccount(Integerid){try{//1.开启事务tManager.beginTransaction();//2.执行操作accountDao.deleteAccount(id);//3.提交事务tManager.commitTransaction();}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();}finally{//6.释放连接tManager.release();}}@OverridepublicintfindCount(){try{//1.开启事务tManager.beginTransaction();//2.执行操作intcount=accountDao.findCount();//3.提交事务tManager.commitTransaction();//4.返回结果returncount;}catch(Exceptione){//5.回滚操作tManager.rollbackTransaction();thrownewRuntimeException(e);}finally{//6.释放连接tManager.release();}}@Overridepublicvoidtransfer(StringsourceName,StringtargetName,Floatmoney){try{tManager.beginTransaction();//1.根据名称查询转出账户、转入账户Accountsource=accountDao.findByName(sourceName);Accounttarget=accountDao.findByName(targetName);//2.转出账户减钱,转入账户加钱source.setMoney(source.getMoney()-money);target.setMoney(target.getMoney()+money);//3.更新转出账户、转入账户accountDao.updateAccount(source);//用来模拟问题// int i = 2 / 0;accountDao.updateAccount(target);tManager.commitTransaction();}catch(Exceptione){System.out.println("转账失败!开始回滚");tManager.rollbackTransaction();}finally{tManager.release();}}}
publicclassAccountServiceImplimplementsAccountService{@OverridepublicvoidsaveAccount(){// int i = 2 / 0;System.out.println("保存账户!");}@OverridepublicvoidupdateAccount(inti){System.out.println("更新账户!");}@OverridepublicintdeleteAccount(){System.out.println("删除账户!");return0;}}
@Component("accountDao")publicclassAccountDaoImplimplementsAccountDao{@AutowiredprivateQueryRunnerrunner;@AutowiredprivateConnectionUtilsconnectionUtils;@OverridepublicList<Account>findAll(){try{returnrunner.query(connectionUtils.getThreadConnection(),"select * from account",newBeanListHandler<Account>(Account.class));}catch(SQLExceptione){//因为sqlexception是编译时期异常,必须处理。把他作为运行时异常,就可以不用处理了。thrownewRuntimeException();}}@OverridepublicAccountfindById(Integerid){try{returnrunner.query(connectionUtils.getThreadConnection(),"select * from account where id=?",newBeanHandler<Account>(Account.class),id);}catch(SQLExceptione){thrownewRuntimeException();}}@OverridepublicvoidupdateAccount(Accountaccount){try{runner.update(connectionUtils.getThreadConnection(),"update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}catch(SQLExceptione){e.printStackTrace();}}@OverridepublicvoiddeleteAccount(Integerid){try{runner.update(connectionUtils.getThreadConnection(),"delete from account where id=?",id);}catch(SQLExceptione){e.printStackTrace();}}/**
* 重点:保存并返回id
* @param account
*/@OverridepublicvoidsaveAccount(Accountaccount){try{runner.update(connectionUtils.getThreadConnection(),"insert into account(name,money) values(?,?)",account.getName(),account.getMoney());//形参是对象时,实际传的是地址。改变形参会影响实参。account.setId(Integer.parseInt(runner.query("select last_insert_id() id",newMapHandler()).get("id").toString()));}catch(SQLExceptione){e.printStackTrace();}}/**
* 重点:聚合查询
* @return
*/@OverridepublicintfindCount(){try{// 法一// return Integer.parseInt(runner.query("select count(*) count from account", new MapHandler()).get("count").toString());// 法二returnInteger.parseInt(runner.query(connectionUtils.getThreadConnection(),"select count(*) from account",newScalarHandler<>(1)).toString());}catch(SQLExceptione){e.printStackTrace();thrownewRuntimeException();}}@OverridepublicAccountfindByName(Stringname){try{List<Account>list=runner.query(connectionUtils.getThreadConnection(),"select * from account where name=?",newBeanListHandler<Account>(Account.class),name);if(list==null||list.size()==0){returnnull;}if(list.size()>1){thrownewRuntimeException("结果集不唯一,数据有问题");}returnlist.get(0);}catch(SQLExceptione){e.printStackTrace();thrownewRuntimeException();}}}
@Component("accountService")publicclassAccountServiceImplimplementsAccountService{@AutowiredprivateAccountDaoaccountDao;@OverridepublicList<Account>findAll(){returnaccountDao.findAll();}@OverridepublicAccountfindById(Integerid){returnaccountDao.findById(id);}@OverridepublicvoidsaveAccount(Accountaccount){accountDao.saveAccount(account);}@OverridepublicvoidupdateAccount(Accountaccount){accountDao.updateAccount(account);}@OverridepublicvoiddeleteAccount(Integerid){accountDao.deleteAccount(id);}@OverridepublicintfindCount(){returnaccountDao.findCount();}@Overridepublicvoidtransfer(StringsourceName,StringtargetName,Floatmoney){//1.根据名称查询转出账户、转入账户Accountsource=accountDao.findByName(sourceName);Accounttarget=accountDao.findByName(targetName);//2.转出账户减钱,转入账户加钱source.setMoney(source.getMoney()-money);target.setMoney(target.getMoney()+money);//3.更新转出账户、转入账户accountDao.updateAccount(source);//用来模拟问题// int i = 2 / 0;accountDao.updateAccount(target);}}