spring batch 에서 여러 파일 데 이 터 를 읽 고 데이터베이스 예제 가 져 오기
파일 을 읽 을 데이터 형식
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.aliyun.springbatch" />
<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"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/aliyun/springbatch/sample/db/jdbc.properties</value>
</property>
</bean>
<!-- -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
batch.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- <bean:import resource="dataSource.xml" /> -->
<bean:import resource="applicationContext.xml" />
<!-- Job -->
<!-- commit-interval="1" 1 -->
<job id="dbJob">
<step id="dbReadAndWriterStep" >
<tasklet>
<chunk reader="userReader" writer="jdbcItemWriter"
commit-interval="1">
</chunk>
</tasklet>
</step>
</job>
<!-- <bean:bean id="jdbcItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step"> <bean:property name="dataSource" ref="dataSource" /> <bean:property
name="sql" value="select id,name,age,score from t_user" /> <bean:property
name="rowMapper"> <bean:bean class="org.springframework.jdbc.core.BeanPropertyRowMapper">
<bean:property name="mappedClass" value="com.aliyun.springbatch.sample.db.User"
/> </bean:bean> </bean:property> </bean:bean> -->
<!-- -->
<bean:bean id="userReader" class="org.springframework.batch.item.file.MultiResourceItemReader"
scope="step">
<!-- -->
<!-- <property name="resource" value="file:./sample.csv" /> -->
<!-- -->
<bean:property name="resources" value="file:#{jobParameters['inputFile']}" />
<!-- -->
<bean:property name="delegate" ref="flatFileItemReader" />
</bean:bean>
<!-- -->
<bean:bean id="flatFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<!-- -->
<bean:property name="linesToSkip" value="1"/>
<!-- -->
<bean:property name="lineMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- -->
<bean:property name="lineTokenizer">
<!-- names , , -->
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="names" value="id,name,age,score" />
</bean:bean>
</bean:property>
<!-- -->
<bean:property name="fieldSetMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="user" />
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
<bean:bean id="user" class="com.aliyun.springbatch.sample.db.User"></bean:bean>
<!-- db -->
<!-- <bean:bean id="jdbcItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<bean:property name="dataSource" ref="dataSource" />
<bean:property name="sql"
value="insert into T_DESTUSER (ID,USERID,USERNAME,PASSWORD,UPDATETIME,UPDATEUSER)
values
(:id,:userId,:userName,:password,:updateDate,:updateUser)" />
<bean:property name="itemSqlParameterSourceProvider">
<bean:bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</bean:property>
</bean:bean> -->
<!-- ItemWriter ItemWriter -->
<bean:bean id="jdbcItemWriter" class="com.aliyun.springbatch.sample.db.JdbcItemWriter">
</bean:bean>
</bean:beans>
jdbc.properties mysql 데이터 원본 프로필
#Oracle
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#validationQuery.sqlserver=SELECT 1 FROM DUAL
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc.username=activitproject
#jdbc.password=activitproject
#Mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_batch_demo
jdbc.username=root
jdbc.password=root
봉 인 된 데이터 의 실체 류 는 스스로 쓰 세 요. 테스트 주 방법:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/aliyun/springbatch/sample/db/batch.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("dbJob");
try {
// JOB ,
JobExecution result = launcher.run(
job,
// job , job
new JobParametersBuilder()
.addString("inputFile",
"src/main/java/com/aliyun/springbatch/sample/db/inputFile*.csv")
.toJobParameters());
//
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.