站长之家app,国内可以做网页的网站,在线网站,英语工作室网站怎么做一、BeanPropertyRowMapper介绍
参考我的另一篇博客#xff1a;
Spring Batch之读数据库——JdbcCursorItemReader#xff08;三十五#xff09;_人……杰的博客-CSDN博客
二、项目实例
1.项目框架 2.代码实现
BatchMain.java:
package com.xj.demo27;import org.spri…一、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 implementsItemProcessorCreditBill, 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 ItemWriterCreditBill {public ListCreditBill creditBills new ArrayListCreditBill();public void write(List? extends CreditBill items) throws Exception {creditBills.addAll(items);}public ListCreditBill getCredits() {return creditBills;}
}demo27-job.xml:
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:batchhttp://www.springframework.org/schema/batchxsi:schemaLocationhttp://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 resourceclasspath:demo27/job/demo27-jobContext.xml/!--定义名字为billJob的作业--batch:job idbillJob!--定义名字为billStep的作业步--batch:step idbillStepbatch:tasklet transaction-managertransactionManager!--定义读、处理、写操作规定每处理两条数据进行一次写入操作这样可以提高写的效率--batch:chunk readerjdbcItemReader processorcreditBillProcessor writercreditItemWriter commit-interval2/batch:chunk/batch:tasklet/batch:step/batch:job
/beans
demo27-jobContext.xml:
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:batchhttp://www.springframework.org/schema/batchxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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 locationclasspath:/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-repositoryidjobRepositorydata-sourcedataSourcetransaction-managertransactionManagerisolation-level-for-createSERIALIZABLEtable-prefixBATCH_max-varchar-length1000/!--数据库的事务管理器--bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean!--定义作业调度器用来启动job--bean idjobLauncher classorg.springframework.batch.core.launch.support.SimpleJobLauncher!--注入jobRepository--property namejobRepository refjobRepository//bean!--数据源--bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassNamevalue${datasource.driver}/value/propertyproperty nameurlvalue${datasource.url}/value/propertyproperty nameusername value${datasource.username}/propertyproperty namepassword value${datasource.password}/property/bean!-- 读取db --bean idjdbcItemReader scopestep classorg.springframework.batch.item.database.JdbcCursorItemReader !--指定访问的数据源--property namedataSource refdataSource/!--指定查询的SQL语句--property namesql valueselect ID,ACCOUNTID,NAME,AMOUNT,DATE,ADDRESS from t_credit where id between 1 and 5 /!--用于将指定结果集ResultSet转换为Java业务对象即CreditBill--property namerowMapperbean classorg.springframework.jdbc.core.BeanPropertyRowMapperproperty namemappedClass valuecom.xj.demo27.CreditBill//bean/property/bean!--处理类--bean idcreditBillProcessor scopestep classcom.xj.demo27.CreditBillProcessor/!--写类--bean idcreditItemWriter classcom.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))ENGINEInnoDB DEFAULT CHARSETutf8;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))ENGINEInnoDB DEFAULT CHARSETutf8;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.drivercom.mysql.cj.jdbc.Driver
datasource.urljdbc:mysql://127.0.0.1:3306/spring_batch_demo1?serverTimezoneUTC
datasource.usernameroot
datasource.password123453.运行结果 三、参考
1.Spring Batch之配置DB Job Repository 项目举例(三)_人……杰的博客-CSDN博客