mybatis 拦截器如何通过Invocation获取表的主键
时间: 2024-05-21 10:12:30 浏览: 15
Mybatis拦截器可以通过获取到的Invocation对象,使用反射获取MappedStatement对象。通过MappedStatement对象可以获取到执行SQL的操作类型,以及执行SQL语句所涉及的参数信息。从而可以根据需要自定义绑定参数、转换结果集等操作。如果需要获取表的主键,可以通过MappedStatement对象获取执行的SQL语句,然后使用正则表达式或者其他方式解析SQL语句中的表名和主键信息。
相关问题
mybatis 如何通过Invocation获取表的主键
可以通过以下步骤获取表的主键:
1. 在 Mybatis 的 Mapper 文件中给出 SQL 语句:SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME=#{tableName} AND CONSTRAINT_SCHEMA=#{databaseName} AND CONSTRAINT_NAME='PRIMARY'.
2. 在对应的 Mapper 接口中编写方法,方法的输入参数为表名 tableName 和数据源名称 databaseName。
3. 在 SqlSessionInterceptor 中修改 Invocation 对象,将参数传递给对应的 Mapper 方法。
4. 在 Mapper 方法中调用 SQL 语句,并将结果作为 String 返回。
mybatis 拦截器如何通过Invocation获取完整sql
语句并进行修改?
Mybatis拦截器可以通过Invocation对象获取完整的SQL语句并进行修改。在拦截器的intercept方法中,可以获取到方法调用的相关信息,包括Mapper接口、方法名、方法参数等。通过这些信息,可以获取到BoundSql对象,它包含了原始的SQL语句和参数信息。可以通过BoundSql对象获取完整的SQL语句并进行修改,然后再将修改后的SQL语句设置回BoundSql对象即可。
具体实现可以参考以下代码:
```
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement) args[0];
Object parameterObject = args[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
String sql = boundSql.getSql();
// 在这里对SQL语句进行修改
String newSql = modifySql(sql);
// 将修改后的SQL语句重新设置回BoundSql对象中
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), newSql, boundSql.getParameterMappings(), parameterObject);
MappedStatement newMappedStatement = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
args[0] = newMappedStatement;
return invocation.proceed();
}
private String modifySql(String sql) {
// 在这里可以对SQL语句进行修改,例如添加分页的limit条件
return "select * from (" + sql + ") limit 0, 10";
}
private MappedStatement copyFromMappedStatement(MappedStatement mappedStatement, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(mappedStatement.getConfiguration(), mappedStatement.getId(), newSqlSource, mappedStatement.getSqlCommandType());
builder.resource(mappedStatement.getResource());
builder.fetchSize(mappedStatement.getFetchSize());
builder.statementType(mappedStatement.getStatementType());
builder.keyGenerator(mappedStatement.getKeyGenerator());
builder.keyProperty(mappedStatement.getKeyProperty());
builder.timeout(mappedStatement.getTimeout());
builder.parameterMap(mappedStatement.getParameterMap());
builder.resultMaps(mappedStatement.getResultMaps());
builder.cache(mappedStatement.getCache());
builder.useCache(mappedStatement.isUseCache());
return builder.build();
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)