본문 바로가기
java/mybatis

mybatis query 보기

by 새로운 도전을 위한 한걸음 2014. 8. 11.

mybatis 쿼리 보기


//쿼리를 파마미터 셋팅된 형태로 보고 싶을때

public static String getQuery(SqlSession sqlSession, String queryId , Object sqlParam){

BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(queryId).getSqlSource().getBoundSql(sqlParam);

String query1 = boundSql.getSql();

Object paramObj = boundSql.getParameterObject();

 

if(paramObj != null){              // 파라미터가 아무것도 없을 경우

List<ParameterMapping> paramMapping = boundSql.getParameterMappings();

 

for(ParameterMapping mapping : paramMapping){

String propValue = mapping.getProperty();       

query1=query1.replaceFirst("\\?", "#{"+propValue+"}");

}

}

return query1; 

}



// 쿼리를 ? 로 보고 싶을때

public static String getQuery(SqlSession sqlSession, String queryId , Object sqlParam){

return sqlSession.getConfiguration().getMappedStatement(queryId).getSqlSource().getBoundSql(sqlParam).getSql();

}