org.apache.ibatis.scripting.defaults.DefaultParameterHandler Java Examples

The following examples show how to use org.apache.ibatis.scripting.defaults.DefaultParameterHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlHelper.java    From mybatis-paging with MIT License 6 votes vote down vote up
public static int getCount(final MappedStatement ms, final Connection connection,
                           final Object parameterObject, Dialect dialect) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    String countSql = dialect.getCountString(boundSql.getSql());

    logger.debug("Total count SQL [{}]", countSql);
    logger.debug("Parameters: {} ", parameterObject);

    PreparedStatement stmt = null;
    ResultSet rs;
    try{
        stmt = connection.prepareStatement(countSql);
        DefaultParameterHandler handler = new DefaultParameterHandler(ms, parameterObject, boundSql);
        handler.setParameters(stmt);
        rs = stmt.executeQuery();

        int count = 0;
        if(rs.next()){
            count = rs.getInt(1);
        }

        return count;
    }finally {
        closeStatement(stmt);
    }
}
 
Example #2
Source File: PaginationInterceptor.java    From platform with Apache License 2.0 6 votes vote down vote up
/**
 * 查询总记录条数
 *
 * @param sql             SQL
 * @param mappedStatement {@link MappedStatement}
 * @param boundSql        {@link BoundSql}
 * @param connection      {@link Connection}
 */
protected long queryTotal(String sql, MappedStatement mappedStatement, BoundSql boundSql, Connection connection) {
    long total = 0;
    try (PreparedStatement statement = connection.prepareStatement(sql)) {
        DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql);
        parameterHandler.setParameters(statement);
        try (ResultSet resultSet = statement.executeQuery()) {
            if (resultSet.next()) {
                total = resultSet.getLong(1);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return total;
}
 
Example #3
Source File: AuditInterceptor.java    From JetfireCloud with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    DefaultParameterHandler statementHandler = (DefaultParameterHandler) invocation.getTarget();
    if (statementHandler.getParameterObject() instanceof BasePo) {
        BasePo parameter = (BasePo) statementHandler.getParameterObject();
        String username = StringUtils.defaultIfBlank(UserContextHolder.getInstance().getUsername(), DEFAULT_USERNAME);
        parameter.setCreatedBy(username);
        parameter.setCreatedTime(new Date());
        parameter.setUpdatedBy(username);
        parameter.setUpdatedTime(new Date());
    }
    return invocation.proceed();
}
 
Example #4
Source File: BindingLogPlugin32.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private List<String> getParameters(MappedStatement ms, Object parameterObject, BoundSql boundSql) throws SQLException {
    // DefaultParameterHandler is the only implementation of parameterHandler interface currently. it may be changed later.
    // need additional codes to find a appropriate implementation in that case.
    ParameterHandler parameterHandler = new DefaultParameterHandler(ms, parameterObject, boundSql);
    PreparedStatementParameterLogger parameterLogger = new PreparedStatementParameterLogger();
    parameterHandler.setParameters(parameterLogger);
    return parameterLogger.getParameters();
}
 
Example #5
Source File: IndexingPlugin.java    From scaffold-cloud with MIT License 4 votes vote down vote up
protected void printWarn(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql,
                            Connection connection, Object parameter ) throws Exception {
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		StringBuilder explain = new StringBuilder("EXPLAIN ");
		explain.append(boundSql.getSql());
		String sqlExplain = explain.toString();
		StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings());
		MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource,
				SqlCommandType.SELECT);
		builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType())
				.statementType(mappedStatement.getStatementType());
		MappedStatement query_statement = builder.build();
		DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql);
		stmt = connection.prepareStatement(sqlExplain);
		handler.setParameters(stmt);
		rs = stmt.executeQuery();
		int count = 0;
		while ( rs.next() ) {
			count++;
			String type = rs.getString("type");
			if ( "ALL".equals(type) ) {
				logger.error(boundSql.getSql());
				logger.error("使用了全表扫描的方式!");
			}
			if ( rs.getString("key") == null ) {
				logger.error(boundSql.getSql());
				logger.warn("没有使用索引,可能存在性能问题!");
			}
			int rows = rs.getInt("rows");
			if ( rows > 500 ) {
				logger.error(boundSql.getSql());
				logger.trace("影响行数为" + rows + ",可能存在性能问题!");
			}
			String extra = rs.getString("Extra");
			if ( extra != null && extra.contains("Using temporary") ) {
				logger.error(boundSql.getSql());
				logger.warn("使用临时表,可能存在性能问题!");
			}
			if ( extra != null && extra.contains("Using filesort") ) {
				logger.error(boundSql.getSql());
				logger.warn("使用文件排序,可能存在性能问题!");
			}
		}
		if ( count > 1 ) {
			logger.error(boundSql.getSql());
			logger.error("您的sql语句中用了子查询或者连接查询,为了保证性能和可扩展性,请不要使用子查询或者连接查询,尽量使用n+1查询或者字段冗余!");
		}

	} catch ( Exception e ) {

	} finally {
		if ( rs != null ) {
			rs.close();
			rs = null;
		}
		if ( stmt != null ) {
			stmt.close();
			stmt = null;
		}
	}
}
 
Example #6
Source File: XMLLanguageDriver.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
 public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
   //返回默认的参数处理器
return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
 }
 
Example #7
Source File: VelocityLanguageDriver.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
}
 
Example #8
Source File: XMLLanguageDriver.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Override
 public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
   //返回默认的参数处理器
return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
 }
 
Example #9
Source File: VelocityLanguageDriver.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
  return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
}
 
Example #10
Source File: PaginationInterceptor.java    From belling-admin with Apache License 2.0 2 votes vote down vote up
/**
 * 代入参数值
 * 
 * @param ps
 * @param mappedStatement
 * @param boundSql
 * @param parameterObject
 * @throws SQLException
 */
private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql,
		Object parameterObject) throws SQLException {
	ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
	parameterHandler.setParameters(ps);
}