开发总结后端获取图片数据的开发文档
LiSir后端获取图片数据的开发文档
1.后端获取图片数据的基本需求(文件)
在com/ruoyi/system/domain/目录下创建SysPicture.java
在该类中编写你在图片数据库中定义的属性:以下拿我的举例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| /** * 图片表 data_entires * * */
public class SysPicture extends BaseEntity{ private static final long serialVersionUID = 1L;
/** 图片序号 */ @Excel(name = "图片序号", cellType = Excel.ColumnType.NUMERIC) private Long pictureId;
/** 图片编码 */ @Excel(name = "图片编码") private String pictureCode;
/** 图片名称 */ @Excel(name = "图片名称") private String pictureName;
/** 图片排序 */ @Excel(name = "图片排序") private Integer pictureSort;
/** 状态(0正常 1停用) */ @Excel(name = "状态", readConverterExp = "0=正常,1=停用") private String status;
|
添加Set,Get方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public Long getPictureId() { return pictureId; }
public void setPictureId(Long pictureId) { this.pictureId = pictureId; }
public String getPictureCode() { return pictureCode; }
public void setPictureCode(String pictureCode) { this.pictureCode = pictureCode; }
public String getPictureName() { return pictureName; }
public void setPictureName(String pictureName) { this.pictureName = pictureName; }
public Integer getPictureSort() { return pictureSort; }
public void setPictureSort(Integer pictureSort) { this.pictureSort = pictureSort; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
|
添加toString()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("pictureId", getPictureId()) .append("postCode", getPictureCode()) .append("postName", getPictureName()) .append("postSort", getPictureSort()) .append("status", getStatus()) .append("createBy", getCreateBy()) .append("createTime", getCreateTime()) .append("updateBy", getUpdateBy()) .append("updateTime", getUpdateTime()) .append("remark", getRemark()) .toString();
|
可以参考其他的domian类文件。
在com/ruoyi/web/controller/system/目录下创建SysPictureController.java
控制层中我们主要添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @RestController @RequestMapping("/system/picture") public class SysPictureController extends BaseController { //创建并注入服务处的对象 @Autowired private ISysPictureService pictureService; /** * 获取图片 */ @PreAuthorize("@ss.hasPermi('system:picture:list')") @GetMapping("/list") public TableDataInfo list(SysPicture picture) { startPage(); List<SysPicture> list = pictureService.selectPictureList(picture); return getDataTable(list); }
}
|
在com/ruoyi/system/service/目录下创建服务层的类ISysPictureService.java
在该类中添加如下代码
1 2 3 4 5 6 7 8 9
| public interface ISysPictureService { /** * 查询图片信息集合 * * @param picture 图片信息 * @return 图片列表 */ public List<SysPicture> selectPictureList(SysPicture picture); }
|
在com/ruoyi/system/service/impl/目录下编写SysPictureServiceImpl.java类并添加如下代码:
1 2 3 4 5 6 7 8 9 10 11
| @Service public class SysPictureServiceImpl implements ISysPictureService { @Autowired private SysPictureMapper pictureMapper; @Override public List<SysPicture> selectPictureList(SysPicture picture) { return pictureMapper.selectPictureList(picture); }
}
|
在com/ruoyi/system/mapper/目录下编写SysPictureMapper.java类
在该类中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| /** *图片信息 数据层 */ @Repository public interface SysPictureMapper { /** * 查询图片数据集合 * * @param picture 图片信息 * @return 图片数据集合 */ public List<SysPicture> selectPictureList(SysPicture picture);
}
|
在mapper/system/目录下添加SysPictureMapper.xml文件
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 以上部分为文件头,可复制直接使用 <mapper namespace="com.ruoyi.system.mapper.SysPictureMapper">//该位置必须要找到Mapper文件所在位置 <resultMap type="SysPicture" id="SysPictureResult"> <id property="pictureId" column="picture_id" /> <result property="pictureCode" column="picture_code" /> <result property="pictureName" column="picture_name" /> <result property="pictureSort" column="picture_sort" /> <result property="status" column="status" /> <result property="createBy" column="create_By" /> <result property="createTime" column="create_time" /> <result property="updateBy" column="update_by" /> <result property="updateTime" column="update_time" /> <result property="remark" column="remark" /> </resultMap> <sql id="selectPictureVo"> select picture_id, picture_code, picture_name, picture_sort, status, create_by, create_time, remark from data_entires </sql>
<select id="selectPictureList" parameterType="SysPost" resultMap="SysPictureResult"> <include refid="selectPictureVo"/> <where> <if test="pictureCode != null and pictureCode != ''"> AND picture_code like concat('%', #{pictureCode}, '%') </if> <if test="status != null and status != ''"> AND status = #{status} </if> <if test="pictureName != null and pictureName != ''"> AND picture_name like concat('%', #{pictureName}, '%') </if> </where> </select> </mapper>
|
以上的大部分代码,均参考ruoyi官方编写的其他内容改编
由于ruoyi设置了查询权限,我们在运行项目进行数据查询时会提示权限不足,这时候我们需要带着两个请求头去查询
Cookie
Authorization