【工程截图】
【PersonDao.java】
package com.HigginCui.daoProxy; public interface PersonDao { public void savePerson(); }
【PersonDaoImpl.java】
package com.HigginCui.daoProxy; public class PersonDaoImpl implements PersonDao{ @Override public void savePerson() { System.out.println("save Person..."); } }
【Transaction.java】
package com.HigginCui.daoProxy; public class Transaction { public void beginTransaction(){ System.out.println("begin transaction..."); } public void commit(){ System.out.println("begin commit..."); } }
【PersonDaoProxy.java】
package com.HigginCui.daoProxy; public class PersonDaoProxy implements PersonDao{ private PersonDao personDao; private Transaction transaction; public PersonDaoProxy(PersonDao personDao,Transaction transaction){ this.personDao=personDao; this.transaction=transaction; } @Override public void savePerson() { //1.开启事务 this.transaction.beginTransaction(); //2.进行save操作 this.personDao.savePerson(); //3.事务提交 this.transaction.commit(); } }
【testPerson.java】
package com.HigginCui.test; import org.junit.Test; import com.HigginCui.daoProxy.PersonDao; import com.HigginCui.daoProxy.PersonDaoImpl; import com.HigginCui.daoProxy.PersonDaoProxy; import com.HigginCui.daoProxy.Transaction; public class testPerson { @Test public void test(){ PersonDao personDao=new PersonDaoImpl(); Transaction transaction=new Transaction(); PersonDaoProxy personDaoProxy=new PersonDaoProxy(personDao,transaction); personDaoProxy.savePerson(); } }
【运行结果】
begin transaction...
save Person...
begin commit...