增,删,改:update
int rows = template.update("insert into account values(?,?)", "jack", 300);
查:query(结果为list集合)
List<Account> accountList = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
queryForObject(结果为 对象)
Account account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class));
queryForObject(结果为数字)
Integer integer = template.queryForObject("select count(*) from account", Integer.class);
管理+定义 = 状态
实现根据不同的dao层技术来实现
①、TransactionStatus getTransaction(TransactionDefinition definition) ,事务管理器 通过TransactionDefinition,获得“事务状 态”,从而管理事务。
②、void commit(TransactionStatus status) 根据状态提交
③、void rollback(TransactionStatus status) 根据状态回滚
Spring中的7个事务传播行为:
—- | 如果A调用B 假设A.., 则…. |
---|---|
PROPAGATION_REQUIRED | 支持当前事务,假设当前没有事务。就新建一个事务 |
PROPAGATION_SUPPORTS | 支持当前事务,假设当前没有事务,就以非事务方式运行 |
PROPAGATION_MANDATORY | 支持当前事务,假设当前没有事务,就抛出异常 |
PROPAGATION_REQUIRES_NEW | 新建事务,假设当前存在事务。把当前事务挂起 |
PROPAGATION_NOT_SUPPORTED | 以非事务方式运行操作。假设当前存在事务,就把当前事务挂起 |
PROPAGATION_NEVER | 以非事务方式运行,假设当前存在事务,则抛出异常 |
PROPAGATION_NESTED | 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。 |
超时时间 | |
是否只读 |
名称 | 说明 |
---|---|
void flush() | 刷新事务 |
boolean hasSavepoint() | 获取是否存在保存点 |
boolean isCompleted() | 获取事务是否完成 |
boolean isNewTransaction() | 获取是否是新事务 |
boolean isRollbackOnly() | 获取是否回滚 |
void setRollbackOnly() | 设置事务回滚 |
业务代码和事务控制通过 事务配置 的方式进行松耦合
采用AOP的思想:切点(业务)
通知(事务控制)
将业务进行增强,从而达成事务配置,并且松耦合
<!-- 配置织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* Service.Impl.*.*(..))"></aop:advisor>
</aop:config>
注意:
<tx:annotation-driven/>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115199.html原文链接:https://javaforall.cn