Springbatch operations DB, springbatchdb

Source: Internet
Author: User

Springbatch operations DB, springbatchdb

I. Requirement Analysis

Use Spring Batch to read and write databases: read data from one table and insert data into another table in batches.


Ii. Code Implementation

1. code structure:


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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http: // Www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <! -- Configure the spring scan range --> <context: component-scan base-package = "com. zdp"/> <! -- Configure the data source --> <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 =" jobluncher "class =" org. springframework. batch. core. launch. support. simplejobluncher "> <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: Scan spring annotations

Jobluncher: Start a Job

JobRepository: Provides persistence operations for jobs.

TransactionManager: Provides transaction management operations.


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/beanshttp://www.springframework.org/schema/beans/spring-beans- 3.1.xsdhttp: // www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <! -- Introduce the core spring configuration file --> <import resource = "applicationContext. xml"/> <batch: job id = "ledgerJob"> <! -- Monitor job running status --> <batch: listeners> <batch: listener ref = "appJobExecutionListener"/> </batch: listeners> <batch: step id = "step"> <! -- Add transaction control --> <batch: tasklet transaction-manager = "transactionManager"> <batch: listeners> <batch: listener ref = "itemFailureLoggerListener"/> </batch: listeners> <! -- Commit-interval: Number of batch submissions; skip-limit: number of records allowed to be skipped --> <batch: chunk reader = "ledgerReader" writer = "ledgerWriter" commit-interval = "1000" skip-limit = "1000"> <batch: skippable-exception-classes> <batch: include class = "java. lang. exception "/> <! -- If an exception or its subclass occurs, the Job will still run later --> <batch: exclude class = "java. io. FileNotFoundException"/> <! -- If this exception occurs, the Job will immediately stop --> </batch: skippable-exception-classes> </batch: chunk> </batch: tasklet> </batch: step> </batch: job> <! -- Read data from the ledger table --> <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 "/> <! -- Scheduled task start --> <bean id = "ledgerJobDetail" class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean"> <property name = "targetObject"> <! -- Scheduled execution class --> <ref bean = "quartzLedgerJob"/> </property> <property name = "targetMethod"> <! -- Method of scheduled execution class --> <value> execute </value> </property> </bean> <bean id = "ledgerCronTrigger" class = "org. springframework. scheduling. quartz. cronTriggerBean "> <property name =" jobDetail "> <ref bean =" ledgerJobDetail "/> </property> <property name =" cronExpression "> <! -- Execute at every night --> <value> 0 30 22? ** </Value> </property> </bean> <! -- Trigger factory, which injects all scheduled tasks into the factory --> <bean class = "org. springframework. scheduling. quartz. SchedulerFactoryBean"> <! -- Add a trigger --> <property name = "triggers"> <list> <! -- Inject the test scheduled task defined above (multiple scheduled tasks can be defined and injected at the same time) --> <ref local = "ledgerCronTrigger"/> </list> </property> </bean> </beans>

4. AppJobExecutionListener. java

/*** Listen to the job running status */@ 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

/*** Check whether a read or write error occurs */@ 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 row ing class */@ 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 data operation class */@ Repositorypublic class implements LedgerDao {private static final String SAVE_ SQL = "INSERT INTO LEDGER_TEMP (RECEIPT_DATE, MEMBER_NAME, CHECK_NUMBER, CHECK_DATE, PAYMENT_TYPE, begin, PAYMENT_AMOUNT, COMMENTS) VALUES (?,?,?,?,?,?,?,?) "; @ Autowiredprivate JdbcTemplate jdbcTemplate; @ Overridepublic 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 write data */@ Component ("ledgerWriter") public class LedgerWriter implements ItemWriter <Ledger >{@ Autowiredprivate LedgerDao ledgerDao; /*** write data ** @ param ledgers */public void write (List <? Extends Ledger> ledgers) throws Exception {for (Ledger ledger: ledgers) {ledgerDao. save (ledger );}}}

11. QuartzLedgerJob. java

/*** Scheduled scheduling class */@ Component ("quartzLedgerJob") public class QuartzLedgerJob {private static final Logger LOG = LoggerFactory. getLogger (QuartzLedgerJob. class); @ Autowiredprivate jobluncher; @ Autowiredprivate Job ledgerJob; @ AutowiredJobParametersBuilder jobParameterBulider; private static long counter = 0l; /*** run the business method ** @ throws Exception */public void execute () throws Exception {/*** Spring Batch Job is the same job instance, after successful execution, re-execution is not allowed. * If re-execution is allowed after failure, you can control it by configuring the restartable parameter of the Job. The default value is true. If re-execution is required, work und: * Add a JobParameters build class with the current time as the parameter to ensure different job instances */LOG when other parameters are the same. debug ("start... "); StopWatch stopWatch = new StopWatch (); stopWatch. start (); jobParameterBulider. addDate ("date", new Date (); jobluncher. run (ledgerJob, jobParameterBulider. toJobParameters (); stopWatch. stop (); LOG. debug ("Time elapsed :{}, Execute quartz ledgerJob :{}", stopWatch. prettyPrint (), ++ counter );}}


12. StartQuartz. java

/*** Start scheduled scheduling ** Requirement Description: regularly reads data from the ledger table, and then writes data to the ledger_temp */public class StartQuartz {public static void main (String [] args) Table in batches) 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) );




Spring Batch supports data table deletion and Modification

Of course, yes. You can use the batchUpdate method of JdbcTemplate.
 
How to operate DB files using JAVA

Import package
Import java. SQL .*;

Public static Connection getCon (){
Class. forName ("put the connection string here ");
Connection conn = DriverManager. getConnections ("Connect driver", "username for database Logon", "password ");
}
This method can connect to the database.
Public static ResultSet select (){
PreparedStatement pstmt = getCon (). preparedStatement. ("SQL statement ");
ResultSet rs = pstmt.exe cuteQuerry ();
Return rs;
}
In this way, you can query the database data.

Public static int select (){
PreparedStatement pstmt = getCon (). preparedStatement. ("SQL statement ");
Int rs = pstmt.exe cuteUpdate ();
/* If (rs> 0)
Run successfully and write your Business Method
Else
Failed. Write your Business Method */
Return rs;
}
This is the method for adding, deleting, and modifying
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.