Skip to content

[3.2.x] #356: Support flush statements via Mapper interface #361

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 1 commit into from
Mar 2, 2015
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
31 changes: 31 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Flush.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2009-2015 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.apache.ibatis.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The maker annotation that invoke a flush statements via Mapper interface.
*
* @author Kazuki Shimizu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Flush {
}
21 changes: 15 additions & 6 deletions src/main/java/org/apache/ibatis/binding/MapperMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.apache.ibatis.binding;

import org.apache.ibatis.annotations.Flush;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.mapping.MappedStatement;
Expand Down Expand Up @@ -67,6 +68,8 @@ public Object execute(SqlSession sqlSession, Object[] args) {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
}
} else if (SqlCommandType.FLUSH == command.getType()) {
result = sqlSession.flushStatements();
} else {
throw new BindingException("Unknown execution method for: " + command.getName());
}
Expand Down Expand Up @@ -186,12 +189,18 @@ public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method
}
}
if (ms == null) {
throw new BindingException("Invalid bound statement (not found): " + statementName);
}
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
if(method.getAnnotation(Flush.class) != null){
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): " + statementName);
}
} else {
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
* @author Clinton Begin
*/
public enum SqlCommandType {
UNKNOWN, INSERT, UPDATE, DELETE, SELECT;
UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import domain.blog.Post;
import domain.blog.Section;
import org.apache.ibatis.annotations.*;
import domain.blog.Author;
import domain.blog.Post;
import domain.blog.Section;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.RowBounds;

import java.util.List;
Expand Down Expand Up @@ -88,4 +92,8 @@ List<Post> findThreeSpecificPosts(@Param("one") int one,
RowBounds rowBounds,
@Param("two") int two,
int three);

@Flush
List<BatchResult> flush();

}
93 changes: 93 additions & 0 deletions src/test/java/org/apache/ibatis/binding/FlushTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2009-2015 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.apache.ibatis.binding;

import org.apache.ibatis.BaseDataTest;
import domain.blog.Author;
import domain.blog.Post;
import domain.blog.Section;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class FlushTest {
private static SqlSessionFactory sqlSessionFactory;

@BeforeClass
public static void setup() throws Exception {
DataSource dataSource = BaseDataTest.createBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.setDefaultExecutorType(ExecutorType.BATCH);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundAuthorMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

@Test
public void invokeFlushStatementsViaMapper() {

SqlSession session = sqlSessionFactory.openSession();

try {

BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
List<Integer> ids = new ArrayList<Integer>();
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());

// test
List<BatchResult> results = mapper.flush();

assertThat(results.size(), is(1));
assertThat(results.get(0).getUpdateCounts().length, is(ids.size()));

for (int id : ids) {
Author selectedAuthor = mapper.selectAuthor(id);
assertNotNull(id + " is not found.", selectedAuthor);
}

session.rollback();
} finally {
session.close();
}

}

}