mybatis 字符串like
时间: 2023-09-30 16:03:46 浏览: 42
MyBatis provides a way to perform string pattern matching using the LIKE operator in SQL. To use it, you can use the `CONCAT` function to concatenate the wildcard characters '%' or '_' with the search string in your SQL query. Here's an example:
```java
@Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{searchString}, '%')")
List<User> searchUsers(@Param("searchString") String searchString);
```
In this example, we're searching for users whose username contains the `searchString`. The '%' characters before and after the parameter ensure that the search is performed as a substring match.
You can also use the wildcard '_' to match any single character. For example:
```java
@Select("SELECT * FROM users WHERE username LIKE CONCAT('_', #{searchString}, '_')")
List<User> searchUsers(@Param("searchString") String searchString);
```
This will search for users whose username has any character before and after the `searchString`.
Remember to properly handle any user input to prevent SQL injection vulnerabilities. MyBatis supports parameterization using `#{}` syntax, which helps with this.