<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>2.1.8.RELEASE</version>
<dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-batch-test</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
">
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="validateTransactionState" value="false" />
</bean>
<!--一个job-->
<batch:job id="writerteacherInterview">
<batch:step id="teacherInterview">
<batch:tasklet>
<batch:chunk reader="jdbcItemReaderTeacherInterview" writer="teacherInterviewItemWriter"
processor="teacherInterviewProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<!--job的读取数据操作-->
<bean id="jdbcItemReaderTeacherInterview"
class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql"
value="select distinct teacherName ,count(teacherName) as num from examininterviewrecord where pdate >'${detail_startime}' and pdate < '${detail_endtime}' GROUP BY teacherName " />
<property name="rowMapper" ref="teacherInterviewMapper">
</property>
</bean>
</beans>
package com.yc.batch;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import com.yc.vo.TeacherInterviewdetail;
import com.yc.vo.TeacherWorkdetail;
import com.yc.vo.Workdetail;
@Component("teacherInterviewMapper")
public class TeacherInterviewMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
TeacherInterviewdetail TId=new TeacherInterviewdetail();
TId.setTeacherName(rs.getString("teacherName"));
TId.setNum(rs.getInt("num"));
return TId;
}
}
package com.yc.batch;
import org.hibernate.engine.transaction.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.yc.vo.TeacherInterviewdetail;
import com.yc.vo.TeacherWorkdetail;
import com.yc.vo.Workdetail;
//业务层
@Component("teacherInterviewProcessor")
public class TeacherInterviewProcessor implements ItemProcessor<TeacherInterviewdetail, TeacherInterviewdetail> {
@Override
public TeacherInterviewdetail process(TeacherInterviewdetail teacherInterviewdetail) throws Exception {
return teacherInterviewdetail;
}
}
package com.yc.batch;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.annotation.Resource;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.yc.biz.ExamineeClassBiz;
import com.yc.biz.WorkBiz;
import com.yc.utils.CsvUtils;
import com.yc.vo.TeacherInterviewdetail;
import com.yc.vo.TeacherWorkdetail;
import com.yc.vo.Workdetail;
import net.sf.ehcache.util.PropertyUtil;
//写
@Component("teacherInterviewItemWriter")
public class TeacherInterviewItemWriter implements ItemWriter<TeacherInterviewdetail>{
@Override
public void write(List<? extends TeacherInterviewdetail> teacherInterviewdetails) throws Exception {
Properties props = new Properties();
InputStream in= PropertyUtil.class.getClassLoader().getResourceAsStream("connectionConfig.properties");
props.load(in);
String time=props.getProperty("detail_time");
CsvUtils cu=new CsvUtils();
List<Object> works=new ArrayList<Object>();
for(TeacherInterviewdetail t:teacherInterviewdetails){
works.add(t);
}
String path=this.getClass().getResource("/").getPath();
path=path.substring(0,path.lastIndexOf("/"));
path=path.substring(0,path.lastIndexOf("/"));
path=path.substring(0,path.lastIndexOf("/"));
path=path.substring(0,path.lastIndexOf("/"));
cu.writeCsv(path+"/csv/teacherInterview_"+time+".csv",works );
}
}
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
/**
* 写入CSV文件
* @throws IOException
*/
public void writeCsv(String path,List<Object> t) throws IOException{
String csvFilePath = path;
String filepath=path.substring(0,path.lastIndexOf("/"));
File f=new File(filepath);
if(!f.exists()){
f.mkdirs();
}
File file=new File(path);
if(!file.exists()){
file.createNewFile();
}
CsvWriter wr =new CsvWriter(csvFilePath,',',Charset.forName("GBK"));
try {
for(Object obj:t){
String[] contents=obj.toString().split(",");
wr.writeRecord(contents);
}
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
package com.yc.task.impl;
import javax.transaction.Transactional;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.yc.batch.ClassBatch;
import com.yc.batch.MessageItemBatch;
import com.yc.batch.TeacherInterviewBatch;
import com.yc.batch.TearcherBatch;
import com.yc.po.Work;
import com.yc.task.WorkTask;
import com.yc.vo.Workdetail;
@Service
public class WorkTaskImpl implements WorkTask{
@Autowired
private TeacherInterviewBatch teacherInterviewBatch;//教师访谈记录
public void setTeacherInterviewBatch(TeacherInterviewBatch teacherInterviewBatch) {
this.teacherInterviewBatch = teacherInterviewBatch;
}
@Scheduled(cron= "0 30 22 * * ?") //每天晚上十点30执行一次 这个注解会让框架会自动把这个方法看成任务启动方法
@Override
public void task() {
try {
teacherInterviewBatch.test();//教师访谈
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.yc.batch;
import javax.annotation.Resource;
import org.apache.commons.jexl2.Main;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TeacherInterviewBatch {
private Job job;
private JobLauncher launcher;
@Resource(name="writerteacherInterview")
public void setJob(Job job) {
this.job = job;
}
@Autowired
public void setLauncher(JobLauncher launcher) {
this.launcher = launcher;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有