Skip to content

Commit c11057c

Browse files
committed
#356: Support flush statements via Mapper interface
1 parent 182e5f7 commit c11057c

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* The maker annotation that invoke a flush statements via Mapper interface.
25+
*
26+
* @author Kazuki Shimizu
27+
*/
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target(ElementType.METHOD)
30+
public @interface Flush {
31+
}

src/main/java/org/apache/ibatis/binding/MapperProxy.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Method;
2121
import java.util.Map;
2222

23+
import org.apache.ibatis.annotations.Flush;
2324
import org.apache.ibatis.reflection.ExceptionUtil;
2425
import org.apache.ibatis.session.SqlSession;
2526

@@ -49,7 +50,16 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4950
throw ExceptionUtil.unwrapThrowable(t);
5051
}
5152
}
52-
final MapperMethod mapperMethod = cachedMapperMethod(method);
53+
final MapperMethod mapperMethod;
54+
try {
55+
mapperMethod = cachedMapperMethod(method);
56+
} catch(BindingException e){
57+
if (method.getAnnotation(Flush.class) != null) {
58+
return sqlSession.flushStatements();
59+
} else {
60+
throw e;
61+
}
62+
}
5363
return mapperMethod.execute(sqlSession, args);
5464
}
5565

src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.ibatis.domain.blog.Author;
2020
import org.apache.ibatis.domain.blog.Post;
2121
import org.apache.ibatis.domain.blog.Section;
22+
import org.apache.ibatis.executor.BatchResult;
2223
import org.apache.ibatis.session.RowBounds;
2324

2425
import java.util.List;
@@ -88,4 +89,8 @@ List<Post> findThreeSpecificPosts(@Param("one") int one,
8889
RowBounds rowBounds,
8990
@Param("two") int two,
9091
int three);
92+
93+
@Flush
94+
List<BatchResult> flush();
95+
9196
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.binding;
17+
18+
import org.apache.ibatis.BaseDataTest;
19+
import org.apache.ibatis.domain.blog.Author;
20+
import org.apache.ibatis.domain.blog.Post;
21+
import org.apache.ibatis.domain.blog.Section;
22+
import org.apache.ibatis.executor.BatchResult;
23+
import org.apache.ibatis.mapping.Environment;
24+
import org.apache.ibatis.session.*;
25+
import org.apache.ibatis.transaction.TransactionFactory;
26+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
import javax.sql.DataSource;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
import static org.hamcrest.core.Is.is;
35+
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertThat;
37+
38+
public class FlushTest {
39+
private static SqlSessionFactory sqlSessionFactory;
40+
41+
@BeforeClass
42+
public static void setup() throws Exception {
43+
DataSource dataSource = BaseDataTest.createBlogDataSource();
44+
TransactionFactory transactionFactory = new JdbcTransactionFactory();
45+
Environment environment = new Environment("Production", transactionFactory, dataSource);
46+
Configuration configuration = new Configuration(environment);
47+
configuration.setDefaultExecutorType(ExecutorType.BATCH);
48+
configuration.getTypeAliasRegistry().registerAlias(Post.class);
49+
configuration.getTypeAliasRegistry().registerAlias(Author.class);
50+
configuration.addMapper(BoundAuthorMapper.class);
51+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
52+
}
53+
54+
@Test
55+
public void invokeFlushStatementsViaMapper() {
56+
57+
SqlSession session = sqlSessionFactory.openSession();
58+
59+
try {
60+
61+
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
62+
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
63+
List<Integer> ids = new ArrayList<Integer>();
64+
mapper.insertAuthor(author);
65+
ids.add(author.getId());
66+
mapper.insertAuthor(author);
67+
ids.add(author.getId());
68+
mapper.insertAuthor(author);
69+
ids.add(author.getId());
70+
mapper.insertAuthor(author);
71+
ids.add(author.getId());
72+
mapper.insertAuthor(author);
73+
ids.add(author.getId());
74+
75+
// test
76+
List<BatchResult> results = mapper.flush();
77+
78+
assertThat(results.size(), is(1));
79+
assertThat(results.get(0).getUpdateCounts().length, is(ids.size()));
80+
81+
for (int id : ids) {
82+
Author selectedAuthor = mapper.selectAuthor(id);
83+
assertNotNull(id + " is not found.", selectedAuthor);
84+
}
85+
86+
session.rollback();
87+
} finally {
88+
session.close();
89+
}
90+
91+
}
92+
93+
}

0 commit comments

Comments
 (0)