Skip to content

Add builder classes for ItemReader and ItemWriter #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.batch.builder;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;

/**
* A builder for the {@link MyBatisBatchItemWriter}.
*
* @author Kazuki Shimizu
* @since 2.0.0
* @see MyBatisBatchItemWriter
*/
public class MyBatisBatchItemWriterBuilder<T> {

private SqlSessionTemplate sqlSessionTemplate;
private SqlSessionFactory sqlSessionFactory;
private String statementId;
private Boolean assertUpdates = true;

/**
* Set the {@link SqlSessionTemplate} to be used by writer for database access.
*
* @param sqlSessionTemplate the {@link SqlSessionTemplate} to be used by writer for database access
* @return this instance for method chaining
* @see MyBatisBatchItemWriter#setSqlSessionTemplate(SqlSessionTemplate)
*/
public MyBatisBatchItemWriterBuilder<T> sqlSessionTemplate(
SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
return this;
}

/**
* Set the {@link SqlSessionFactory} to be used by writer for database access.
*
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
* @return this instance for method chaining
* @see MyBatisBatchItemWriter#setSqlSessionFactory(SqlSessionFactory)
*/
public MyBatisBatchItemWriterBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
return this;
}

/**
* Set the statement id identifying the statement in the SqlMap configuration file.
*
* @param statementId the id for the statement
* @return this instance for method chaining
* @see MyBatisBatchItemWriter#setStatementId(String)
*/
public MyBatisBatchItemWriterBuilder<T> statementId(String statementId) {
this.statementId = statementId;
return this;
}

/**
* The flag that determines whether an assertion is made that all items cause at least one row to
* be updated.
*
* @param assertUpdates the flag to set. Defaults to true
* @return this instance for method chaining
* @see MyBatisBatchItemWriter#setAssertUpdates(boolean)
*/
public MyBatisBatchItemWriterBuilder<T> assertUpdates(boolean assertUpdates) {
this.assertUpdates = assertUpdates;
return this;
}

/**
* Returns a fully built {@link MyBatisBatchItemWriter}.
*
* @return the writer
*/
public MyBatisBatchItemWriter<T> build() {
MyBatisBatchItemWriter<T> writer = new MyBatisBatchItemWriter<>();
writer.setSqlSessionTemplate(this.sqlSessionTemplate);
writer.setSqlSessionFactory(this.sqlSessionFactory);
writer.setStatementId(this.statementId);
if (this.assertUpdates != null) {
writer.setAssertUpdates(this.assertUpdates);
}
return writer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.batch.builder;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;

import java.util.Map;

/**
* A builder for the {@link MyBatisCursorItemReader}.
*
* @author Kazuki Shimizu
* @since 2.0.0
* @see MyBatisCursorItemReader
*/
public class MyBatisCursorItemReaderBuilder<T> {

private SqlSessionFactory sqlSessionFactory;
private String queryId;
private Map<String, Object> parameterValues;
private Boolean saveState;
private Integer maxItemCount;

/**
* Set the {@link SqlSessionFactory} to be used by reader for database access.
*
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
* @return this instance for method chaining
* @see MyBatisCursorItemReader#setSqlSessionFactory(SqlSessionFactory)
*/
public MyBatisCursorItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
return this;
}

/**
* Set the query id identifying the statement in the SqlMap configuration file.
*
* @param queryId the id for the query
* @return this instance for method chaining
* @see MyBatisCursorItemReader#setQueryId(String)
*/
public MyBatisCursorItemReaderBuilder<T> queryId(String queryId) {
this.queryId = queryId;
return this;
}

/**
* Set the parameter values to be used for the query execution.
*
* @param parameterValues the parameter values to be used for the query execution
* @return this instance for method chaining
* @see MyBatisCursorItemReader#setParameterValues(Map)
*/
public MyBatisCursorItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
this.parameterValues = parameterValues;
return this;
}

/**
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should
* be persisted within the {@link org.springframework.batch.item.ExecutionContext} for restart
* purposes.
*
* @param saveState defaults to true
* @return The current instance of the builder.
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
*/
public MyBatisCursorItemReaderBuilder<T> saveState(boolean saveState) {
this.saveState = saveState;
return this;
}

