在上一篇对springbatch简单有了了解,接下来加入操作数据库操作
需要用到两张表:
需求:将这两个表的信息按照班级关联并输出为txt文件
1.创建项目引入依赖:
<dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>LATEST</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency>
2.编辑实体类,mapper等:
package com.zs.springbatch.model; import lombok.Data; import lombok.experimental.Accessors; import javax.persistence.*; import java.io.Serializable; /** * create by Mybatis-generator */ @Table(name = "batch_class") @Data @Accessors(chain = true) public class BatchClass implements Serializable { @Id @Column(name = "c_id") @GeneratedValue(generator = "JDBC") private Long cId; @Column(name = "c_name") private String cName; @Column(name = "c_teacher") private String cTeacher; private static final long serialVersionUID = 1L; }
package com.zs.springbatch.model; import lombok.Data; import lombok.experimental.Accessors; import javax.persistence.*; import java.io.Serializable; /** * create by Mybatis-generator */ @Table(name = "batch_student") @Data @Accessors(chain = true) public class BatchStudent implements Serializable { @Id @Column(name = "s_id") @GeneratedValue(generator = "JDBC") private Long sId; @Column(name = "s_name") private String sName; @Column(name = "s_age") private String sAge; @Column(name = "s_class") private String sClass; private static final long serialVersionUID = 1L; }
package com.zs.springbatch.mapper; import com.zs.springbatch.model.BatchClass; import tk.mybatis.mapper.common.Mapper; /** * Created by Mybatis Generator */ public interface BatchClassMapper extends Mapper<BatchClass> { }
package com.zs.springbatch.mapper; import com.zs.springbatch.model.BatchStudent; import tk.mybatis.mapper.common.Mapper; /** * Created by Mybatis Generator */ public interface BatchStudentMapper extends Mapper<BatchStudent> { }
mapper.xml就不写了,以上内容都是自动生成代码,详细可以看:https://github.com/Zs-xiazhi/springboot
手动编辑转存文件中的实体类型:
package com.zs.springbatch.model; import lombok.Data; import lombok.experimental.Accessors; /** * @Company lhfinance.com * @Author ZhaoShuai * @Date Create in 2019/12/11 **/ @Data @Accessors(chain = true) public class Student { private Long id; private String name; private String age; private String className; private String teacher; }
然后配置三要素:reader,process,writer:
package com.zs.springbatch.config; import com.zs.springbatch.model.BatchStudent; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.NonTransientResourceException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import tk.mybatis.mapper.common.Mapper; import javax.annotation.PostConstruct; import java.util.List; /** * @Company lhfinance.com * @Author ZhaoShuai * @Date Create in 2019/12/11 **/ @Component public class Reader implements ItemReader<BatchStudent> { @Autowired private Mapper<BatchStudent> studentMapper; private List<BatchStudent> students; @PostConstruct public void init() { students = studentMapper.selectAll(); } public BatchStudent read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { if (students.size() != 0) { return students.remove(0); } return null; } }
package com.zs.springbatch.config; import com.zs.springbatch.model.BatchClass; import com.zs.springbatch.model.BatchStudent; import com.zs.springbatch.model.Student; import org.springframework.batch.item.ItemProcessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import tk.mybatis.mapper.common.Mapper; /** * @Company lhfinance.com * @Author ZhaoShuai * @Date Create in 2019/12/11 **/ @Component public class Process implements ItemProcessor<BatchStudent, Student> { @Autowired private Mapper<BatchClass> classMapper; public Student process(BatchStudent batchStudent) throws Exception { Student student = new Student(); student.setId(batchStudent.getSId()) .setName(batchStudent.getSName()) .setAge(batchStudent.getSAge()); BatchClass batchClass = new BatchClass(); batchClass.setCId(Long.valueOf(batchStudent.getSClass())); batchClass = classMapper.selectOne(batchClass); student.setClassName(batchClass.getCName()) .setTeacher(batchClass.getCTeacher()); return student; } }
package com.zs.springbatch.config; import com.zs.springbatch.model.Student; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; import org.springframework.batch.item.file.transform.DelimitedLineAggregator; import org.springframework.batch.item.file.transform.ExtractorLineAggregator; import org.springframework.batch.item.file.transform.LineAggregator; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.File; /** * @Company lhfinance.com * @Author ZhaoShuai * @Date Create in 2019/12/11 **/ @Component public class Writer extends FlatFileItemWriter<Student> { private Resource resource = new FileSystemResource(new File("out.txt")); @PostConstruct public void init() { setResource(resource); setLineSeparator(" "); setLineAggregator((student)-> new StringBuffer() .append(student.getId() + "|") .append(student.getName() + "|") .append(student.getAge() + "|") .append(student.getClassName() + "|") .append(student.getTeacher()).toString() ); } }
配置spring配置文件:
<?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:batch="http://www.springframework.org/schema/batch" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd"> <context:component-scan base-package="com.zs.springbatch.config"/> <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="transactionManager" ref="transactionManager"/> </bean> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///batch"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zs.springbatch.mapper"/> </bean> <batch:job id="job" job-repository="jobRepository"> <batch:step id="step1" > <batch:tasklet> <batch:chunk writer="writer" reader="reader" processor="process" processor-transactional="true" commit-interval="2"/> </batch:tasklet> </batch:step> </batch:job> </beans>
import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; 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.context.support.ClassPathXmlApplicationContext; /** * @Company lhfinance.com * @Author ZhaoShuai * @Date Create in 2019/12/12 **/ public class TestDemo { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); Job job = context.getBean(Job.class); JobLauncher jobLauncher = context.getBean(JobLauncher.class); try { JobExecution run = jobLauncher.run(job, new JobParameters()); System.out.println(run); } catch (JobExecutionAlreadyRunningException e) { e.printStackTrace(); } catch (JobRestartException e) { e.printStackTrace(); } catch (JobInstanceAlreadyCompleteException e) { e.printStackTrace(); } catch (JobParametersInvalidException e) { e.printStackTrace(); } } }
运行测试:
可以看到当前文件夹主目录下多了一个out.txt,打开可以看到:
1|张三|18|计算机科学与技术|张涛 2|lisi|20|商务英语|王二 3|wanger|18|计算机科学与技术|张涛
这就是程序运行的结果,将student表和class表的数据信息都加载出来存入一个文件里,同样的还可以反过来,读取文件存入数据库,有兴趣可以自行尝试