定时器的编写

时间:2020-12-25 23:31:26
applicationContext-dao.xml的代码


<?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:context
="http://www.springframework.org/schema/context"
xmlns:jdbc
="http://www.springframework.org/schema/jdbc"
xmlns:jee
="http://www.springframework.org/schema/jee"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:mvc
="http://www.springframework.org/schema/mvc"
xmlns:util
="http://www.springframework.org/schema/util"
xmlns:jpa
="http://www.springframework.org/schema/data/jpa"
xmlns:task
="http://www.springframework.org/schema/task"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

<!--配置dbcp -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
</bean>


<!--配置 sqlsessionfactorybean和MapperScannerConfigurer-->
<!--把 sqlsessionfactorybean配置进容器 -->

<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcp"></property>
<property name="mapperLocations" value="classpath:com/dyy/mapper/*.xml"></property>
</bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dyy.XXX"></property>
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>

<context:component-scan base-package="com.dyy.timer"></context:component-scan>
<task:annotation-driven/>


</beans>

 

package com.dyy.timer;

import javax.annotation.Resource;

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dyy.dao.StudentDao;
import com.dyy.entity.Student;
import com.dyy.entity.Teacher;

@Component
@Lazy(value=false)
public class TestTimers {

@Resource
private StudentDao dao;

public StudentDao getDao() {
return dao;
}

public void setDao(StudentDao dao) {
this.dao = dao;
}

@Scheduled(cron="0/5 * * * * ? ") //间隔5秒执行
public void TaskJob() {
System.out.println("test second annotation style ...");

Student stu = new Student();
int id =1;
for (int i = 0; i < 10; i++) {
stu.setId(id);
id++;
int test = dao.InsertTest(stu);
System.out.println(test);//返回值
}
}
}

  

<!--新添加的一个jar ,不知道有没有用,可以试着添加一下 -->

<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>

基本一个定时器就写完了,具体的还需要在测试.......

敬请期待......