/**
* Configure the max number of items to be read.
*
* @param maxItemCount the max items to be read
* @return The current instance of the builder.
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
*/
public MyBatisCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) {
this.maxItemCount = maxItemCount;
return this;
}

/**
* Returns a fully built {@link MyBatisCursorItemReader}.
*
* @return the reader
*/
public MyBatisCursorItemReader<T> build() {
MyBatisCursorItemReader<T> reader = new MyBatisCursorItemReader<>();
reader.setSqlSessionFactory(this.sqlSessionFactory);
reader.setQueryId(this.queryId);
reader.setParameterValues(this.parameterValues);
if (this.saveState != null) {
reader.setSaveState(saveState);
}
if (this.maxItemCount != null) {
reader.setMaxItemCount(this.maxItemCount);
}
return reader;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.batch.builder;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;

import java.util.Map;

/**
* A builder for the {@link MyBatisPagingItemReader}.
*
* @author Kazuki Shimizu
* @since 2.0.0
* @see MyBatisPagingItemReader
*/
public class MyBatisPagingItemReaderBuilder<T> {

private SqlSessionFactory sqlSessionFactory;
private String queryId;
private Map<String, Object> parameterValues;
private Integer pageSize;
private Boolean saveState;
private Integer maxItemCount;

/**
* Set the {@link SqlSessionFactory} to be used by writer for database access.
*
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
* @return this instance for method chaining
* @see MyBatisPagingItemReader#setSqlSessionFactory(SqlSessionFactory)
*/
public MyBatisPagingItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
return this;
}

/**
* Set the query id identifying the statement in the SqlMap configuration file.
*
* @param queryId the id for the query
* @return this instance for method chaining
* @see MyBatisPagingItemReader#setQueryId(String)
*/
public MyBatisPagingItemReaderBuilder<T> queryId(String queryId) {
this.queryId = queryId;
return this;
}

/**
* Set the parameter values to be used for the query execution.
*
* @param parameterValues the parameter values to be used for the query execution
* @return this instance for method chaining
* @see MyBatisPagingItemReader#setParameterValues(Map)
*/
public MyBatisPagingItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
this.parameterValues = parameterValues;
return this;
}

/**
* The number of records to request per page/query. Defaults to 10. Must be greater than zero.
*
* @param pageSize number of items
* @return this instance for method chaining
* @see org.springframework.batch.item.database.AbstractPagingItemReader#setPageSize(int)
*/
public MyBatisPagingItemReaderBuilder<T> pageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}

/**
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should
* be persisted within the {@link org.springframework.batch.item.ExecutionContext} for restart
* purposes.
*
* @param saveState defaults to true
* @return The current instance of the builder.
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
*/
public MyBatisPagingItemReaderBuilder<T> saveState(boolean saveState) {
this.saveState = saveState;
return this;
}

/**
* Configure the max number of items to be read.
*
* @param maxItemCount the max items to be read
* @return The current instance of the builder.
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
*/
public MyBatisPagingItemReaderBuilder<T> maxItemCount(int maxItemCount) {
this.maxItemCount = maxItemCount;
return this;
}

/**
* Returns a fully built {@link MyBatisPagingItemReader}.
*
* @return the reader
*/
public MyBatisPagingItemReader<T> build() {
MyBatisPagingItemReader<T> reader = new MyBatisPagingItemReader<>();
reader.setSqlSessionFactory(this.sqlSessionFactory);
reader.setQueryId(this.queryId);
reader.setParameterValues(this.parameterValues);
if (this.pageSize != null) {
reader.setPageSize(this.pageSize);
}
if (this.saveState != null) {
reader.setSaveState(saveState);
}
if (this.maxItemCount != null) {
reader.setMaxItemCount(this.maxItemCount);
}
return reader;
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/mybatis/spring/batch/builder/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2010-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Contains classes to builder classes for {@link org.springframework.batch.item.ItemReader} and
* {@link org.springframework.batch.item.ItemWriter}.
*
* @since 2.0.0
*/
package org.mybatis.spring.batch.builder;
Loading