步驟,如圖所示java
1.添加定時任務1業務類web
package top.ytheng.demo.task; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; //定時任務業務類 @Component public class TestTask { //兩秒執行一次 @Scheduled(fixedRate=2000) public void sum() { System.out.println("當前時間:" + new Date()); } }
2.添加啓動類spring
package top.ytheng.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //等於下面3個 //@SpringBootConfiguration //@EnableAutoConfiguration //@ComponentScan //攔截器用到 @ServletComponentScan //MyBatis用到 @MapperScan("top.ytheng.demo.mapper") //定時使用(開啓定時任務) @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3.右鍵項目Run As啓動,查看打印日誌便可mybatis