lombok插件 XML
<!-- 加载资源文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 注入数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${msg1}"></property> <property name="jdbcUrl" value="${msg2}"></property> <property name="user" value="${msg3}"></property> <property name="password" value="${msg4}"></property> </bean> <!-- 注入QueryRunner --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg name="ds" ref="dataSource"></constructor-arg> </bean> <!-- 连接工具类 --> <bean id="connectionUtil" class="com.xn.util.ConnectionUtil"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事务工具类 --> <bean id="transactionUtil" class="com.xn.util.TransactionUtil"> <property name="connectionUtil" ref="connectionUtil"/> </bean> <!-- 注入dao --> <bean id="mapperImp" class="com.xn.dao.AccountMapperImp"> <property name="queryRunner" ref="queryRunner"></property> <property name="connectionUtil" ref="connectionUtil"></property> </bean> <!-- 注入service --> <bean id="service" class="com.xn.service.AccountServiceImp"> <property name="mapper" ref="mapperImp"/> <property name="transactionUtil" ref="transactionUtil"></property> </bean> <!-- 注入controller --> <bean id="controller" class="com.xn.controller.AccountControllerImp"> <property name="service" ref="service"/> </bean>
功能:对实体类自动,动态生成getset,无参有参..... 步骤: 1.idea安装插件(只做一次) 2.添加坐标<!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> </dependencies>
3.编写注解 dbUtil-阿帕奇提供操作数据库的插件 核心类:QueryRunner .query() 查询 .update() 增删改//操作数据库的核心类 QueryRunner queryRunner; public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } @Override public void save(Account account) { try { queryRunner.update(connectionUtil.createCon(),"insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney()); } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override public void updateById(Account account) { try { queryRunner.update(connectionUtil.createCon(),"update account set aname=?,amoney=? where aid=?",account.getAname(),account.getAmoney(),account.getAid()); } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override public void deleteById(int id) { try { queryRunner.update(connectionUtil.createCon(),"delete from account where aid=?",id); } catch (SQLException throwables) { throwables.printStackTrace(); } }
<!-- 注入QueryRunner --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg name="ds" ref="dataSource"></constructor-arg> </bean>
junit测试 使用步骤: 1.坐标单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
2.注解(修饰方法) @Test======>可以运行的方法 @Before====>@Test运行之前 @After=====>@Test运行之后public class Test01 { ClassPathXmlApplicationContext applicationContext=null; IAccountController controller=null; @Before public void beforeMethod(){ applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); controller = (IAccountController) applicationContext.getBean("controller"); } @After public void afterMethod(){ applicationContext.close(); } @Test public void show1(){ controller.save(new Account("林航宇",2000)); controller.save(new Account("杨文琪",2000)); } @Test public void show2(){ List<Account> all = controller.findAll(); for (int i = 0; i < all.size(); i++) { Account account = all.get(i); System.out.println(account); } } }
注解
<!-- 加载资源文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 注入数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${msg1}"></property> <property name="jdbcUrl" value="${msg2}"></property> <property name="user" value="${msg3}"></property> <property name="password" value="${msg4}"></property> </bean> <!-- 注入QueryRunner --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg name="ds" ref="dataSource"></constructor-arg> </bean> <context:component-scan base-package="com.xn"></context:component-scan> </beans>
配置工具
项目总结: 1.事务管理应该由service层进行实现 代码优化: 目的:业务层进行事务管理public class AccountServiceImp implements IAccountService{ IAccountMapper mapper; public void setMapper(IAccountMapper mapper) { this.mapper = mapper; } //装配 TransactionUtil transactionUtil; public void setTransactionUtil(TransactionUtil transactionUtil) { this.transactionUtil = transactionUtil; } @Override public void transfer(String sourceName, String targetName, int money) { try { transactionUtil.beginTx(); //查询数据 Account sourceAccount=mapper.findByName(sourceName); Account targetAccount=mapper.findByName(targetName); //转账 sourceAccount.setAmoney(sourceAccount.getAmoney()-money); targetAccount.setAmoney(targetAccount.getAmoney()+money); //修改数据库 mapper.updateById(sourceAccount); // int a=10/0;//模拟异常 mapper.updateById(targetAccount); transactionUtil.commitTx(); } catch (Exception e) { e.printStackTrace(); transactionUtil.rollbackTx(); } finally { transactionUtil.closeTx(); } }
1.同一个业务方法的多个dao方法公用一个connection对象public class AccountMapperImp implements IAccountMapper{ //操作数据库的核心类 QueryRunner queryRunner; public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } //注入连接工具类 ConnectionUtil connectionUtil; public void setConnectionUtil(ConnectionUtil connectionUtil) { this.connectionUtil = connectionUtil; } @Override public void save(Account account) { try { queryRunner.update(connectionUtil.createCon(),"insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney()); } catch (SQLException throwables) { throwables.printStackTrace(); } }
2.ThreadLocal<!-- 事务工具类 --> <bean id="transactionUtil" class="com.xn.util.TransactionUtil"> <property name="connectionUtil" ref="connectionUtil"/> </bean> <!-- 注入dao --> <bean id="mapperImp" class="com.xn.dao.AccountMapperImp"> <property name="queryRunner" ref="queryRunner"></property> <property name="connectionUtil" ref="connectionUtil"></property> </bean> <!-- 注入service --> <bean id="service" class="com.xn.service.AccountServiceImp"> <property name="mapper" ref="mapperImp"/> <property name="transactionUtil" ref="transactionUtil"></property> </bean>
3.通过连接对象进行事务的统一管理public class ConnectionUtil { //装配数据源 DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } //线程区域对象 ThreadLocal<Connection> threadLocal=new ThreadLocal<Connection>(); //获取连接 public Connection createCon(){ try { //获取线程内的连接对象 Connection connection=threadLocal.get(); //判断 if(connection==null){ connection=dataSource.getConnection();//创建连接 threadLocal.set(connection);//保存 } return connection; } catch (SQLException throwables) { throwables.printStackTrace(); } return null; } //移除连接 public void removeCon(){ threadLocal.remove();//一处连接对象 } }
public class TransactionUtil { //注入连接工具类 ConnectionUtil connectionUtil; public void setConnectionUtil(ConnectionUtil connectionUtil) { this.connectionUtil = connectionUtil; } //开启事务 public void beginTx(){ try { connectionUtil.createCon().setAutoCommit(false); } catch (SQLException throwables) { throwables.printStackTrace(); } } //提交事务 public void commitTx(){ try { connectionUtil.createCon().commit(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //回滚事务 public void rollbackTx(){ try { connectionUtil.createCon().rollback(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //关闭事务 public void closeTx(){ try { connectionUtil.createCon().close(); connectionUtil.removeCon(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }