- public void testIbatisToJDBC(List list, final String statement) throws Exception {
- Connection connection = sqlMapClientTemplate.getDataSource().getConnection();
- PreparedStatement ps = null;
- try {
- /**根据ibatis statement动态拼接sql,并且通过JDBC执行**/
- SqlMapClientImpl sqlMapClient = (SqlMapClientImpl) sqlMapClientTemplate;
- MappedStatement stmt = sqlMapClient.getMappedStatement(statement);
- SessionScope session = new SessionScope();
- session.setSqlMapClient(sqlMapClient);
- StatementScope statementScope = new StatementScope(session);
- stmt.initRequest(statementScope);
- Sql sql = stmt.getSql();
- Object o = list.get(0);
- String sqlString = sql.getSql(statementScope, o);
- ps = connection.prepareStatement(sqlString);
- ParameterMap pmap = sql.getParameterMap(statementScope, o);
- Object[] parameterObj = pmap.getParameterObjectValues(statementScope, o);
- pmap.setParameters(statementScope, ps, parameterObj);
- ps.executeUpdate();
- } catch (Exception e) {
- logger.error("testIbatisToJDBC method" + e.getMessage());
- throw e;
- } finally {
- connection.setAutoCommit(true);
- DBUtil.close(null, ps, connection);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0601201511485.html
来源: http://www.codesnippet.cn/detail/0601201511485.html