Fuzzy query in MyBatis--like

The first way

In the mapper file

<!--第一种like,java代码中指定like的内容-->
<select id="selectLikeOne" resultType="com.itheima.domain.User">
    select * from user where username like #{name}
</select>

In java code

//需要准备好模糊查询的内容
String name = "%i%";
List<User> users = userDao.selectLikeOne(name);

The query process is as follows

The second way

Concatenate strings in the mapper file

<select id="selectLikeOne2" resultType="com.itheima.domain.User">
    select * from user where username like "%" #{name} "%";
</select>

java code

String name = "i";
List<User> users = userDao.selectLikeOne2(name);

 The query process is as follows

The first method is recommended.

Guess you like

Origin blog.csdn.net/kidchildcsdn/article/details/114168055