본문 바로가기
Developer/Java

[Java] ibatis SQL ID 로그찍기

by 순수한소년 2023. 8. 1.
728x90
반응형

ibatis SQL ID 로그찍기

mybatis-3.2.8.jar
0.68MB
mybatis-3.3.0.jar
1.35MB

 

DefaultSqlSession.java
0.01MB
StrictMap.java
0.00MB

 

@

DefaultSqlSession.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package org.apache.ibatis.session.defaults;
 
import java.sql.SQLException;
import java.sql.Connection;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.executor.result.DefaultResultContext;
import org.apache.ibatis.executor.result.DefaultMapResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Map;
import java.util.List;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
//import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
public class DefaultSqlSession implements SqlSession {
    static org.slf4j.Logger logger = LoggerFactory.getLogger(DefaultSqlSession.class);
//    protected Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private Configuration configuration;
    private Executor executor;
    private boolean autoCommit;
    private boolean dirty;
 
    public DefaultSqlSession(final Configuration configuration, final Executor executor, final boolean autoCommit) {
        this.configuration = configuration;
        this.executor = executor;
        this.dirty = false;
        this.autoCommit = autoCommit;
    }
 
    public DefaultSqlSession(final Configuration configuration, final Executor executor) {
        this(configuration, executor, false);
    }
 
