使用Springboot + mybatis框架
4.1.1 pom.xml
4.0.0
com.mlsama
batchPay
1.0.0-SNAPSHOT
jar
batchPay
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
1.3.2
org.springframework.boot
spring-boot-maven-plugin
org.springframework.boot
spring-boot-starter-batch
org.springframework
spring-context-support
org.quartz-scheduler
quartz
2.3.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.batch
spring-batch-test
test
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
org.slf4j
log4j-over-slf4j
com.mchange
c3p0
0.9.5.2
org.apache.commons
commons-lang3
3.5
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.version}
4.1.2 application.properties
server.port=8888
#关闭项目启动job自动执行
spring.batch.job.enabled = false
#整合c3p0,要自定义配置类,如:DataSourceConfiguration
spring.datasource.c3p0.driverClass=com.mysql.jdbc.Driver
spring.datasource.c3p0.jdbcUrl=jdbc:mysql://localhost:3306/springbatch
spring.datasource.c3p0.user=root
spring.datasource.c3p0.password=mlsama
spring.datasource.c3p0.maxPoolSize=30
spring.datasource.c3p0.minPoolSize=10
spring.datasource.c3p0.initialPoolSize=10
#mybatis配置
#别名包扫描
mybatis.typeAliasesPackage=com.mlsama.hellospringbatch.pojo
#引入*Mapper.xml文件
mybatis.mapperLocations=classpath:mappers/**/*Mapper.xml
# 引入mybatis主配置文件
mybatis.configLocation=classpath:mybatis-config.xml
4.1.3 整合c3p0,自定义数据源
@Configuration //声明为配置类
public class DataSourceConfiguration {
@Bean(name = "dataSource") //对象及名称
@Primary //主要的候选者
//配置属性,prefix : 前缀 spring.datasource固定
@ConfigurationProperties(prefix = "spring.datasource.c3p0")
public DataSource createDataSource(){
return DataSourceBuilder.create() // 创建数据源构建对象
.type(ComboPooledDataSource.class) // 设置数据源类型
.build(); // 构建数据源对象
}
}
4.1.4 mybatis-config.xml
4.1.5 job的配置
4.1.6 数据源(.csv文件)
86000041-20181008.csv
procCode,account,accountName,amount,payType,reserve
86000041,612565688853252536,df,203000,DS,转账
86000041,813565678853252533,ds,240000,DS,转账
86000041,615465688853252531,ed,203000,DS,转账
86000041,614565688853252532,de,200600,DS,转账
86000041,687565688853252534,er,200050,DS,转账
4.1.7 实体类
@Data
public class BatchPay {
/**
* 流水号
*/
private String logSeq;
/**
* 支付机构编码
*/
private String procCode;
/**
* 账号
*/
private String account;
/**
* 户名
*/
private String accountName;
/**
* 金额
*/
private BigDecimal amount;
/**
* 支付类型
*/
private String paySeq;
/**
* 支付类型
*/
private String payType;
/**
* 交易日期
*/
private String tradeDate;
/**
* 交易时间
*/
private String tradeTime;
/**
* 交易结果
*/
private String resultCode;
/**
* 交易返回码
*/
private String respCode;
/**
* 交易返回码描述
*/
private String respMsg;
/**
* 自定义域1
*/
private String reserve;
public BatchPay(String procCode, String account, String accountName, BigDecimal amount, String payType, String reserve) {
this.procCode = procCode;
this.account = account;
this.accountName = accountName;
this.amount = amount;
this.payType = payType;
this.reserve = reserve;
}
@Override
public String toString() {
return "BatchPay{" +
"logSeq='" + logSeq + '\'' +
", procCode='" + procCode + '\'' +
", account='" + account + '\'' +
", accountName='" + accountName + '\'' +
", amount=" + amount +
", payType='" + payType + '\'' +
", paySeq='" + paySeq + '\'' +
", tradeDate='" + tradeDate + '\'' +
", tradeTime='" + tradeTime + '\'' +
", resultCode='" + resultCode + '\'' +
", respCode='" + respCode + '\'' +
", respMsg='" + respMsg + '\'' +
", reserve1='" + reserve + '\'' +
'}';
}
}
4.1.8 读取数据源
使用FlatFileItemReader类,其实已经在job的配置文件中有了配置:
将行映射为对象的类需要自定义:
BatchPayReader.java
public class BatchPayReader implements FieldSetMapper{
@Override
public BatchPay mapFieldSet(FieldSet fieldSet) throws BindException {
return new BatchPay(
//fieldSet里有每一行的数据,从0开始
fieldSet.readString(0),
fieldSet.readString(1),
fieldSet.readString(2),
new BigDecimal(fieldSet.readString(3)),
fieldSet.readString(4),
fieldSet.readString(5)
);
}
}
4.1.9 业务处理
在配置文件中也有了定义
BatchPayItemProcessor.Java如下:
@Slf4j
public class BatchPayItemProcessor implements ItemProcessor{
@Override
public BatchPay process(BatchPay batchPay) throws Exception {
String logSeq = CommonUtil.getLogSeq();
batchPay.setLogSeq(logSeq);
batchPay.setPaySeq(logSeq);
batchPay.setTradeDate(new SimpleDateFormat(CommonConstants.DATA_FORMAT).format(new Date()));
batchPay.setTradeTime(new SimpleDateFormat(CommonConstants.Time_FORMAT).format(new Date()));
batchPay.setResultCode(CommonConstants.RESULT_CODE_0050);
return batchPay;
}
}
4.1.10 持久化
在配置文件中的配置如下:
BatchPayWriter.Java如下:
@Slf4j
public class BatchPayWriter implements ItemWriter {
@Autowired
private BatchPayManager batchPayManager;
@Override
public void write(List extends BatchPay> list) throws Exception {
log.info("开始持久化数据.list={}",list);
try {
batchPayManager.batchPayInsert(list);
}catch (Exception e){
log.error("持久化数据异常{}.",e);
}
}
}
4.1.11 统一管理层
@Service
public class BatchPayManager {
@Autowired
private BatchPayMapper batchPayMapper;
@Transactional(rollbackFor = Exception.class)
public void batchPayInsert(List extends BatchPay> list){
batchPayMapper.batchInsert(list);
}
}
4.1.12 mybatis的mapper层
@Mapper
public interface BatchPayMapper {
void batchInsert(List extends BatchPay> list);
}
4.1.13 mybatis的mapper配置
在resource下创建文件夹mappers,创建配置文件BatchPayMapper.xml
INSERT INTO t_batch_pay
(
log_seq,
proc_code,
account,
account_name,
amount,
pay_seq,
pay_type,
trade_date,
trade_time,
result_code,
resp_code,
resp_msg,
reserver
)VALUES
(
#{item.logSeq, jdbcType=VARCHAR},
#{item.procCode, jdbcType=VARCHAR},
#{item.account, jdbcType=VARCHAR},
#{item.accountName, jdbcType=VARCHAR},
#{item.amount, jdbcType=DECIMAL},
#{item.paySeq, jdbcType=VARCHAR},
#{item.payType, jdbcType=VARCHAR},
#{item.tradeDate, jdbcType=VARCHAR},
#{item.tradeTime, jdbcType=VARCHAR},
#{item.resultCode, jdbcType=VARCHAR},
#{item.respCode, jdbcType=VARCHAR},
#{item.respMsg, jdbcType=VARCHAR},
#{item.reserve, jdbcType=VARCHAR}
)
4.1.14 job调度层
用于启动job,通过配置在job配置文件中的SimpleJobLauncher启动job.
@Service
@Slf4j
public class BatchPayJobLauncher {
@Resource(name = "batchJobLauncher")
private SimpleJobLauncher batchJobLauncher;
@Resource(name = "batchPayJob")
private Job batchPayJob;
public String doBatchPayJob(){
try {
JobExecution jobExecution = batchJobLauncher.run(batchPayJob, new RunIdIncrementer().getNext(null));
return jobExecution.getStatus().name();
}catch (Exception e){
log.error("执行job:batchPayJob发生异常{}",e);
}
return null;
}
}
4.1.15 控制层
接受页面请求,调用job调度层方法,启动job
@Controller
@RequestMapping("/batchPay")
public class BatchPayController {
@Autowired
private BatchPayJobLauncher batchPayJobLauncher;
@ResponseBody
@GetMapping("/doBatchPayJob")
public String doBatchPayJob(){
String result = batchPayJobLauncher.doBatchPayJob();
if (StringUtils.isBlank(result)){
result = "调用失败.发生异常";
}
return result;
}
}
4.1.16 项目启动类
@SpringBootApplication //springboot启动
//@EnableBatchProcessing //加载所有的job,这里不能有这个,否则不能调度
@ImportResource(locations = "classpath:*-config.xml")
public class Application {
public static void main(String[] args) {
/** 创建SpringApplication对象 */
SpringApplication springApplication =
new SpringApplication(Application.class);
/** 设置横幅关闭 */
springApplication.setBannerMode(Banner.Mode.OFF);
/** 运行 */
springApplication.run(args);
}
}
4.1.17 项目结构
运行项目启动类,访问: http://localhost:8888/batchPay/doBatchPayJob