参考博客:
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表达式详解、举例子:
https://www.cnblogs.com/javahr/p/8318728.html
cron表达式说明: