它是spring框架中提供的一个对象,是对原始繁琐的jdbc API对象的简单封装。
spring框架为我们提供了很多的操作模板类。
例如:操作关系数据的jdbcTemplate和HibermateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等
我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbaTemplate模板对象中,配置如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_1"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
@Test
public void testDelete(){
int row = jdbcTemplate.update("delete from person where id=?",103);
System.out.println(row);
}
<!-- 配置目标对象-->
<bean id="target" class="com.DemoTestNode.aop.Target"></bean>
<!-- 配置切面对象-->
<bean id="aspect" class="com.DemoTestNode.aop.MyAspect"></bean>
<!-- 配置织入;告诉Spring【哪些方法】需要【哪些增强】-->
<aop:config>
<aop:aspect ref="aspect">
<aop:before method="before" pointcut="execution(public void com.DemoTestNode.aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号* 代表任意
包名与类名之间一个点"." 代表当前包下的类,两个类。"…"表示当前包及其子包下的类
参数列表可以使用两个带点"…"表示任意个数,任意类型的参数列表
例如:
execution(public void com.demotestnode.aop.target.method())
execution(void com.demotestnode.aop.target.*(..))
execution(* com.demotestnode.aop.*.*(..))
execution(* com.demotestnode.aop..*.*(..))
execution(* *..*.*(..))
<aop:config>
<aop:aspect ref="aspect">
<aop:pointcut id="myPointcut" expression="execution(* com.DemoTestNode.aop.*.*(..))"/>
<aop:after method="final_funtion" pointcut-ref="myPointcut"></aop:after>
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect ref="切面类(bean)">
<aop:before method="”通知方法名称" pointcut="切点表达式"></aop:before>
</aop:aspect>
</aop:config>
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
<context:component-scan base-package="com.DemoTestNode.annon"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 开启 AOP 自动代理 -->
名称: | 标签 | 说明 |
---|---|---|
切入点 | @Pointcut | 该注解配置该方法为一个切入点方法。指定增强方法执行 |
名称: | 标签 | 说明 |
---|---|---|
前置通知: | @Before | 用于前置通知。指定增强的方法在切入点方法之前执行 |
后置通知: | @AfterReturning | 用于配置后置通知。指定增强的方法在切入点方法知州执行 |
环绕通知: | @Around | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |
异常抛出通知: | @AfterThrowing | 用于配置异常抛出童子。指定增强的方法在出现异常时执行 |
最终通知: | @After | 用于配置最终通知。无论增强方式执行是否有异常都会执行 |
名称: | 标签 | 说明 |
---|---|---|
前置通知: | @Before | 用于前置通知。指定增强的方法在切入点方法之前执行 |
后置通知: | @AfterReturning | 用于配置后置通知。指定增强的方法在切入点方法知州执行 |
环绕通知: | @Around | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |
异常抛出通知: | @AfterThrowing | 用于配置异常抛出童子。指定增强的方法在出现异常时执行 |
最终通知: | @After | 用于配置最终通知。无论增强方式执行是否有异常都会执行 |
PlatfromTransactionManager 接口是Spring的事务管理器,它里面提供了我们常用的操作事务的方法
方法 | 说明 |
---|---|
TransactionStatus | 获取事务的状态信息 |
void commit(TransactionStatus status) | 提交事务 |
void rollaback(TransactionStatus status) | 回滚事务 |
TransactionDefinition是事务的定义信息对象,里面右如下方法:
方法 | 说明 |
---|---|
int getIsolationLevel() | 获得事务的隔离级别 |
int getPropogationBehavior() | 获得事务的传播级别 |
int getTimeout() | 获得超时时间 |
boolean isReadOnly() | 是否只读 |
设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读
TransactionStatus 接口提供的是事务具体的运行状态,方法介绍如下:
方法 | 说明 |
---|---|
boolean hasSavepoint() | 是否存储回滚点 |
boolean isCompleted() | 事务是否完成 |
boolean isNewTransaction() | 是否是新事务 |
boolean isRollbackOnly() | 事务是否回滚 |
声明式事务的控制明确事项:
<tx:advice id="txAdvice" transaction-manager="事务管理器的beanID">
<tx:attributes>
<tx:method name="方法名" timeout="-1" isolation="DEFAULT" read-only="false" />
<tx:method name="*代表所有方法"/>
</tx:attributes>
</tx:advice>
其中,< tx:method > 代表切点方法的事务参数的配置,例如:
<tx:method name="方法名" timeout="-1" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />