Spring Batch之读数据库——JdbcCursorItemReader之使用框架提供的BeanPropertyRowMapper(三十六)

news/2024/11/7 18:33:36/

一、BeanPropertyRowMapper介绍

参考我的另一篇博客:

Spring Batch之读数据库——JdbcCursorItemReader(三十五)_人……杰的博客-CSDN博客

二、项目实例

1.项目框架

2.代码实现

BatchMain.java:

package com.xj.demo27;import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author : xjfu* @Date : 2021/10/26 20:01* @Description : demo27 JdbcCursorItemReader之使用框架提供的BeanPropertyRowMapper*/
public class BatchMain {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("demo27/job/demo27-job.xml");//Spring Batch的作业启动器,JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");//在batch.xml中配置的一个作业Job job  = (Job)context.getBean("billJob");try{//开始执行这个作业,获得处理结果(要运行的job,job参数对象)JobExecution result = launcher.run(job, new JobParameters());System.out.println(result.toString());}catch (Exception e){e.printStackTrace();}}
}

CreditBill.java:

package com.xj.demo27;/*** @Author : xjfu* @Date : 2021/10/26 19:27* @Description :*/
public class CreditBill {//银行卡账户IDprivate String accountID = "";//持卡人姓名private String name = "";//消费金额private double amount = 0;//消费日期private String date = "";//消费场所private String address = "";public String getAccountID() {return accountID;}public void setAccountID(String accountID) {this.accountID = accountID;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getAmount() {return amount;}public void setAmount(double amount) {this.amount = amount;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return this.accountID + "," + this.name + "," + this.amount + "," + this.date + "," + this.address;}
}

CreditBillProcessor.java:

package com.xj.demo27;import org.springframework.batch.item.ItemProcessor;public class CreditBillProcessor implementsItemProcessor<CreditBill, CreditBill> {public CreditBill process(CreditBill bill) throws Exception {System.out.println(bill.toString());return bill;}
}

DummyCreditItemWriter.java:

package com.xj.demo27;import org.springframework.batch.item.ItemWriter;
import java.util.ArrayList;
import java.util.List;public class DummyCreditItemWriter implements ItemWriter<CreditBill> {public List<CreditBill> creditBills = new ArrayList<CreditBill>();public void write(List<? extends CreditBill> items) throws Exception {creditBills.addAll(items);}public List<CreditBill> getCredits() {return creditBills;}
}

demo27-job.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: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/batch http://www.springframework.org/schema/batch/spring-batch.xsd"><!--导入文件--><import resource="classpath:demo27/job/demo27-jobContext.xml"/><!--定义名字为billJob的作业--><batch:job id="billJob"><!--定义名字为billStep的作业步--><batch:step id="billStep"><batch:tasklet transaction-manager="transactionManager"><!--定义读、处理、写操作,规定每处理两条数据,进行一次写入操作,这样可以提高写的效率--><batch:chunk reader="jdbcItemReader" processor="creditBillProcessor" writer="creditItemWriter"   commit-interval="2"></batch:chunk></batch:tasklet></batch:step></batch:job>
</beans>

demo27-jobContext.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:batch="http://www.springframework.org/schema/batch"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--引入配置参数--><context:property-placeholder location="classpath:/demo27/properties/demo27-batch-mysql.properties" /><!--data-source:定义数据源,默认dataSourcetransaction-manager:定义事务管理器isolation-level-for-create:定义创建Job Execution时候的事务隔离级别,避免多个Job Execution执行一个Job Instance,默认SERIALIZABLEtable_prefix:定义使用的数据库表的前缀为BATCH_,默认BATCH_max-varchar-length:定义varchar的最大长度为1000,默认值为2500--><batch:job-repositoryid="jobRepository"data-source="dataSource"transaction-manager="transactionManager"isolation-level-for-create="SERIALIZABLE"table-prefix="BATCH_"max-varchar-length="1000"/><!--数据库的事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--定义作业调度器,用来启动job--><bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><!--注入jobRepository--><property name="jobRepository" ref="jobRepository"/></bean><!--数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>${datasource.driver}</value></property><property name="url"><value>${datasource.url}</value></property><property name="username" value="${datasource.username}"></property><property name="password" value="${datasource.password}"></property></bean><!-- 读取db --><bean id="jdbcItemReader" scope="step" class="org.springframework.batch.item.database.JdbcCursorItemReader" ><!--指定访问的数据源--><property name="dataSource" ref="dataSource"/><!--指定查询的SQL语句--><property name="sql" value="select ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS from t_credit where id between 1 and 5 "/><!--用于将指定结果集ResultSet转换为Java业务对象,即CreditBill--><property name="rowMapper"><bean class="org.springframework.jdbc.core.BeanPropertyRowMapper"><property name="mappedClass" value="com.xj.demo27.CreditBill"/></bean></property></bean><!--处理类--><bean id="creditBillProcessor" scope="step" class="com.xj.demo27.CreditBillProcessor"/><!--写类--><bean id="creditItemWriter" class="com.xj.demo27.DummyCreditItemWriter"/></beans>

create-tables-mysql.sql:

DROP TABLE IF EXISTS t_credit;
DROP TABLE IF EXISTS t_destcredit;CREATE TABLE t_credit(ID VARCHAR(10),ACCOUNTID VARCHAR(20),NAME VARCHAR(10),AMOUNT NUMERIC(10,2),DATE VARCHAR(20),ADDRESS VARCHAR(128),primary key (ID))ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE t_destcredit(ID VARCHAR(10),ACCOUNTID VARCHAR(20),NAME VARCHAR(10),AMOUNT NUMERIC(10,2),DATE VARCHAR(20),ADDRESS VARCHAR(128),primary key (ID))ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO t_credit(ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS) VALUES('1','4047390012345678','tom',100.00,'2013-2-2 12:00:08','Lu Jia Zui road');
INSERT INTO t_credit(ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS) VALUES('2','4047390012345678','tom',320.00,'2013-2-3 10:35:21','Lu Jia Zui road');
INSERT INTO t_credit(ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS) VALUES('3','4047390012345678','tom',674.70,'2013-2-6 16:26:49','South Linyi road');
INSERT INTO t_credit(ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS) VALUES('4','4047390012345678','tom',793.20,'2013-2-9 15:15:37','Longyang road');
INSERT INTO t_credit(ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS) VALUES('5','4047390012345678','tom',360.00,'2013-2-11 11:12:38','Longyang road');

demo27-batch-mysql.properties:

datasource.driver=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://127.0.0.1:3306/spring_batch_demo1?serverTimezone=UTC
datasource.username=root
datasource.password=12345

3.运行结果

 三、参考

1.Spring Batch之配置DB Job Repository 项目举例(三)_人……杰的博客-CSDN博客


http://www.ppmy.cn/news/911183.html

相关文章

幸福摩天轮

他们相约的城市&#xff0c;有美丽的世界之窗。 她想&#xff0c;那里一定有很大很大的摩天轮。 有一天&#xff0c;她会和他坐上这摩天轮&#xff0c;一起走完全程。 她心里一直有隐隐的不安&#xff0c;只是&#xff0c;她知道&#xff0c;即使是受伤&#xff0c;也要爱。 …

H5实现摩天轮

代码&#xff1a; html:css效果 <head><meta charset"utf-8" /><title>幸福摩天轮</title><link rel"stylesheet" href"css/mtl.css" /> </head> <body><div><ul><li></li&g…

摩天大树问题

一 问题描述 有一棵摩天大树&#xff0c;在树的每个分支节点上都有一个整数&#xff0c;可否在树上找到这样一个链&#xff0c;以便链上所有整数的乘积&#xff08;mod 10^63&#xff09;都等于k &#xff1f; 二 输入和输出 1 输入 输入包含几个测试用例。每个测试用例的第…

前端摩天轮效果实现,鼠标移入停止,移出继续转动。

最近实现了摩天轮的效果&#xff0c;记录下实现原理与过程&#xff0c;备忘&#xff0c;分享。 兼容性不高&#xff0c;IE10,11,Chrome41。 360浏览器(急速模式会有卡顿的感觉) 使用css3js控制了摩天轮旋转&#xff0c;以及鼠标移入移出的停止动画的效果 原理:css3中有旋转的特…

css33d摩天轮动画

摩天轮代码 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><style>*{margin: 0;padding: 0;}html,body{height:100%;}body{background: url("img3/2.jpg")no-repeat;background-size:100%…

【力扣1599】 经营摩天轮的最大利润

你正在经营一座摩天轮&#xff0c;该摩天轮共有 4 个座舱 &#xff0c;每个座舱 最多可以容纳 4 位游客 。你可以 逆时针 轮转座舱&#xff0c;但每次轮转都需要支付一定的运行成本 runningCost 。摩天轮每次轮转都恰好转动 1 / 4 周。 给你一个长度为 n 的数组 customers &…

摩天大楼

Problem:摩天大楼 Description: 随着科技的发展&#xff0c;某国家准备修建一座高楼&#xff0c;高度为n&#xff08;n<1e18&#xff09;&#xff0c;来展现他们国家的强大国力&#xff0c;他们国家有两支强大的施工队&#xff0c;国家于是把修建计划给了这两只施工队&…

css3实现摩天轮特效

css3效果摩天轮 效果图html代码css3代码完整代码 效果图 html代码 <div class"box"><div class"wheel"><ul><li></li><li></li><li></li><li></li><li></li><li>&l…