Spring中的 JdbcTemplate和声明式事务控制
JdbcTemplate概述
![持久层](https://img-blog.csdnimg.cn/a6fe0a91536e4a85ba832187e01fe188.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzY1MjE0Ng==,size_16,color_FFFFFF,t_70#pic_center)
-
JdbcTemplate的作用:他就是用于和数据库交互的,实现CRUD操作
-
如何创建该对象
- 在dao的实现类中定义并用set方法注入
- **JdbcDaoSupport ** 类的 getJdbcTemplate() 方法
-
对象中的常用方法
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
jt.update("insert into account(name,money) VALUES (?,?)","fff",3000);
jt.update("update account set name=?,money=? where id=?","ll",6000,8);
jt.update("delete from account where id=?",9);
List<Account> accounts = jt.query("select * from account where money > ?",
new BeanPropertyRowMapper<Account>(Account.class),1000f);
for (Account a:accounts) {
System.out.println(a);
}
List<Account> account = jt.query("select * from account where id = ?",
new BeanPropertyRowMapper<>(Account.class),1);
System.out.println(account.isEmpty()?"没有内容":accounts.get(0));
Long count = jt.queryForObject("select count(*) from account where money > ?",
Long.class,1000f);
System.out.println(count);
spring中的事务控制
-
基于XML的
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.ssm.service.impl.AccountServiceImpl.*(..))"></aop:pointcut>
<aop:aspect id="txAdvice" ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pt"></aop:before>
<aop:after-returning method="commit" pointcut-ref="pt"></aop:after-returning>
<aop:after-throwing method="rollback" pointcut-ref="pt"></aop:after-throwing>
<aop:after method="release" pointcut-ref="pt"></aop:after>
</aop:aspect>
</aop:config>
-
基于注解的
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-
使用前置通知、后置通知、异常通知、最终通知注解会出错,执行顺序是前置–>最终–>后置/异常
-
建议使用环绕通知
@Around("pt()")
public Object aroundAdvice(ProceedingJoinPoint pjp){
Object rtValue;
try{
Object[] args = pjp.getArgs();
this.beginTransaction();
rtValue = pjp.proceed(args);
this.commit();
return rtValue;
}catch (Throwable t){
this.rollback();
throw new RuntimeException(t);
}finally {
this.release();
}
}
spring中声明式事务的控制
-
基于XML的
- 配置事务管理器
- 配置事务的通知
- 配置AOP中的通用切入点表达式
- 建立切入点表达式和事务通知的对应关系
- 配置事务的属性
- isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
- no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,表示任何异常都回滚。
- propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
- read-only:用于指定事务是否只读,只有查询方法才能设置为true,默认值是false,表示读写。
- rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值,表示任何异常都回滚。
- timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
-
基于注解的
-
配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
-
开启spring对注解事务的支持
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
@EnableTransactionManagement
-
在需要事务支持的地方使用@Transactional()注解
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)