mybatis - [07] 模糊查詢

HOUHUILIN發表於2024-06-05

題記部分

(1)mapper類

List<User> getUserLike(String value);

(2)mapper.xml

<!-- 寫法1 -->
<select id="getUserLike" resultType="com.harley.pojo.User">
    select * from user where name like #{value}
</select>

<!-- 寫法2 -->
<select id="getUserLike" resultType="com.harley.pojo.User">
    select * from user where name like '%${value}%'
</select>

(3)測試類

程式碼執行的時候使用萬用字元%keyword%

// 寫法1
@Test
public void getUserLike(){
    List<User> userList = userMapper.getUserLike("%日%");

    for (User user : userList) {
        System.out.println(user.toString());
    }
}

// 寫法2
@Test
public void getUserLike(){
    List<User> userList = userMapper.getUserLike("日");

    for (User user : userList) {
        System.out.println(user.toString());
    }
}

mybatis - [07] 模糊查詢

— 業精於勤荒於嬉,行成於思毀於隨 —

相關文章