springBoot 添加定时器和事务

定时器使用

一、启动类上添加注解

二、在配置文件中加上规则( cron的表达式自行百度)

三、在service层中添加任务使用注解:

@Scheduled(cron = "${krystal.quarz.order.cron}")

事务使用:

一、pom.xml中添加依赖

<dependencies>

<dependency> <!--添加Web依赖 -->

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency> <!--添加Mybatis依赖 -->

     <groupId>org.mybatis.spring.boot</groupId>

     <artifactId>mybatis-spring-boot-starter</artifactId>

     <version>1.3.1</version>

</dependency>

<dependency><!--添加MySQL驱动依赖 -->

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <scope>runtime</scope> </dependency>

<dependency><!--添加Test依赖 -->

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-test</artifactId>

  <scope>test</scope>

  </dependency>

</dependencies>

二、在启动类上添加注解:

@EnableTransactionManagement //如果mybatis中service实现类中加入事务注解,需要此处添加该注解

三、实现:在service中使用

注意点:

 1.( @Scheduled 和@Transactional不能同时存在一个类里面,会报错,我遇到的,不知道为啥)

 2.Spring的默认的事务规则是遇到运行异常(RuntimeException)和程序错误(Error)才会回滚。如果想针对非检测异常进行事务回滚,可以在@Transactional 注解里使用,@Transactional(rollbackFor=Exception.class) 属性明确指定异常。()

猜你喜欢

转载自blog.csdn.net/sinat_34338162/article/details/83576422