Sping学习四——JdbcTemplate与事务控制
目录一、Spring中的JdbcTemplate二、作业分析1)基于XML的AOP配置2)基于注解的AOP实现事务控制三、Spring的声明式事务控制1、spring中事务控制的一组API2、spring中基于xml的声明式事务控制----配置步骤3.spring中基于注解的声明式事务控制day4.....一、Spring中的JdbcTemplate...
目录
2、spring中基于xml的声明式事务控制----配置步骤
day4.....
一、Spring中的JdbcTemplate
1、作用:用于和数据库进行交互,实现对表的CURD操作
2、如何创建该对象
3、对象中常用的方法
*创建表的实体类 t_account表
*jdbcTemlpate包下创建JdbcTemplate类
使用spring的IOC来配置,简化上述代码
<!--配置JdbcTemplate数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_spring"/>
<property name="username" value="456789"/>
<property name="password" value="132456"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
增删改:update方法;
查询所有:query方法
查询一个和查询所有使用相同的方法;
//准备数据源,spring内置的数据源
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);
//给jt设置数据源
// jt.setDataSource(ds);
//执行操作
//增
// jt.update("insert into t_account(aname,money ) values (?,?)","eee",1000f);
// jt.execute("insert into t_account(aname,money ) values ('fff',2000)");
//改
// jt.update("update t_account set aname = ?,money = ? where id = ?","fff",5000f,7);
//删
int rt = jt.update("delete from t_account where id = ?",6);
if(rt >0){
System.out.println("删除成功!");
}
//查
List<Account> accountList = jt.query("select * from t_account",new BeanPropertyRowMapper<Account>(Account.class));
for (Account account:
accountList) {
System.out.println(account);
}
//查询单行单列(使用聚合函数,但不加group by 子句)
Long num = jt.queryForObject("select count(*) from t_account where money > ?",Long.class,10000f);
System.out.println(num);
}
JdbcTemplate在Dao中的使用:
接口实现类:jdbcTemplate变量,及其set方法(用于配置bean)
JdbcDaoSupport的使用:继承(不易使用注解配置)或者不继承(使用xml配置)
二、作业分析
1)基于XML的AOP配置
<!--配置aop实现对事务的控制和管理-->
<aop:config>
<!--配置切点表达式-->
<aop:pointcut id="ptcut" expression="execution(* com.dzg.service.impl.*.*(..))"/>
<!--配置切面 -> 管理事务,要和事务管理器相关联-->
<aop:aspect id="txAdvice" ref="transactionManager">
<!--配置前置通知-->
<aop:before method="beginTransaction" pointcut-ref="ptcut"/>
<!--配置后置通知-->
<aop:after-returning method="commit" pointcut-ref="ptcut"/>
<!--配置异常通知-->
<aop:after-throwing method="rollback" pointcut-ref="ptcut"/>
<!--配置最终通知-->
<aop:after method="release" pointcut-ref="ptcut"/>
</aop:aspect>
</aop:config>
2)基于注解的AOP实现事务控制
在不同层设置注解,将xml文件中对应的配置删除。。。
context
前四种通知存在顺序问题,解决的方法就是使用环绕通知
三、Spring的声明式事务控制
1、spring中事务控制的一组API
隔离级别:
2、spring中基于xml的声明式事务控制----配置步骤
<!--step1:配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--step2:配置事务的通知-->
<tx:advice id="txAdvvice" transaction-manager="transactionManager">
<!--step5:配置事务的属性 -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<!--step3:配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution(* com.dzg.service.impl.*.*(..))"/>
<!--step4:建立切入点表达式和事务通知的对应关系-->
<aop:advisor advice-ref="txAdvvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
3.spring中基于注解的声明式事务控制
注解:context
service层: @Service()
dao层:@Repository()
基于注解声明式事务控制的步骤:
新建一个工程:annotation-without-xml
把上一个注解的内容拷贝进来
新建一个包 config
SpringConfiguration
变量加上配置@Value
数据配置项文件properties,加入注解:
开启注解的支持:
事务管理器:
测试类:
4、Spring编程式事务控制(了解)
更多推荐
所有评论(0)