springbatch操作DB
一、需求分析
使用Spring Batch对DB进行读写操作: 从一个表中读取数据, 然后批量的插入另外一张表中.
二、代码实现
1. 代码结构图:
2. 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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 配置spring扫描范围 -->
<context:component-scan base-package="com.zdp" /> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" abstract="false" scope="singleton">
<property name="driverClass" value="org.gjt.mm.mysql.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="checkoutTimeout" value="30000" />
<property name="maxIdleTime" value="120" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="2" />
<property name="initialPoolSize" value="2" />
<property name="maxStatements" value="0" />
<property name="maxStatementsPerConnection" value="0" />
<property name="idleConnectionTestPeriod" value="30" />
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
base-package: 扫描spring注解
jobLauncher: 启动Job
jobRepository: 为Job提供持久化操作
transactionManager: 提供事务管理操作
3. springBatch.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 引入spring核心配置文件 -->
<import resource="applicationContext.xml"/> <batch:job id="ledgerJob">
<!-- 监听job运行状态 -->
<batch:listeners>
<batch:listener ref="appJobExecutionListener" />
</batch:listeners>
<batch:step id="step">
<!-- 加入事务控制 -->
<batch:tasklet transaction-manager="transactionManager">
<batch:listeners>
<batch:listener ref="itemFailureLoggerListener" />
</batch:listeners>
<!-- commit-interval: 批量提交的条数; skip-limit: 指同意跳过记录数 -->
<batch:chunk reader="ledgerReader" writer="ledgerWriter" commit-interval="1000" skip-limit="1000">
<batch:skippable-exception-classes>
<batch:include class="java.lang.Exception"/> <!-- 出现exception或其子类, Job仍然会往后运行 -->
<batch:exclude class="java.io.FileNotFoundException"/> <!-- 出现这个异常, Job会立马停止 -->
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job> <!-- 从ledger表读取数据 -->
<bean id="ledgerReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="select * from ledger" />
<property name="rowMapper" ref="ledgerRowMapper" />
</bean> <bean id="jobParameterBulider" class="org.springframework.batch.core.JobParametersBuilder" /> <!-- 定时任务開始 -->
<bean id="ledgerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<!-- 定时运行类 -->
<ref bean="quartzLedgerJob" />
</property>
<property name="targetMethod">
<!-- 定时运行类的方法 -->
<value>execute</value>
</property>
</bean> <bean id="ledgerCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" >
<ref bean="ledgerJobDetail" />
</property>
<property name="cronExpression" >
<!-- 每天晚上22:30运行 -->
<value>0 30 22 ? * *</value>
</property>
</bean> <!-- 触发器工厂。将全部的定时任务都注入工厂-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 加入触发器 -->
<property name="triggers">
<list>
<!-- 将上面定义的測试定时任务注入(能够定义多个定时任务。同一时候注入)-->
<ref local="ledgerCronTrigger" />
</list>
</property>
</bean>
</beans>
4. AppJobExecutionListener.java
/**
* 监听job执行状态
*/
@Component("appJobExecutionListener")
public class AppJobExecutionListener implements JobExecutionListener {
private final static Logger logger = Logger.getLogger(AppJobExecutionListener.class); public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
logger.info("Job completed: " + jobExecution.getJobId());
} else if (jobExecution.getStatus() == BatchStatus.FAILED) {
logger.info("Job failed: " + jobExecution.getJobId());
}
} public void beforeJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
logger.info("Job completed: " + jobExecution.getJobId());
} else if (jobExecution.getStatus() == BatchStatus.FAILED) {
logger.info("Job failed: " + jobExecution.getJobId());
}
}
}
5. ItemFailureLoggerListener.java
/**
* 检查是读出错还是写出错
*/
@Component("itemFailureLoggerListener")
public class ItemFailureLoggerListener extends ItemListenerSupport<Object, Object> {
private final static Logger LOG = Logger.getLogger(ItemFailureLoggerListener.class); public void onReadError(Exception ex) {
LOG.error("Encountered error on read", ex);
} public void onWriteError(Exception ex, Object item) {
LOG.error("Encountered error on write", ex);
} }
6. Ledger.java
public class Ledger implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private Date receiptDate;
private String memberName;
private String checkNumber;
private Date checkDate;
private String paymentType;
private double depositAmount;
private double paymentAmount;
private String comments; // getter and setter
}
7. LedgerRowMapper.java
/**
* ledger行的映射类
*/
@SuppressWarnings("rawtypes")
@Component("ledgerRowMapper")
public class LedgerRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Ledger ledger = new Ledger();
ledger.setId(rs.getInt("ID"));
ledger.setReceiptDate(rs.getDate("RECEIPT_DATE"));
ledger.setMemberName(rs.getString("MEMBER_NAME"));
ledger.setCheckNumber(rs.getString("MEMBER_NAME"));
ledger.setCheckDate(rs.getDate("CHECK_DATE"));
ledger.setPaymentType(rs.getString("PAYMENT_TYPE"));
ledger.setDepositAmount(rs.getDouble("DEPOSIT_AMOUNT"));
ledger.setPaymentAmount(rs.getDouble("PAYMENT_AMOUNT"));
ledger.setComments(rs.getString("COMMENTS"));
return ledger;
}
}
8. LedgerDao.java
public interface LedgerDao {
public void save(final Ledger item) ;
}
9. LedgerDaoImpl.java
/**
* ledger数据操作类
*/
@Repository
public class LedgerDaoImpl implements LedgerDao { private static final String SAVE_SQL = "INSERT INTO LEDGER_TEMP (RECEIPT_DATE, MEMBER_NAME, CHECK_NUMBER, CHECK_DATE, PAYMENT_TYPE, DEPOSIT_AMOUNT, PAYMENT_AMOUNT, COMMENTS) VALUES(?,? ,? ,?,? ,?,? ,?)"; @Autowired
private JdbcTemplate jdbcTemplate; @Override
public void save(final Ledger item) {
jdbcTemplate.update(SAVE_SQL, new PreparedStatementSetter() {
public void setValues(PreparedStatement stmt) throws SQLException {
stmt.setDate(1, new java.sql.Date(item.getReceiptDate().getTime()));
stmt.setString(2, item.getMemberName());
stmt.setString(3, item.getCheckNumber());
stmt.setDate(4, new java.sql.Date(item.getCheckDate().getTime()));
stmt.setString(5, item.getPaymentType());
stmt.setDouble(6, item.getDepositAmount());
stmt.setDouble(7, item.getPaymentAmount());
stmt.setString(8, item.getComments());
}
});
}
}
10. LedgerWriter.java
/**
* ledger写入数据
*/
@Component("ledgerWriter")
public class LedgerWriter implements ItemWriter<Ledger> { @Autowired
private LedgerDao ledgerDao; /**
* 写入数据
* @param ledgers
*/
public void write(List<? extends Ledger> ledgers) throws Exception {
for (Ledger ledger : ledgers) {
ledgerDao.save(ledger);
}
} }
11. QuartzLedgerJob.java
/**
* 定时调度类
*/
@Component("quartzLedgerJob")
public class QuartzLedgerJob { private static final Logger LOG = LoggerFactory.getLogger(QuartzLedgerJob.class); @Autowired
private JobLauncher jobLauncher; @Autowired
private Job ledgerJob; @Autowired
JobParametersBuilder jobParameterBulider; private static long counter = 0l; /**
* 运行业务方法
* @throws Exception
*/
public void execute() throws Exception {
/**
* Spring Batch Job同一个job instance,成功运行后是不同意又一次运行的,
* 失败后是否同意重跑,可通过配置Job的restartable參数来控制,默认是true,假设须要又一次运行。能够变通处理,
* 加入一个JobParameters构建类,以当前时间作为參数,保证其它參数同样的情况下是不同的job instance
*/
LOG.debug("start...");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
jobParameterBulider.addDate("date", new Date());
jobLauncher.run(ledgerJob, jobParameterBulider.toJobParameters());
stopWatch.stop();
LOG.debug("Time elapsed:{},Execute quartz ledgerJob:{}", stopWatch.prettyPrint(), ++counter);
}
}
12. StartQuartz.java
/**
* 启动定时调度
* 需求描写叙述: 定时从表ledger读取数据, 然后批量写入表ledger_temp
*/
public class StartQuartz {
public static void main(String[] args) throws FileNotFoundException {
new ClassPathXmlApplicationContext("/com/zdp/resources/springBatch.xml");
}
}
13. sql:
create table ledger(
ID int(10) not null AUTO_INCREMENT PRIMARY KEY,
RECEIPT_DATE date,
MEMBER_NAME varchar(10) ,
CHECK_NUMBER varchar(10) ,
CHECK_DATE date,
PAYMENT_TYPE varchar(10) ,
DEPOSIT_AMOUNT double(10,3),
PAYMENT_AMOUNT double(10,3),
COMMENTS varchar(100)
); create table ledger_temp(
ID int(10) not null AUTO_INCREMENT PRIMARY KEY,
RECEIPT_DATE date,
MEMBER_NAME varchar(10) ,
CHECK_NUMBER varchar(10) ,
CHECK_DATE date,
PAYMENT_TYPE varchar(10) ,
DEPOSIT_AMOUNT double(10,3),
PAYMENT_AMOUNT double(10,3),
COMMENTS varchar(100)
);
springbatch操作DB的更多相关文章
- [Android] Android 使用 Greendao 操作 db sqlite(2)-- 封装DaoUtils类
继续接上文: Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用 布局文件同上文一致,这里就不贴了. 一.封装DaoUtils类 User ...
- [Android] Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用
继续接上文: Android 使用 Greendao 操作 db sqlite 布局文件: activity_test_green.xml <?xml version="1.0&quo ...
- [Android] Android 使用 Greendao 操作 db sqlite
Android 使用 Greendao 操作 db sqlite GreenDAO是一个开源的安卓ORM框架,能够使SQLite数据库的开发再次变得有趣.它减轻开发人员处理低级数据库需求,同时节省开发 ...
- Atitit.软件按钮与仪表盘(13)--全文索引操作--db数据库子系统mssql2008
Atitit.软件按钮与仪表盘(13)--全文索引操作--db数据库子系统mssql2008 全文索引操作 4.全文索引和like语句比较 1 5.倒排索引 inverted index 1 2.SQ ...
- 对SQLite数据库操作 操作db文件
sqlite数据库就是一个DB文件. 程序每操作一次数据库都要读一次 .DB 文件 . 这个文件就是这个SQLite数据库. 如果需要依赖包的可以联系我 工具类: package com.hot ...
- Atitit.软件button和仪表板(13)--全文索引操作--db数据库子系统mssql2008
Atitit.软件button和仪表板(13)--全文索引操作--db数据库子系统mssql2008 全文索引操作 4.全文索引和like语句比較 1 5.倒排索引 inverted index 1 ...
- springbatch操作CSV文件
一.需求分析 使用Spring Batch对CSV文件进行读写操作: 读取一个含有四个字段的CSV文件(id, name, age, score), 对文件做简单的处理, 然后输出到还有一个csv文件 ...
- thinkphp 和 laravel使用sql语句操作db和源码浅析
thinkphp 和 laravel是phper开发中用的比较多的两个框架,无所谓好坏,看个人习惯及喜爱! 前言对于一个PHP应用,可能最多的就是操作数据,以致于初学者有时只把php当做数据库增删查改 ...
- 20. Spring Boot 默认、自定义数据源 、配置多个数据源 jdbcTemplate操作DB
Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari,这里主要研究下hikari的默认配置 0. 创建Spring Boot项目,选中 ...
随机推荐
- 在做了 BasePage 时: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 / / 节中包括
摘自: http://lichengguizy.blog.163.com/blog/static/11771858620122342749552/ 只有在配置文件或 Page 指令中将 enableS ...
- Spark Streaming ReceiverTracker架构设计
本节的主要内容: 一.ReceiverTracker的架构设计 二.消息循环系统 三.ReceiverTracker具体实现 Spark Streaming作为Spark Core基础 架构之上的一个 ...
- C#秘密武器之异步编程
一.概述 1.什么是异步? 异步操作通常用于执行完成时间可能较长的任务,如打开大文件.连接远程计算机或查询数据库.异步操作在主应用程序线程以外的线程中执行.应用程序调用方法异步执行某个操作时,应用程序 ...
- Win8 Metro中文件读写删除与复制操作
Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下Win8 Metro中文件的读写操作. 1 Windows 8 Metro Style App ...
- git commit 出现 changes not staged for commit 错误
git commit 出现 changes not staged for commit 错误 修复: 参考:http://stackoverflow.com/questions/8488887/git ...
- python版本管理--pyenv
python版本环境管理 下载依赖 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readli ...
- 1000个经常使用的Python库和演示样例代码
以下是programcreek.com通过分析大量开源码,提取出的最经常使用的python库. 1. sys (4627) 2. os (4088) 3. re (3563) 4 ...
- mui 监听app运行状态
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- lodash获取数组或对象的值 at
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Can report_aeroo_ooo work with Ubuntu 13.10?
来 自 :https://www.odoo.com/forum/help-1/question/can-report-aeroo-ooo-work-with-ubuntu-13-10-34314 I ...