mappedStatement.getBoundSql(parameter).getSql()获取的sql带‘?’怎么办
时间: 2024-06-03 20:11:00 浏览: 4
如果获取的 SQL 带有问号,那么它是一个预编译的 SQL 语句。你需要使用参数对象将问号替换为具体的参数值。可以通过设置参数对象来实现这一点。如果你使用 MyBatis 框架,那么可以使用 `sqlSession.selectList()` 等方法来自动为你处理参数,否则你需要手动解析 SQL 语句并设置参数。
相关问题
mappedStatement.getBoundSql(parameter).getSql()怎么让这个方法的sql没有'?'
对于这个问题,你可以尝试使用参数化查询。在参数化查询中,'?'会被替换为具体的参数值,这样可以避免 SQL 注入攻击,并且也不会影响 SQL 的执行。你可以使用 PreparedStatement 对象来执行参数化查询。为了达到你要求的效果,你需要在 PreparedStatement 中设置每个参数的值,这样就可以避免出现问号。具体实现可以参考 PreparedStatement 的 API。
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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)