天天看点

Mybatisplus 自定义sql 使用条件构造器(分页)

动态查找:

@Select("select ${ew.SqlSelect} from ${tableName} ${ew.customSqlSegment}")
List<File> listFileByCondition(@Param("tableName") String tableName, @Param("ew") Wrapper wrapper);

ew.SqlSelect:所需要查找的字段

tableName:使用的是那张表

ew.customSqlSegment:条件
用法:allFileMapper.listFileByCondition(tableName,Wrappers.query().select("*").in("relation_uuid", uuids));
结果: select * from tablName where relation_uuid in ()
           

动态修改:

@Update("update ${tableName} set ${ew.sqlSet} ${ew.customSqlSegment}")
int updateByCondition(@Param("tableName") String tableName, @Param("ew") Wrapper wrapper);

ew.sqlSet:修改的字段

tableName:使用的是那张表

ew.customSqlSegment:条件

用法:
mapper.updateByCondition(tableName, Wrappers.update().set("state", "初始状态").in("id", ids));
结果: update tableName set state = '初始状态' where id in ()
           

xml查找带分页//只要把page放在第一个位置,不用处理,myBatis-plus会帮我们处理分页

mapper.xml文件用法:
Page<File> selectPage(Page page, @Param("tableName") String tableName, @Param("ew") Wrapper wrapper);

<select id="selectPage" resultType="com.example.entity.File">
        select * from ${tableName} ${ew.customSqlSegment}
</select>
           

普通分页

Controller层

@RequestMapping("/api/getFloorPage")
public IPage<Floor> getPage(@RequestParam("currentPage")int currentPage, @RequestParam("pageSize") int pageSize){
    return floorService.selectByPage(currentPage,pageSize);
}
           

实现接口类

@Autowired
    private FloorMapper floorMapper;

   @Override
    public IPage<Floor> selectByPage(int currentPage, int pageSize) {
        //查询对象
        QueryWrapper<Floor> wrapper = new QueryWrapper<>();
        //分页  第一个参数为,查询第几页,第二个参数为 每页多少条记录
        Page<Floor> page = new Page<>(currentPage,pageSize);
        //
        IPage<Floor> floorIPage = floorMapper.selectPage(page,wrapper);
        return floorIPage;
    }
           

前端

继续阅读