mybatis逆向查询使用binary区分大小写
时间: 2023-07-22 08:28:03 浏览: 58
如果需要在 MyBatis 逆向查询中使用 `binary` 进行大小写区分,可以在 SQL 语句中使用 `binary` 函数将列名或参数转换成二进制格式,从而实现大小写区分。
比如,假设有一个表 `user`,其中有一个列 `username`,需要进行大小写区分的查询。可以使用以下 SQL 语句进行查询:
```sql
SELECT * FROM user WHERE BINARY username = BINARY #{username}
```
这里使用了两个 `BINARY` 函数将 `username` 和参数都转换成二进制格式,从而实现了大小写区分。
在 MyBatis Generator 中,可以通过在 `columnOverride` 标签中添加 `javaType` 属性指定该列对应的 Java 类型,并在该 Java 类型中添加一个方法,将字符串转换成二进制格式。例如:
```xml
<table tableName="user">
<columnOverride column="username" javaType="String">
<property name="typeHandler" value="com.example.BinaryStringTypeHandler"/>
</columnOverride>
</table>
```
这里通过设置 `typeHandler` 属性指定了一个自定义的类型处理器 `BinaryStringTypeHandler`,该类型处理器的代码如下:
```java
public class BinaryStringTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, "BINARY " + parameter);
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
```
这里重写了 `setNonNullParameter` 方法,在设置参数时将字符串添加上 `BINARY` 前缀,从而实现了大小写区分。需要注意的是,该方法中的 `ps.setString(i, "BINARY " + parameter)` 语句可能要根据具体数据库的语法进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)