List of usage examples for org.apache.ibatis.mapping BoundSql getParameterMappings
public List<ParameterMapping> getParameterMappings()
From source file:com.sinotopia.mybatis.pagehelper.PageInterceptor.java
License:Open Source License
/** * ? count //from ww w. ja va2 s. c om * * @param executor * @param countMs * @param parameter * @param boundSql * @param rowBounds * @param resultHandler * @return * @throws IllegalAccessException * @throws SQLException */ private Long executeAutoCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException { Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql); // count key CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql); //? count sql String countSql = dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey); //countKey.update(countSql); BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter); //? SQL ???? BoundSql for (String key : additionalParameters.keySet()) { countBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } // count Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql); Long count = (Long) ((List) countResultList).get(0); return count; }
From source file:com.sjtu.icare.common.persistence.interceptor.SQLHelper.java
License:Open Source License
/** * //from w w w. j a v a 2 s . c om * @param sql SQL? * @param connection ? * @param mappedStatement mapped * @param parameterObject ? * @param boundSql boundSql * @return * @throws SQLException sql */ public static int getCount(final String sql, final Connection connection, final MappedStatement mappedStatement, final Object parameterObject, final BoundSql boundSql, Log log) throws SQLException { // final String countSql = "select count(1) from (" + sql + ") tmp_count"; final String countSql = "select count(1) " + removeSelect(removeOrders(sql)); Connection conn = connection; PreparedStatement ps = null; ResultSet rs = null; try { if (log.isDebugEnabled()) { log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[] { "\n", "\t" }, new String[] { " ", " " })); } if (conn == null) { conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); } ps = conn.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); //?metaParameters.. MetaObject countBsObject = SystemMetaObject.forObject(countBS); MetaObject boundSqlObject = SystemMetaObject.forObject(boundSql); countBsObject.setValue("metaParameters", boundSqlObject.getValue("metaParameters")); SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject); rs = ps.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); } return count; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } }
From source file:com.springD.framework.persistence.interceptor.PaginationInterceptor.java
License:Open Source License
@Override public Object intercept(Invocation invocation) throws Throwable { final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; // //?SQL // if (mappedStatement.getId().matches(_SQL_PATTERN)) { // if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) { Object parameter = invocation.getArgs()[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); Object parameterObject = boundSql.getParameterObject(); //??// w w w . j av a 2s. c o m Page<Object> page = null; if (parameterObject != null) { page = convertParameter(parameterObject, page); } // if (page != null && page.getPageSize() != -1) { if (org.apache.commons.lang3.StringUtils.isBlank(boundSql.getSql())) { return null; } String originalSql = boundSql.getSql().trim(); // page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log)); // ?? String pageSql = SQLHelper.generatePageSql(originalSql, page, DIALECT); // if (log.isDebugEnabled()) { // log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", "")); // } invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT); BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql)); invocation.getArgs()[0] = newMs; } // } return invocation.proceed(); }
From source file:com.springD.framework.persistence.interceptor.SQLHelper.java
License:Open Source License
/** * //from w w w. ja v a 2 s.co m * @param sql SQL? * @param connection ? * @param mappedStatement mapped * @param parameterObject ? * @param boundSql boundSql * @return * @throws SQLException sql */ public static int getCount(final String sql, final Connection connection, final MappedStatement mappedStatement, final Object parameterObject, final BoundSql boundSql, Log log) throws SQLException { String tmpSql = sql.replaceAll("(?i)order by.+\\z", ""); String countSql = "select count(1) from (" + tmpSql + ") tmp_count"; // final String countSql = "select count(1) " + removeSelect(removeOrders(sql)); Connection conn = connection; PreparedStatement ps = null; ResultSet rs = null; try { if (log.isDebugEnabled()) { log.debug("COUNT SQL: " + org.apache.commons.lang3.StringUtils.replaceEach(countSql, new String[] { "\n", "\t" }, new String[] { " ", " " })); } if (conn == null) { conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); } ps = conn.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject); rs = ps.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); } return count; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } }
From source file:com.tj.mybatisplus.plugins.PaginationInterceptor.java
License:Apache License
/** * ?// w ww. java2 s. co m * * @param sql * @param connection * @param mappedStatement * @param boundSql * @param page */ public Pagination count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql, Pagination page) { String sqlUse = sql; int order_by = sql.toUpperCase().lastIndexOf("ORDER BY"); if (order_by > -1) { sqlUse = sql.substring(0, order_by); } StringBuffer countSql = new StringBuffer("SELECT COUNT(1) AS TOTAL FROM ("); countSql.append(sqlUse).append(") A"); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement(countSql.toString()); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql.toString(), boundSql.getParameterMappings(), boundSql.getParameterObject()); ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), countBS); parameterHandler.setParameters(pstmt); rs = pstmt.executeQuery(); int total = 0; if (rs.next()) { total = rs.getInt(1); } page.setTotal(total); /** * ?? */ if (page.getCurrent() > page.getPages()) { page = new Pagination(1, page.getSize()); page.setTotal(total); } } catch (SQLException e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } return page; }
From source file:com.wbsf.core.mybatis.mook.locker.interceptor.OptimisticLocker.java
License:Open Source License
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Object intercept(Invocation invocation) throws Exception { String versionColumn;/*from w w w .j a v a 2 s. c om*/ if (null == props || props.isEmpty()) { versionColumn = "version"; } else { versionColumn = props.getProperty("versionColumn", "version"); } String interceptMethod = invocation.getMethod().getName(); if ("prepare".equals(interceptMethod)) { StatementHandler routingHandler = (StatementHandler) PluginUtil.processTarget(invocation.getTarget()); MetaObject routingMeta = SystemMetaObject.forObject(routingHandler); MetaObject hm = routingMeta.metaObjectForProperty("delegate"); VersionLocker vl = VersionLockerResolver.resolve(hm); if (null != vl && !vl.value()) { return invocation.proceed(); } String originalSql = (String) hm.getValue("boundSql.sql"); StringBuilder builder = new StringBuilder(originalSql); builder.append(" AND "); builder.append(versionColumn); builder.append(" = ?"); hm.setValue("boundSql.sql", builder.toString()); } else if ("setParameters".equals(interceptMethod)) { ParameterHandler handler = (ParameterHandler) PluginUtil.processTarget(invocation.getTarget()); MetaObject hm = SystemMetaObject.forObject(handler); VersionLocker vl = VersionLockerResolver.resolve(hm); if (null != vl && !vl.value()) { return invocation.proceed(); } BoundSql boundSql = (BoundSql) hm.getValue("boundSql"); Object parameterObject = boundSql.getParameterObject(); if (parameterObject instanceof MapperMethod.ParamMap<?>) { MapperMethod.ParamMap<?> paramMap = (MapperMethod.ParamMap<?>) parameterObject; if (!paramMap.containsKey(versionColumn)) { throw new TypeException( "All the primitive type parameters must add MyBatis's @Param Annotaion"); } } Configuration configuration = ((MappedStatement) hm.getValue("mappedStatement")).getConfiguration(); MetaObject pm = configuration.newMetaObject(parameterObject); Object value = pm.getValue(versionColumn); ParameterMapping versionMapping = new ParameterMapping.Builder(configuration, versionColumn, Object.class).build(); TypeHandler typeHandler = versionMapping.getTypeHandler(); JdbcType jdbcType = versionMapping.getJdbcType(); if (value == null && jdbcType == null) { jdbcType = configuration.getJdbcTypeForNull(); } int versionLocation = boundSql.getParameterMappings().size() + 1; try { PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0]; typeHandler.setParameter(ps, versionLocation, value, jdbcType); } catch (TypeException | SQLException e) { throw new TypeException("set parameter 'version' faild, Cause: " + e, e); } if (value.getClass() != Long.class && value.getClass() != long.class) { if (log.isDebugEnabled()) { log.error(Constent.LogPrefix + "property type error, the type of version property must be Long or long."); } } // increase version pm.setValue(versionColumn, (long) value + 1); } return invocation.proceed(); }
From source file:com.wsun.seap.dao.interceptor.PaginationInterceptor.java
License:Open Source License
@Override public Object intercept(Invocation invocation) throws Throwable { // ??//from w w w.j a va 2s. co m final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[MAPPED_STATEMENT_INDEX]; // ?SQL // ?? Object parameterObj = invocation.getArgs()[PARAMETER_INDEX]; // ??QueryParam Object parameter; if (parameterObj instanceof QueryParam) { // ?QueryParam,??Map parameter = ((QueryParam) parameterObj).getAllParam(); invocation.getArgs()[PARAMETER_INDEX] = parameter; } else { parameter = parameterObj; } BoundSql boundSql = mappedStatement.getBoundSql(parameter); if (StringUtils.isBlank(boundSql.getSql())) { return null; } // ??RowBounds RowBounds rowBounds = (RowBounds) invocation.getArgs()[ROWBOUNDS_INDEX]; // if (rowBounds != null && rowBounds != RowBounds.DEFAULT) { String originalSql = boundSql.getSql().trim(); // ??sql String pageSql = dialect.getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit()); invocation.getArgs()[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT); BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql)); invocation.getArgs()[MAPPED_STATEMENT_INDEX] = newMs; } return invocation.proceed(); }
From source file:com.wsun.seap.dao.interceptor.SQLHelper.java
License:Open Source License
/** * //w ww.j a v a2 s. c om * @param sql SQL? * @param mappedStatement mapped * @param parameterObject ? * @param boundSql boundSql * @return * @throws java.sql.SQLException sql */ public static int getCount(final String sql, final MappedStatement mappedStatement, final Object parameterObject, final BoundSql boundSql) throws SQLException { final String countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count"; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); ps = conn.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject); rs = ps.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); } return count; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } }
From source file:com.xiaogua.web.util.MybatisSqlHelper.java
License:Open Source License
/** /*from w ww . j a va 2 s . co m*/ * ????sql * * @param session * @param namespace * @param params * @return */ public static String getNameSpaceSql(SqlSession session, String namespace, Object params) { params = wrapCollection(params); Configuration configuration = session.getConfiguration(); MappedStatement mappedStatement = configuration.getMappedStatement(namespace); TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry(); BoundSql boundSql = mappedStatement.getBoundSql(params); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql(); if (parameterMappings != null) { for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (params == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) { value = params; } else { MetaObject metaObject = configuration.newMetaObject(params); value = metaObject.getValue(propertyName); } JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull(); sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType()); } } } return sql; }
From source file:com.zrx.authority.dao.plugin.PagePlugin.java
public Object intercept(Invocation ivk) throws Throwable { // TODO Auto-generated method stub if (ivk.getTarget() instanceof RoutingStatementHandler) { RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper .getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); if (mappedStatement.getId().matches(pageSqlId)) { //?SQL BoundSql boundSql = delegate.getBoundSql(); Object parameterObject = boundSql.getParameterObject();//SQL<select>parameterType??Mapper??,?? if (parameterObject == null) { throw new NullPointerException("parameterObject?"); } else { Connection connection = (Connection) ivk.getArgs()[0]; String sql = boundSql.getSql(); //String countSql = "select count(0) from (" + sql+ ") as tmp_count"; // String countSql = "select count(0) from (" + sql + ") tmp_count"; // == oracle as (SQL command not properly ended) PreparedStatement countStmt = connection.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); setParameters(countStmt, mappedStatement, countBS, parameterObject); ResultSet rs = countStmt.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); }// w ww . j a v a2 s . c o m rs.close(); countStmt.close(); //System.out.println(count); Page page = null; if (parameterObject instanceof Page) { //?Page page = (Page) parameterObject; page.setEntityOrField(true); //?com.jalan.entity.Page.entityOrField page.setTotalResult(count); } else { //??Page Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "page"); if (pageField != null) { page = (Page) ReflectHelper.getValueByFieldName(parameterObject, "page"); if (page == null) page = new Page(); page.setEntityOrField(false); //?com.jalan.entity.Page.entityOrField page.setTotalResult(count); ReflectHelper.setValueByFieldName(parameterObject, "page", page); //?? } else { throw new NoSuchFieldException( parameterObject.getClass().getName() + "? page ?"); } } String pageSql = generatePageSql(sql, page); ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); //sql???BoundSql. } } } return ivk.proceed(); }