    public <T> T selectOne(final String statement) {
        System.out.println("\n\n@@@@@@@@ selectOne1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectOne1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.selectOne(statement, null);
    }
 
    public <T> T selectOne(final String statement, final Object parameter) {
        System.out.println("\n\n@@@@@@@@@ selectOne2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@@ selectOne2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        final List<T> list = this.selectList(statement, parameter);
        if (list.size() == 1) {
            return list.get(0);
        }
        if (list.size() > 1) {
            throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
        }
        return null;
    }
 
    public <K, V> Map<K, V> selectMap(final String statement, final String mapKey) {
        System.out.println("\n\n@@@@@@@@ selectMap2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectMap2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.selectMap(statement, null, mapKey, RowBounds.DEFAULT);
    }
 
    public <K, V> Map<K, V> selectMap(final String statement, final Object parameter, final String mapKey) {
        System.out.println("\n\n@@@@@@@@ selectMap3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectMap3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.selectMap(statement, parameter, mapKey, RowBounds.DEFAULT);
    }
 
    public <K, V> Map<K, V> selectMap(final String statement, final Object parameter, final String mapKey, final RowBounds rowBounds) {
        System.out.println("\n\n@@@@@@@@ selectMap4 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectMap4 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        final List<?> list = this.selectList(statement, parameter, rowBounds);
        final DefaultMapResultHandler<K, V> mapResultHandler = (DefaultMapResultHandler<K, V>new DefaultMapResultHandler(mapKey, this.configuration.getObjectFactory(), this.configuration.getObjectWrapperFactory());
        final DefaultResultContext context = new DefaultResultContext();
        for (final Object o : list) {
            context.nextResultObject(o);
            mapResultHandler.handleResult((ResultContext) context);
        }
        final Map<K, V> selectedMap = (Map<K, V>) mapResultHandler.getMappedResults();
        return selectedMap;
    }
 
    public <E> List<E> selectList(final String statement) {
        System.out.println("\n\n@@@@@@@@ selectList1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectList1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.selectList(statement, null);
    }
 
    public <E> List<E> selectList(final String statement, final Object parameter) {
        System.out.println("\n\n@@@@@@@@ selectList2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectList2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.selectList(statement, parameter, RowBounds.DEFAULT);
    }
 
    public <E> List<E> selectList(final String statement, final Object parameter, final RowBounds rowBounds) {
        System.out.println("\n\n@@@@@@@@ selectList3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ selectList3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        try {
            final MappedStatement ms = this.configuration.getMappedStatement(statement);
            final List<E> result = (List<E>this.executor.query(ms, this.wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
            return result;
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public void select(final String statement, final Object parameter, final ResultHandler handler) {
        System.out.println("\n\n@@@@@@@@ select3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ select3 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        this.select(statement, parameter, RowBounds.DEFAULT, handler);
    }
 
    public void select(final String statement, final ResultHandler handler) {
        System.out.println("\n\n@@@@@@@@ select2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ select2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        this.select(statement, null, RowBounds.DEFAULT, handler);
    }
 
    public void select(final String statement, final Object parameter, final RowBounds rowBounds, final ResultHandler handler) {
        try {
            System.out.println("\n\n@@@@@@@@ select4 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
            logger.debug("\n\n@@@@@@@@ select4 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
            final MappedStatement ms = this.configuration.getMappedStatement(statement);
            this.executor.query(ms, this.wrapCollection(parameter), rowBounds, handler);
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public int insert(final String statement) {
        System.out.println("\n\n@@@@@@@@ insert1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ insert1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.insert(statement, null);
    }
 
    public int insert(final String statement, final Object parameter) {
        System.out.println("\n\n@@@@@@@@ insert2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ insert1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.update(statement, parameter);
    }
 
    public int update(final String statement) {
        System.out.println("\n\n@@@@@@@@ update1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ insert1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.update(statement, null);
    }
 
    public int update(final String statement, final Object parameter) {
        try {
            this.dirty = true;
            System.out.println("\n\n@@@@@@@@ update2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
            logger.debug("\n\n@@@@@@@@ update2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
            final MappedStatement ms = this.configuration.getMappedStatement(statement);
            return this.executor.update(ms, this.wrapCollection(parameter));
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error updating database.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public int delete(final String statement) {
        System.out.println("\n\n@@@@@@@@ delete1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ delete1 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.update(statement, null);
    }
 
    public int delete(final String statement, final Object parameter) {
        System.out.println("\n\n@@@@@@@@ delete2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        logger.debug("\n\n@@@@@@@@ delete2 statement @@@@@@@@\n"+statement+"\n"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+"\n");
        return this.update(statement, parameter);
    }
 
    public void commit() {
        this.commit(false);
    }
 
    public void commit(final boolean force) {
        try {
            this.executor.commit(this.isCommitOrRollbackRequired(force));
            this.dirty = false;
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error committing transaction.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public void rollback() {
        this.rollback(false);
    }
 
    public void rollback(final boolean force) {
        try {
            this.executor.rollback(this.isCommitOrRollbackRequired(force));
            this.dirty = false;
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error rolling back transaction.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public List<BatchResult> flushStatements() {
        try {
            return (List<BatchResult>this.executor.flushStatements();
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error flushing statements.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public void close() {
        try {
            this.executor.close(this.isCommitOrRollbackRequired(false));
            this.dirty = false;
        } finally {
            ErrorContext.instance().reset();
        }
    }
 
    public Configuration getConfiguration() {
        return this.configuration;
    }
 
    public <T> T getMapper(final Class<T> type) {
        return (T) this.configuration.getMapper((Class) type, (SqlSession) this);
    }
 
    public Connection getConnection() {
        try {
            return this.executor.getTransaction().getConnection();
        } catch (SQLException e) {
            throw ExceptionFactory.wrapException("Error getting a new connection.  Cause: " + e, (Exception) e);
        }
    }
 
    public void clearCache() {
        this.executor.clearLocalCache();
    }
 
    private boolean isCommitOrRollbackRequired(final boolean force) {
        return (!this.autoCommit && this.dirty) || force;
    }
 
    private Object wrapCollection(Object object) {
        if ((object instanceof List)) {
            StrictMap map = new StrictMap();
            map.put("list", object);
            return map;
        }
        if ((object != null&& (object.getClass().isArray())) {
            StrictMap map = new StrictMap();
            map.put("array", object);
            return map;
        }
        return object;
    }
 
}
cs

 

@

StrictMap.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.apache.ibatis.session.defaults;
 
import org.apache.ibatis.binding.BindingException;
import java.util.HashMap;
 
public class StrictMap<V> extends HashMap<String, V>
{
    private static final long serialVersionUID = -5741767162221585340L;
    
    @Override
    public V get(final Object key) {
        if (!super.containsKey(key)) {
            throw new BindingException("BindingException Parameter '" + key + "' not found. Available parameters are " + this.keySet());
        }
        return super.get(key);
    }
}
cs

@

728x90
반응형