- A+
public void selectBykeyWord(String keyword) {
String id = "%" + keyword + "%";
String roleType = "%" + keyword + "%";
String roleName = "%" + keyword + "%";
userDao.selectBykeyWord(id,roleName,roleType);
}
在Dao层指定各个参数的别名
List<RoleEntity> selectBykeyWord(@Param("id") String id,@Param("roleName") String roleName,@Param("roleType") String roleType);
<select id="selectBykeyWord" parameterType="string" resultType="com.why.mybatis.entity.RoleEntity">
SELECT
*
FROM
t_role
WHERE
role_name LIKE #{roleName}
OR id LIKE #{id}
OR role_type LIKE #{roleType}
</select>
SELECT
*
FROM
t_role
WHERE
role_name LIKE '%why%'
OR id LIKE '%why%'
OR role_type LIKE '%why%';
CONCAT()函数
List<RoleEntity> selectBykeyWord(@Param("keyword") String keyword);
<select id="selectBykeyWord" parameterType="string" resultType="com.why.mybatis.entity.RoleEntity">
SELECT
*
FROM
t_role
WHERE
role_name LIKE CONCAT('%',#{keyword},'%')
OR
id LIKE CONCAT('%',#{keyword},'%')
OR
role_type LIKE CONCAT('%',#{keyword},'%')
</select>
Mybatis的bind
List<RoleEntity> selectBykeyWord(@Param("keyword") String keyword);
<select id="selectBykeyWord" parameterType="string" resultType="com.why.mybatis.entity.RoleEntity">
<bind name="pattern" value="'%' + keyword + '%'" />
SELECT
*
FROM
t_role
WHERE
role_name LIKE #{pattern}
OR
id LIKE #{pattern}
OR
role_type like #{pattern}
</select>