MyBatis Mapper Interface: Query Employee List by Name Keyword
MyBatis Mapper Interface: Query Employee List by Name Keyword
Problem:
Jordan has created a MyBatis application and added the following Mapper interface:
public interface EmployeeMapper {
List<Employee> getListByNameKeyword(String keyword);
}
He needs help selecting the correct mapping code snippet to implement the functionality of querying employee list based on a name keyword.
Solution:
The correct mapping code snippet is:
<select id="getListByNameKeyword" resultType="list">
select * from employee where name like '%'||#{keyword}||'%'
</select>
Explanation:
- This code snippet uses the
%wildcard characters to search for any employee name containing the keyword specified in the parameter. - The
||operator is used to concatenate the wildcard characters with the parameter value. - The
resultTypeis set to "list" as the method signature specifies that the return type is a list of employees.
Key Points:
- Use the
%wildcard character for partial matching. - Use the
||operator for concatenation in MyBatis SQL statements. - Ensure the
resultTypematches the method's return type.
By using this code snippet, Jordan can effectively implement the functionality to query employee lists based on a name keyword in his MyBatis application.
原文地址: https://www.cveoy.top/t/topic/oCE7 著作权归作者所有。请勿转载和采集!