spring的定時任務配置(註解)

參考博客:

http://www.jb51.net/article/110541.htm

http://blog.csdn.net/wxwzy738/article/details/25158787

我這邊項目的需求是:每天晚上1點刪除數據庫表t_tempclob中的所有記錄;

 

代碼:

Controller:

複製代碼
@Controller
public class AjaxFileDownload {
    
    private static Logger logger = Logger.getLogger(AjaxFileDownload.class);
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    private TempClobService tcService;
    
    /**
     * 定時任務,每天晚上1點刪除數據表t_tempClob中的所有記錄
     */
    @Scheduled(cron= "0 0 1 * * ?")
    public void deleteAllTempClob(){
        int count = tcService.deleteAllTempClob();
        System.err.println("---->>deleteAllTempClob刪除數據庫記錄數:" + count);
    }
}
複製代碼

Service:

複製代碼
/**
     * 刪除所有大文本
     */
    public int deleteAllTempClob(){
        int count = 0;
        try {
            count = tcMapper.deleteAllTempClob();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return count;
    }
複製代碼

Mapper接口:

複製代碼
public interface TempClobMapper {

    
    /**
     * 刪除所有的大文本
     */
    public int deleteAllTempClob() throws Exception;
}
複製代碼

Mapper.xml:

複製代碼
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cy.dao.TempClobMapper" >

    
    <!-- 刪除所有的大文本 -->
    <delete id="deleteAllTempClob">
        delete from t_tempclob 
    </delete>
      
</mapper>
複製代碼

spring-mvc.xml中關於task的配置:

複製代碼
<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">  

<!-- 使用註解的包,包括子集 -->
    <context:component-scan base-package="com.cy.controller" />

 <!-- 定時器開關--> 
     <task:annotation-driven />
    
</beans>  
複製代碼

 

cron表達式說明:

參考博客:http://www.jb51.net/article/110541.htm

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章