mybatis动态sql分页多字段模糊查询。

一。场景:页面搜索涉及到多张表联合查询,且只有1个输入框
例如:在这里插入图片描述
动态mysql

<select id="listPersonPage" resultType="com.vkl.xx.domain.vo.SUserVO">
        select s.id as id,
        s.account as account,
        s.`name` as name,
        s.mobile as mobile,
        r.`NAME`as roleName,
        sd.dept_name as deptName,
        (CASE s.`status`
        WHEN '1' THEN '正常'
        WHEN '2' THEN '冻结'
        END)as `status`,
        s.create_time as createTime
        from s_user s left join sys_dept sd
        on s.dept_id = sd.dept_id
        left join s_role r
        on s.role_id = r.id
        <where>
            <if test="query.roleName != null and query.roleName!='' ">
                r.`NAME` like CONCAT(CONCAT('%',#{query.roleName},'%'))
            </if>
            <if test="query.deptName != null and query.deptName != '' ">
                 and sd.dept_name like CONCAT(CONCAT('%',#{query.deptName},'%'))
            </if>
            <if test="query.status != null and query.status != '' ">
                 and s.`status` like CONCAT(CONCAT('%',#{query.status},'%'))
            </if>
            <if test="query.name != null and query.name != '' ">
            <!--多个字段一个搜索框-->
            and (s.account like CONCAT('%',#{query.name},'%') or s.name like CONCAT('%',#{query.name},'%'))
            </if>
        </where>
        order by s.id desc
        limit #{query.current},#{query.size}
    </select>
@ApiModel("用户管理搜索")
@Data
public class PersonQuery{

    @ApiModelProperty("角色")
    private String roleName;

    @ApiModelProperty("所属部门")
    private String deptName;

    @ApiModelProperty("账号状态 1:正常 2:冻结")
    private String status;

    @ApiModelProperty("用户姓名/账号")
    private String name;

    @ApiModelProperty("页码")
    private Integer current;

    @ApiModelProperty("每页大小")
    private Integer size;
}

你可能感兴趣的:(mybatis,sql)