根据条件查询列表
mybatisplus方式:
dao层:
package com.luoyong.mybatisplus.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 相册
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("pms_album")
public class Album implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 记录id
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 相册名称
*/
@TableField("name")
private String name;
/**
* 相册简介
*/
@TableField("description")
private String description;
/**
* 自定义排序序号
*/
@TableField("sort")
private Integer sort;
/**
* 数据创建时间
*/
@TableField("gmt_create")
private LocalDateTime gmtCreate;
/**
* 数据最后修改时间
*/
@TableField("gmt_modified")
private LocalDateTime gmtModified;
}
DTO层:
package com.luoyong.mybatisplus.pojo.dto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 相册
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("pms_album")
public class AlbumDto implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 记录id
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 相册名称
*/
@TableField("name")
private String name;
/**
* 相册简介
*/
@TableField("description")
private String description;
/**
* 自定义排序序号
*/
@TableField("sort")
private Integer sort;
/**
* 数据创建时间
*/
@TableField("gmt_create")
private LocalDateTime gmtCreate;
/**
* 数据最后修改时间
*/
@TableField("gmt_modified")
private LocalDateTime gmtModified;
}
mapper层:
package com.luoyong.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoyong.mybatisplus.pojo.Album;
import com.luoyong.mybatisplus.pojo.dto.AlbumDto;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* <p>
* 相册 Mapper 接口
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
@Repository
public interface AlbumMapper extends BaseMapper<Album> {
}
service层
package com.luoyong.mybatisplus.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.luoyong.mybatisplus.pojo.Album;
import com.luoyong.mybatisplus.pojo.dto.AlbumDto;
/**
* <p>
* 相册 服务类
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
public interface AlbumService extends IService<Album> {
/**
* 根据条件查询相册列表
* @param albumDto
* @return
*/
IPage<Album> selectList(AlbumDto albumDto);
}
serviceimpl层
package com.luoyong.mybatisplus.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyong.mybatisplus.mapper.AlbumMapper;
import com.luoyong.mybatisplus.pojo.Album;
import com.luoyong.mybatisplus.pojo.dto.AlbumDto;
import com.luoyong.mybatisplus.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* <p>
* 相册 服务实现类
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {
@Autowired
AlbumMapper albumMapper;
/**
* 根据条件查询相册列表
* @param albumDto
* @return
*/
@Override
public IPage<Album> selectList(AlbumDto albumDto) {
log.debug("开始处理【相册分页查询】的业务");
Page page=new Page();
page.getCurrent();
page.getSize();
QueryWrapper<Album> queryWrapper=new QueryWrapper<>();
queryWrapper.eq(StringUtils.isNotBlank(albumDto.getName()), "name", albumDto.getName());
queryWrapper.eq(StringUtils.isNotBlank(albumDto.getDescription()),"description",albumDto.getDescription());
queryWrapper.orderByDesc("gmt_create");
IPage<Album> list = albumMapper.selectPage(page, queryWrapper);
return list;
}
}
controller层:
package com.luoyong.mybatisplus.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoyong.mybatisplus.pojo.Album;
import com.luoyong.mybatisplus.pojo.dto.AlbumDto;
import com.luoyong.mybatisplus.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 相册 前端控制器
* </p>
*
* @author cn.luoyong
* @since 2023-05-13
*/
@RestController
@RequestMapping("/Albums")
public class AlbumController {
@Autowired
private AlbumService albumService;
@PostMapping("/list")
public IPage<Album> selectList(@RequestBody AlbumDto albumDto){
IPage<Album> selectList = albumService.selectList(albumDto);
return selectList;
}
}
mybatis方式
dao层:
package cn.luoyong.csmall.product.lianxi.pojo.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 相册
*
* @author java@tedu.cn
* @version 0.0.1
*/
@Data
public class AlbumListItemDTO implements Serializable {
/**
* 记录id
*/
private Long id;
/**
* 相册名称
*/
private String name;
/**
* 相册简介
*/
private String description;
/**
* 自定义排序序号
*/
private Integer sort;
/**
* 数据创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime gmtCreate;
/**
* 数据最后修改时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime gmtModified;
}
mapper层:
/**
* 根据相册名称或者相册简介查询相册列表
* @return
*/
List<AlbumListItemDTO> listDemo(AlbumListItemDTO albumAddNewDTO);
mapper.xml层:
<!-- List<AlbumListItemDTO> listDemo(AlbumListItemDTO albumListItemDTO);-->
<select id="listDemo" resultType="cn.luoyong.csmall.product.lianxi.pojo.dto.AlbumListItemDTO">
select
id,name,description,sort,gmt_create,gmt_modified
from pms_album where 1=1
<if test="name != null and name!= '' ">
and name=#{name}
</if>
<if test="description!=null and description!=''">
and description=#{description}
</if>
</select>
service层:
/**
* 根据相册名称或者相册简介查询相册列表 进行分页
* @param page
* @param pageSize
* @return
*/
JsonPage<AlbumListItemDTO> list(AlbumListItemDTO albumListItemDTO, Integer page, Integer pageSize);
serviceimpl层:
/**
* 根据相册名称或者相册简介查询相册列表 进行分页
* @return
*/
@Override
public JsonPage<AlbumListItemDTO> list(AlbumListItemDTO albumListItemDTO,Integer page, Integer pageSize) {
PageHelper.startPage(page, pageSize);
List<AlbumListItemDTO> list = albumMapper.listDemo(albumListItemDTO);
return JsonPage.restPage(new PageInfo<>(list));
}
controller层:
@ApiOperationSupport(order = 800)
@ApiOperation("根据条件查询相册列表")
@PostMapping("/listDemo")
// @ApiImplicitParams({
// @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
// @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
// })
public JsonResult<JsonPage<AlbumListItemDTO>> listDemo(@RequestBody AlbumListItemDTO albumListItemDTO,
@RequestParam Integer page,
@RequestParam Integer pageSize){
JsonPage<AlbumListItemDTO> list = albumService.list(albumListItemDTO,page,pageSize);
return JsonResult.ok(list);
}