pom.xml加入依赖,版本2.3.3
<!-- MybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3.3</version>
</dependency>
controller中模糊查询的方法,Page类导入mybatisplus包中的
@RequestMapping(value = "/staffFuzzyQuery", method = RequestMethod.GET)
@ApiOperation(value = "员工模糊查询")
public Object staffFuzzyQuery(String accessToken, @ModelAttribute Staff staff, @ModelAttribute PageVo pageVo) {
Page staffList = staffService.staffFuzzyQuery(staff, pageVo);
return staffList;
}
对应service的方法
/**
* @Description: 员工模糊查询
* @auther: Hanweihu
* @date: 16:52 2019/3/26
* @param: [staff, pageNo, pageSize]
* @return: com.baomidou.mybatisplus.plugins.Page
*/
public Page staffFuzzyQuery(Staff staff, PageVo pageVo) {
Wrapper<Staff> entity = new EntityWrapper<>(staff);
Page<Staff> page = new Page<>(pageVo.getPageNumber(), pageVo.getPageSize());
List<Staff> staffList = staffMapper.fuzzyQuery(page, staff);
return page.setRecords(staffList);
}
Wrapper类,Page类都导入mybatisplus包中的。Staff是你的实体类(即要查的表对应的实体类)。staffMapper是你的mapper接口。PageVo是一个我自定义的封装类,下面会贴出。
PageVo
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class PageVo implements Serializable{
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "页号")
private int pageNumber;
@ApiModelProperty(value = "页面大小")
private int pageSize;
@ApiModelProperty(value = "排序字段")
private String sort;
@ApiModelProperty(value = "排序方式 asc/desc")
private String order;
}
@Data注解是lombok的,需安装插件,可百度,不再赘述
Mapper接口
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface StaffMapper extends BaseMapper<Staff> {
// 模糊查询
List<Staff> fuzzyQuery(Page<Staff> page, @Param("staff") Staff staff);
}
mapper.xml
<select id="fuzzyQuery" resultType="你的实体类Reference" parameterType="你的实体类Reference">
SELECT * FROM sz_staff
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test='staff.userName!=null and staff.userName!=" "'>
AND USER_NAME like CONCAT('%',#{staff.userName},'%')
</if>
<if test='staff.phone!=null and staff.phone!=" "'>
AND PHONE like CONCAT('%',#{staff.phone},'%')
</if>
<if test='staff.belongFirm!=null and staff.belongFirm!=" "'>
AND BELONG_FIRM like CONCAT('%',#{staff.belongFirm},'%')
</if>
<if test='staff.positionId!=null and staff.positionId!=" "'>
AND POSITION_ID=#{staff.positionId}
</if>
<if test='staff.superLeader!=null and staff.superLeader!=" "'>
AND SUPER_LEADER like CONCAT('%',#{staff.superLeader},'%')
</if>
<if test='staff.status!=null and staff.status!=" "'>
AND STATUS=#{staff.status}
</if>
<if test='staff.isSupportPeople!=null and staff.isSupportPeople!=" "'>
AND IS_SUPPORT_PEOPLE=#{staff.isSupportPeople}
</if>
</trim>
</select>
xml中的if标签可根据你的业务情况编写。test判断时原来写法,是双引号,但执行判断时会报错,把test的双引号变为单引号,即可解决报错。
swagger-ui测试
看上面controller,我有两个参数对象,staff实体类,我只给userName传了“明”,pagevo封装类只传了第几页,一页几条。
测试结果