当前位置: 首页 > news >正文

SpringIOC和SpringAOC

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;}@Overridepublic void save(Account account) {try {queryRunner.update(connectionUtil.createCon(),"insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney());} catch (SQLException throwables) {throwables.printStackTrace();}}@Overridepublic 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();}}@Overridepublic 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;@Beforepublic void beforeMethod(){applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");controller = (IAccountController) applicationContext.getBean("controller");}@Afterpublic void afterMethod(){applicationContext.close();}@Testpublic void show1(){controller.save(new Account("林航宇",2000));controller.save(new Account("杨文琪",2000));}@Testpublic 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;}@Overridepublic 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;}@Overridepublic 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();}}}

http://www.lryc.cn/news/420338.html

相关文章:

  • static关键字详解
  • 使用 Java RestClient 与 Elasticsearch 进行索引管理的示例
  • 编程-设计模式 10:外观模式
  • 非范型ArrayList和泛型List<T>
  • 魔众文库系统v7.0.0版本推荐店铺功能,管理菜单逻辑优化
  • 03、流程控制语句
  • [Android] [解决]Bottom Navigation Views Activity工程带来的fragment底部遮盖的问题
  • Armv8/Armv9架构中的原子性
  • 读零信任网络:在不可信网络中构建安全系统15协议和过滤
  • C语言学习笔记 Day11(指针--中2)
  • Golang 并发编程
  • 【数据结构详解】——选择排序(动图详解)
  • 杂项命令(笔记)
  • 代码随想录算法训练营Day38||完全背包问题、leetcode 518. 零钱兑换 II 、 377. 组合总和 Ⅳ 、70. 爬楼梯 (进阶)
  • 超越链端:Web3的无边界技术革命
  • 127. Go反射基本原理
  • 提高PDF电子书的分辨率
  • Spring Cloud全解析:注册中心之zookeeper注册中心
  • 解决戴尔台式电脑休眠后无法唤醒问题
  • MySQL运维-分库分表
  • AGX orin硬件设计
  • AI大模型开发——2.深度学习基础(1)
  • go语言day22 gin-vue-admin全栈项目的依赖安装
  • PHP之docker学习笔记
  • 基于树莓派4B与STM32的UART串口通信实验(代码开源)
  • 【云服务器系列】基于华为云OBS实现Picgo和Typora的完美融合
  • IIC协议
  • 如何在linux系统上部署nginx
  • 香港网站服务器抵御恶意攻击的一些措施
  • 实战:docker部署filesite.io完美解决家庭相册需求-2024.8.10(测试成功)