Skip to content

Commit 0e9c048

Browse files
committed
Merge pull request #361 from kazuki43zoo/issues/356_flush-via-mapper
[3.2.x] #356: Support flush statements via Mapper interface
2 parents 10383a2 + 8785e7d commit 0e9c048

File tree

5 files changed

+148
-7
lines changed

5 files changed

+148
-7
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/MapperMethod.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.apache.ibatis.binding;
1717

18+
import org.apache.ibatis.annotations.Flush;
1819
import org.apache.ibatis.annotations.MapKey;
1920
import org.apache.ibatis.annotations.Param;
2021
import org.apache.ibatis.mapping.MappedStatement;
@@ -67,6 +68,8 @@ public Object execute(SqlSession sqlSession, Object[] args) {
6768
Object param = method.convertArgsToSqlCommandParam(args);
6869
result = sqlSession.selectOne(command.getName(), param);
6970
}
71+
} else if (SqlCommandType.FLUSH == command.getType()) {
72+
result = sqlSession.flushStatements();
7073
} else {
7174
throw new BindingException("Unknown execution method for: " + command.getName());
7275
}
@@ -186,12 +189,18 @@ public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method
186189
}
187190
}
188191
if (ms == null) {
189-
throw new BindingException("Invalid bound statement (not found): " + statementName);
190-
}
191-
name = ms.getId();
192-
type = ms.getSqlCommandType();
193-
if (type == SqlCommandType.UNKNOWN) {
194-
throw new BindingException("Unknown execution method for: " + name);
192+
if(method.getAnnotation(Flush.class) != null){
193+
name = null;
194+
type = SqlCommandType.FLUSH;
195+
} else {
196+
throw new BindingException("Invalid bound statement (not found): " + statementName);
197+
}
198+
} else {
199+
name = ms.getId();
200+
type = ms.getSqlCommandType();
201+
if (type == SqlCommandType.UNKNOWN) {
202+
throw new BindingException("Unknown execution method for: " + name);
203+
}
195204
}
196205
}
197206

src/main/java/org/apache/ibatis/mapping/SqlCommandType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
* @author Clinton Begin
2020
*/
2121
public enum SqlCommandType {
22-
UNKNOWN, INSERT, UPDATE, DELETE, SELECT;
22+
UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;
2323
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import domain.blog.Post;
2020
import domain.blog.Section;
2121
import org.apache.ibatis.annotations.*;
22+
import domain.blog.Author;
23+
import domain.blog.Post;
24+
import domain.blog.Section;
25+
import org.apache.ibatis.executor.BatchResult;
2226
import org.apache.ibatis.session.RowBounds;
2327

2428
import java.util.List;
@@ -88,4 +92,8 @@ List<Post> findThreeSpecificPosts(@Param("one") int one,
8892
RowBounds rowBounds,
8993
@Param("two") int two,
9094
int three);
95+
96+
@Flush
97+
List<BatchResult> flush();
98+
9199
}
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 domain.blog.Author;
20+
import domain.blog.Post;
21+
import 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)