How to retrieve a specific column value from the mybatis

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to retrieve a specific column value from the mybatis

nvishy
This post has NOT been accepted by the mailing list yet.
My goal is to write an interceptor to audit the table entries when certain transactions happened. I want to capture only insert/update/delete on specific tables which I am able to do. WHAT I AM UNABLE TO DO IS TO GET A VALUE OF SPECIFIC COLUMN - "SERIAL ID" which is like the key on which all this auditing tables depends on. I even see something in logs like this but does not show the actual value.

@Intercepts({
    @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
    @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class,
            ResultHandler.class }), })

public class SimpleInterceptor implements Interceptor {

    private static final Log Logger = LogFactory.getLog(SimpleInterceptor.class);
    private static final String DBAUDIT = "DBAUDIT";

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
         for (Object obj : invocation.getArgs()) {
            if (obj instanceof MappedStatement) {
                MappedStatement ms = (MappedStatement) obj;
                if (SqlCommandType.INSERT.equals(ms.getSqlCommandType()) ||SqlCommandType.DELETE.equals(ms.getSqlCommandType())
                    || SqlCommandType.UPDATE.equals(ms.getSqlCommandType())) {
                    Object parameter = invocation.getArgs()[1];
                    BoundSql boundSql = ms.getBoundSql(parameter);
                    List<ParameterMapping> boundParams = boundSql.getParameterMappings();
                    for (int i = 0; i < boundParams.size(); i++) {
                         final ParameterMapping parameterMapping = boundParams.get(i);
 Logger.info("parameterMapping.getProperty():" + parameterMapping.getProperty() + "\"parameterMapping.getResultMapId:\"" + parameterMapping.getResultMapId()
                                              + "\"parameterMapping.expression:\"" + parameterMapping.getExpression());
                    }
                    String[] resultSet = ms.getResulSets();
                    insertRecordIntoDBAudit(ms);
                }  
            }
        }
            return invocation.proceed();
        }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // intentionally left empty
    }

    public void insertRecordIntoDBAudit(MappedStatement mappedStmt) {
        final String sql = "insert into "+ DBAUDIT + " (TABLE_NAME, SERIALID, ACTION, CREATED_DT, CREATED_BY)" + " values(?,?,?,?,?)";
        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        try {
            dbConnection = ((DataSource)getApplicationContext().getBean("dataSource")).getConnection();
            preparedStatement = dbConnection.prepareStatement(sql);
            preparedStatement.setString(1, mappedStmt.getId() );
            preparedStatement.setString(2, "111" );// Need to retrieve SerialId which is many mappedstatements (Insert/Update/Delete statements)
            preparedStatement.setString(3, mappedStmt.getSqlCommandType().toString());
            preparedStatement.setDate(4, getCurrentDate());
            preparedStatement.setString(5,"AuditUser" );
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            Logger.error("Exception thrown" + e);
        } finally {
            preparedStatement.close();
            dbConnection.close();
        }
    }

    private static java.sql.Date getCurrentDate() {
        java.util.Date today = new java.util.Date();
        return new java.sql.Date(today.getTime());
    }

Logs show as follows
2017-08-22 13:50:45,958 INFO  [Persist-Pool-Thread-12] SampleInterceptor.java:77 - The SQL statement is delete from TM_ACCOUNT    
     WHERE (  FK_ACCOUNT_GID = ? )
2017-08-22 13:50:45,958 INFO  [Persist-Pool-Thread-2] SampleInterceptor.java:85  parameterMapping.getProperty():__frch_criterion_1.value"parameterMapping.getResultMapId:"null"parameterMapping.expression:"null

How can I retrieve what is the actual value inside ():__frch_criterion_1.value???? This is require for me to move forward