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.session;
17
18 import java.io.Closeable;
19 import java.sql.Connection;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.ibatis.executor.BatchResult;
24
25 /**
26 * The primary Java interface for working with MyBatis.
27 * Through this interface you can execute commands, get mappers and manage transactions.
28 *
29 * @author Clinton Begin
30 */
31 public interface SqlSession extends Closeable {
32
33 /**
34 * Retrieve a single row mapped from the statement key
35 * @param <T> the returned object type
36 * @param statement
37 * @return Mapped object
38 */
39 <T> T selectOne(String statement);
40
41 /**
42 * Retrieve a single row mapped from the statement key and parameter.
43 * @param <T> the returned object type
44 * @param statement Unique identifier matching the statement to use.
45 * @param parameter A parameter object to pass to the statement.
46 * @return Mapped object
47 */
48 <T> T selectOne(String statement, Object parameter);
49
50 /**
51 * Retrieve a list of mapped objects from the statement key and parameter.
52 * @param <E> the returned list element type
53 * @param statement Unique identifier matching the statement to use.
54 * @return List of mapped object
55 */
56 <E> List<E> selectList(String statement);
57
58 /**
59 * Retrieve a list of mapped objects from the statement key and parameter.
60 * @param <E> the returned list element type
61 * @param statement Unique identifier matching the statement to use.
62 * @param parameter A parameter object to pass to the statement.
63 * @return List of mapped object
64 */
65 <E> List<E> selectList(String statement, Object parameter);
66
67 /**
68 * Retrieve a list of mapped objects from the statement key and parameter,
69 * within the specified row bounds.
70 * @param <E> the returned list element type
71 * @param statement Unique identifier matching the statement to use.
72 * @param parameter A parameter object to pass to the statement.
73 * @param rowBounds Bounds to limit object retrieval
74 * @return List of mapped object
75 */
76 <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
77
78 /**
79 * The selectMap is a special case in that it is designed to convert a list
80 * of results into a Map based on one of the properties in the resulting
81 * objects.
82 * Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
83 * @param <K> the returned Map keys type
84 * @param <V> the returned Map values type
85 * @param statement Unique identifier matching the statement to use.
86 * @param mapKey The property to use as key for each value in the list.
87 * @return Map containing key pair data.
88 */
89 <K, V> Map<K, V> selectMap(String statement, String mapKey);
90
91 /**
92 * The selectMap is a special case in that it is designed to convert a list
93 * of results into a Map based on one of the properties in the resulting
94 * objects.
95 * @param <K> the returned Map keys type
96 * @param <V> the returned Map values type
97 * @param statement Unique identifier matching the statement to use.
98 * @param parameter A parameter object to pass to the statement.
99 * @param mapKey The property to use as key for each value in the list.
100 * @return Map containing key pair data.
101 */
102 <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
103
104 /**
105 * The selectMap is a special case in that it is designed to convert a list
106 * of results into a Map based on one of the properties in the resulting
107 * objects.
108 * @param <K> the returned Map keys type
109 * @param <V> the returned Map values type
110 * @param statement Unique identifier matching the statement to use.
111 * @param parameter A parameter object to pass to the statement.
112 * @param mapKey The property to use as key for each value in the list.
113 * @param rowBounds Bounds to limit object retrieval
114 * @return Map containing key pair data.
115 */
116 <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
117
118 /**
119 * Retrieve a single row mapped from the statement key and parameter
120 * using a {@code ResultHandler}.
121 * @param statement Unique identifier matching the statement to use.
122 * @param parameter A parameter object to pass to the statement.
123 * @param handler ResultHandler that will handle each retrieved row
124 * @return Mapped object
125 */
126 void select(String statement, Object parameter, ResultHandler handler);
127
128 /**
129 * Retrieve a single row mapped from the statement
130 * using a {@code ResultHandler}.
131 * @param statement Unique identifier matching the statement to use.
132 * @param handler ResultHandler that will handle each retrieved row
133 * @return Mapped object
134 */
135 void select(String statement, ResultHandler handler);
136
137 /**
138 * Retrieve a single row mapped from the statement key and parameter
139 * using a {@code ResultHandler} and {@code RowBounds}
140 * @param statement Unique identifier matching the statement to use.
141 * @param rowBounds RowBound instance to limit the query results
142 * @param handler ResultHandler that will handle each retrieved row
143 * @return Mapped object
144 */
145 void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
146
147 /**
148 * Execute an insert statement.
149 * @param statement Unique identifier matching the statement to execute.
150 * @return int The number of rows affected by the insert.
151 */
152 int insert(String statement);
153
154 /**
155 * Execute an insert statement with the given parameter object. Any generated
156 * autoincrement values or selectKey entries will modify the given parameter
157 * object properties. Only the number of rows affected will be returned.
158 * @param statement Unique identifier matching the statement to execute.
159 * @param parameter A parameter object to pass to the statement.
160 * @return int The number of rows affected by the insert.
161 */
162 int insert(String statement, Object parameter);
163
164 /**
165 * Execute an update statement. The number of rows affected will be returned.
166 * @param statement Unique identifier matching the statement to execute.
167 * @return int The number of rows affected by the update.
168 */
169 int update(String statement);
170
171 /**
172 * Execute an update statement. The number of rows affected will be returned.
173 * @param statement Unique identifier matching the statement to execute.
174 * @param parameter A parameter object to pass to the statement.
175 * @return int The number of rows affected by the update.
176 */
177 int update(String statement, Object parameter);
178
179 /**
180 * Execute a delete statement. The number of rows affected will be returned.
181 * @param statement Unique identifier matching the statement to execute.
182 * @return int The number of rows affected by the delete.
183 */
184 int delete(String statement);
185
186 /**
187 * Execute a delete statement. The number of rows affected will be returned.
188 * @param statement Unique identifier matching the statement to execute.
189 * @param parameter A parameter object to pass to the statement.
190 * @return int The number of rows affected by the delete.
191 */
192 int delete(String statement, Object parameter);
193
194 /**
195 * Flushes batch statements and commits database connection.
196 * Note that database connection will not be committed if no updates/deletes/inserts were called.
197 * To force the commit call {@link SqlSession#commit(boolean)}
198 */
199 void commit();
200
201 /**
202 * Flushes batch statements and commits database connection.
203 * @param force forces connection commit
204 */
205 void commit(boolean force);
206
207 /**
208 * Discards pending batch statements and rolls database connection back.
209 * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
210 * To force the rollback call {@link SqlSession#rollback(boolean)}
211 */
212 void rollback();
213
214 /**
215 * Discards pending batch statements and rolls database connection back.
216 * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
217 * @param force forces connection rollback
218 */
219 void rollback(boolean force);
220
221 /**
222 * Flushes batch statements.
223 * @return BatchResult list of updated records
224 * @since 3.0.6
225 */
226 List<BatchResult> flushStatements();
227
228 /**
229 * Closes the session
230 */
231 @Override
232 void close();
233
234 /**
235 * Clears local session cache
236 */
237 void clearCache();
238
239 /**
240 * Retrieves current configuration
241 * @return Configuration
242 */
243 Configuration getConfiguration();
244
245 /**
246 * Retrieves a mapper.
247 * @param <T> the mapper type
248 * @param type Mapper interface class
249 * @return a mapper bound to this SqlSession
250 */
251 <T> T getMapper(Class<T> type);
252
253 /**
254 * Retrieves inner database connection
255 * @return Connection
256 */
257 Connection getConnection();
258 }