本文整理汇总了Java中org.apache.ibatis.executor.statement.RoutingStatementHandler类的典型用法代码示例。如果您正苦于以下问题:Java RoutingStatementHandler类的具体用法?Java RoutingStatementHandler怎么用?Java RoutingStatementHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RoutingStatementHandler类属于org.apache.ibatis.executor.statement包,在下文中一共展示了RoutingStatementHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
Object obj = boundSql.getParameterObject();
Page page = seekPage(obj);
if (page != null) {
MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
Connection connection = (Connection) invocation.getArgs()[0];
String sql = boundSql.getSql();
this.setTotalRecord(page, mappedStatement, connection);
String pageSql = this.getPageSql(page, sql);
ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
}
return invocation.proceed();
}
示例2: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
if (invocation.getTarget() instanceof RoutingStatementHandler) {
StatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
String mapperId = mappedStatement.getId();
String mapperClassName = mapperId.substring(0, mapperId.lastIndexOf("."));
Class<?> mapperClass = Class.forName(mapperClassName);
if (isDaoMapper(mapperClass)&&mapperId.endsWith(".insertUseReturnGeneratedKeys")) {
BaseStatementHandler baseStatementHandler = (BaseStatementHandler) metaStatementHandler.getValue("delegate");
if ("query".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerQuery(invocation, baseStatementHandler);
} else if ("prepare".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerPrepare(invocation, baseStatementHandler);
}
}
}
return invocation.proceed();
}
示例3: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
/**
* 拦截后要执行的方法
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
Page<?> page = PageThreadLocal.getThreadLocalPage();
if (page == null) {
return invocation.proceed();
}
PageThreadLocal.removeThreadLocalPage();
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
// 通过反射获取到当前RoutingStatementHandler对象的delegate属性
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
// 获取当前要执行的Sql语句,也就是我们直接在Mapper映射语句中写的Sql语句
String sql = boundSql.getSql();
// 是否查询总页数和总数据 默认为TRUE
if (page.getTotalFlag()) {
// 给当前的page参数对象设置总记录数
this.setTotalRecord(page, mappedStatement, boundSql, sql);
}
String pageSql = this.getPageSql(sql, page);
// 利用反射设置当前BoundSql对应的sql属性为我们建立好的分页Sql语句
ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
return invocation.proceed();
}
示例4: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MappedStatement mappedStatement = null;
if (statementHandler instanceof RoutingStatementHandler) {
StatementHandler delegate = (StatementHandler) ReflectionUtils.getFieldValue(statementHandler, "delegate");
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(delegate, "mappedStatement");
} else {
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(statementHandler, "mappedStatement");
}
Object parm = statementHandler.getParameterHandler().getParameterObject();
String sql = statementHandler.getBoundSql().getSql();
String mapperId = mappedStatement.getId();
if (ShardHolder.getInstance().hasShard(mapperId)) {
ShardObject shardObject = ShardHolder.getInstance().getShardObject(mapperId);
String targetSql = ShardParser.getInstance().parse(sql, parm, shardObject);
if (!sql.equals(targetSql)) {
ReflectionUtils.setFieldValue(statementHandler.getBoundSql(), "sql", targetSql);
}
}
return invocation.proceed();
}
示例5: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
if(statementHandler instanceof RoutingStatementHandler){
RoutingStatementHandler routeStatementHandler = (RoutingStatementHandler)statementHandler;
MetaObject metaObject = SystemMetaObject.forObject(routeStatementHandler);
int offset = Integer.valueOf(String.valueOf(metaObject.getValue("delegate.rowBounds.offset")));
int limit = Integer.valueOf(String.valueOf(metaObject.getValue("delegate.rowBounds.limit")));
if(offset == RowBounds.NO_ROW_OFFSET && limit == RowBounds.NO_ROW_LIMIT){
return invocation.proceed();
}
String sql = String.valueOf(metaObject.getValue("delegate.boundSql.sql")).trim();
if(StringUtils.isBlank(sql) || !sql.toLowerCase().startsWith("select")){
return invocation.proceed();
}
String pagedSql = this.buildPagedSql(sql, offset, limit);
metaObject.setValue("delegate.boundSql.sql", pagedSql);
metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
}else{
return invocation.proceed();
}
return invocation.proceed();
}
示例6: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MappedStatement mappedStatement;
if (statementHandler instanceof RoutingStatementHandler) {
StatementHandler delegate = ReflectionUtil.getFieldValue(statementHandler, "delegate");
mappedStatement = ReflectionUtil.getFieldValue(delegate, "mappedStatement");
} else {
mappedStatement = ReflectionUtil.getFieldValue(statementHandler, "mappedStatement");
}
// 读写分离设置使用哪个数据源
Boolean readOnly = Boolean.FALSE;
if (mappedStatement.getSqlCommandType() == SqlCommandType.SELECT) {
final String sql = statementHandler.getBoundSql().getSql();
String s = sql.toLowerCase();
readOnly = !(s.contains("last_insert_id()") || s.contains("row_count()"));
}
READ_ONLY_LOCAL.set(readOnly);
return invocation.proceed();
}
示例7: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
public Object intercept(Invocation ivk) throws Throwable {
if (ivk.getTarget() instanceof RoutingStatementHandler) {
RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
BaseStatementHandler delegate = ReflectUtil.getValueByFieldName(statementHandler, "delegate");
MappedStatement mappedStatement = ReflectUtil.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];
Page page = getPage(parameterObject);
page.setTotalResult(getTotalResult(statementHandler, connection));
if (page.hasSumColumns() && page.lessThanMaxSumLimit()) {
page.setSumResult(getSumResult(statementHandler, connection, page));
}
String pageSql = generatePageSql(boundSql.getSql(), page);
ReflectUtil.setValueByFieldName(boundSql, "sql", pageSql); //将分页sql语句反射回BoundSql.
}
}
}
return ivk.proceed();
}
示例8: getTotalResult
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
private int getTotalResult(RoutingStatementHandler statementHandler, Connection connection) {
int totalResult = 0;
try {
BaseStatementHandler delegate = ReflectUtil.getValueByFieldName(statementHandler, "delegate");
MappedStatement mappedStatement = ReflectUtil.getValueByFieldName(delegate, "mappedStatement");
BoundSql boundSql = delegate.getBoundSql();
Object parameterObject = boundSql.getParameterObject();
String sql = boundSql.getSql();
String countSql = "select count(0) from (" + sql + ") tmp_count"; //记录统计
PreparedStatement countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject);
setParameters(countStmt, mappedStatement, countBS, parameterObject);
ResultSet rs = countStmt.executeQuery();
if (rs.next()) {
totalResult = rs.getInt(1);
}
rs.close();
countStmt.close();
} catch (SQLException e) {
logger.error(ApplicationConstant.ERROR, e);
}
return totalResult;
}
示例9: getSumResult
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
private Map<String, String> getSumResult(RoutingStatementHandler statementHandler, Connection connection, Page page) throws SQLException {
Map<String, String> sumResultMap = new HashMap<String, String>();
try {
BaseStatementHandler delegate = ReflectUtil.getValueByFieldName(statementHandler, "delegate");
MappedStatement mappedStatement = ReflectUtil.getValueByFieldName(delegate, "mappedStatement");
BoundSql boundSql = delegate.getBoundSql();
Object parameterObject = boundSql.getParameterObject();
String sql = boundSql.getSql();
String sumSql = getSumSql(sql, page);
PreparedStatement sumStatement = connection.prepareStatement(sumSql);
BoundSql sumBoundSql = new BoundSql(mappedStatement.getConfiguration(), sumSql, boundSql.getParameterMappings(), parameterObject);
setParameters(sumStatement, mappedStatement, sumBoundSql, parameterObject);
ResultSet rs = sumStatement.executeQuery();
if (rs.next()) {
for (String sumColumn : page.getSumColumns()) {
sumResultMap.put(sumColumn, rs.getString(sumColumn));
}
}
rs.close();
sumStatement.close();
page.setSumResult(sumResultMap);
} catch (SQLException e) {
logger.error(ApplicationConstant.ERROR, e);
}
return sumResultMap;
}
示例10: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
Pagination pagination = getPaginationParamter(invocation);
if (pagination == null) {
return invocation.proceed();
}
BoundSql boundSql = handler.getBoundSql();
// 获取分页sql,从hibernate拷贝而来(mysql/oracle/db2/h2/sqlserver)
String sql = dialect.getLimitString(boundSql.getSql(), pagination);
try {
Field sqlField = boundSql.getClass().getDeclaredField("sql");
if (sqlField != null) {
sqlField.setAccessible(true);
sqlField.set(boundSql, sql);
}
} catch (Exception e) {
logger.error(e);
}
return invocation.proceed();
}
示例11: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MappedStatement mappedStatement = null;
if (statementHandler instanceof RoutingStatementHandler) {
StatementHandler delegate = (StatementHandler) ReflectionUtils.getFieldValue(statementHandler, "delegate");
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(delegate, "mappedStatement");
} else {
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(statementHandler, "mappedStatement");
}
String mapperId = mappedStatement.getId();
Object params = statementHandler.getBoundSql().getParameterObject();
handleTableName(mapperId, statementHandler, params);
handleDataSource(mapperId);
try {
return invocation.proceed();
} finally {
DataSourceContextHolder.clearDataSourceName();
}
}
示例12: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation ivk) throws Throwable {
if (ivk.getTarget().getClass()
.isAssignableFrom(RoutingStatementHandler.class)) {
final RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk
.getTarget();
final BaseStatementHandler delegate = (BaseStatementHandler) Reflection
.getFieldValue(statementHandler, DELEGATE);
final MappedStatement mappedStatement = (MappedStatement) Reflection
.getFieldValue(delegate, MAPPED_STATEMENT);
if (mappedStatement.getId().matches(SQL_PATTERN)) {
// 拦截需要分页的SQL
BoundSql boundSql = delegate.getBoundSql();
// 分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
Object parameterObject = boundSql.getParameterObject();
if (null == parameterObject) {
throw new NullPointerException("parameterObject尚未实例化!");
} else {
final Connection connection = (Connection) ivk.getArgs()[0];
final String sql = boundSql.getSql();
// 记录统计
final int count = BaseParameter
.getCount(sql, connection, mappedStatement,
parameterObject, boundSql, DIALECT);
Page page = null;
page = convertParameter(parameterObject, page);
page.init(count, page.getSize(), page.getLimit());
String pagingSql = BaseParameter.generatePageSql(sql, page,
DIALECT);
// 将分页sql语句反射回BoundSql.
Reflection.setFieldValue(boundSql, "sql", pagingSql);
}
}
}
return ivk.proceed();
}
示例13: plugin
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object plugin(Object target) {
// TODO Auto-generated method stub
if (target instanceof RoutingStatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
示例14: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
@Override
public Object intercept(Invocation ivk) throws Throwable {
if (ivk.getTarget().getClass().isAssignableFrom(RoutingStatementHandler.class)) {
final RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
final BaseStatementHandler delegate = (BaseStatementHandler) Reflections.getFieldValue(statementHandler, DELEGATE);
final MappedStatement mappedStatement = (MappedStatement) Reflections.getFieldValue(delegate, MAPPED_STATEMENT);
// //拦截需要分页的SQL
//// if (mappedStatement.getId().matches(_SQL_PATTERN)) {
// if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
BoundSql boundSql = delegate.getBoundSql();
//分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
Object parameterObject = boundSql.getParameterObject();
if (parameterObject == null) {
log.error("参数未实例化");
throw new NullPointerException("parameterObject尚未实例化!");
} else {
final Connection connection = (Connection) ivk.getArgs()[0];
final String sql = boundSql.getSql();
//记录统计
final int count = SQLHelper.getCount(sql, connection, mappedStatement, parameterObject, boundSql, log);
Page<Object> page = null;
page = convertParameter(parameterObject, page);
page.setCount(count);
String pagingSql = SQLHelper.generatePageSql(sql, page, DIALECT);
if (log.isDebugEnabled()) {
log.debug("PAGE SQL:" + pagingSql);
}
//将分页sql语句反射回BoundSql.
Reflections.setFieldValue(boundSql, "sql", pagingSql);
}
if (boundSql.getSql() == null || "".equals(boundSql.getSql())){
return null;
}
}
// }
return ivk.proceed();
}
示例15: intercept
import org.apache.ibatis.executor.statement.RoutingStatementHandler; //导入依赖的package包/类
public Object intercept(Invocation invocation) throws Throwable {
if (invocation.getTarget() instanceof RoutingStatementHandler) {
RoutingStatementHandler routingStatementHandler = (RoutingStatementHandler) invocation
.getTarget();
StatementHandler delegate = (StatementHandler) ReflectHelper
.getValueByFieldName(routingStatementHandler, "delegate");
if (delegate instanceof PreparedStatementHandler) {
PreparedStatementHandler preparedStatementHandler = (PreparedStatementHandler) delegate;
MappedStatement mappedStatement = (MappedStatement) ReflectHelper
.getValueByFieldName(preparedStatementHandler,
"mappedStatement");
BoundSql boundSql = (BoundSql) ReflectHelper
.getValueByFieldName(preparedStatementHandler,
"boundSql");
Object parameterObject = boundSql.getParameterObject();
if (parameterObject instanceof BatchInsertParameter) {
PreparedStatement ps = (PreparedStatement) invocation
.getArgs()[0];
int rows = ps.getUpdateCount();
Executor executor = (Executor) ReflectHelper
.getValueByFieldName(preparedStatementHandler,
"executor");
KeyGenerator keyGenerator = mappedStatement
.getKeyGenerator();
keyGenerator.processAfter(executor, mappedStatement, ps,
parameterObject);
return rows;
}
}
}
return invocation.proceed();
}