我们的业务场景:上传文件之类的东西,并展示;包括CRUD的功能与一体;
1.公共参数方法
1.1公共返回类型定义
package com.aostar.ida.framework.base.vo; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import com.aostar.ida.framework.base.TokenHolder; /** * 定义前后端交互对象具体属性 * * @version 2019年4月17日 */ public class MsgContext implements Serializable { private static final long serialVersionUID = 1L; /** * 服务代码 */ private String code = MSG_CODE_SUCCESS; /** * 服务器server端返回成功 */ public static final String MSG_CODE_SUCCESS = "1"; /** * 服务器server端返回失败 */ public static final String MSG_CODE_ERROR = "-1"; /** * 通知消息 */ private String msg; /** * 结果数据 */ private Map<String, Object> result = new HashMap<String, Object>(); public MsgContext(){ this.setCode(MSG_CODE_SUCCESS); TokenHolder.set(this); } /** * 方便使用,添加返回结果 * @param key 值名称 * @param value 具体值 */ public void addResult(String key, Object value) { if (result == null) { result = new HashMap<String, Object>(); } result.put(key, value); } public static MsgContext createErrorContext(String msg) { MsgContext mc = new MsgContext(); mc.setCode(MSG_CODE_ERROR); mc.setMsg(msg); return mc; } public static MsgContext createSuccessContext(String msg) { MsgContext mc = new MsgContext(); mc.setCode(MSG_CODE_SUCCESS); mc.setMsg(msg); return mc; } public static MsgContext createSuccessContext(String msg, Long id) { MsgContext mc = new MsgContext(); mc.setCode(MSG_CODE_SUCCESS); mc.setMsg(msg); mc.addResult("id", id); return mc; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getResult() { return result; } public void setResult(Map<String, Object> result) { this.result = result; } }
1.2 分页接口
package com.aostar.ida.business.datasources.vo; import java.io.Serializable; import lombok.Data; import java.util.Collection; import java.util.Collections; import java.util.List; @Data public class MyPageInfoVo <T> implements Serializable{ private static final long serialVersionUID = 1L; //当前页 private Integer pageNum; //每页的数量 private Integer pageSize; //当前页的数量 private int size; //当前页面第一个元素在数据库中的行号 private int startRow; //当前页面最后一个元素在数据库中的行号 private int endRow; //总记录数 private int total; //总页数 private int pages; //结果集 private List<T> list; //前一页 private int prePage; //下一页 private int nextPage; //是否为第一页 private boolean isFirstPage = false; //是否为最后一页 private boolean isLastPage = false; //是否有前一页 private boolean hasPreviousPage = false; //是否有下一页 private boolean hasNextPage = false; //导航页码数 private int navigatePages; //所有导航页号 private int[] navigatepageNums; //导航条上的第一页 private int navigateFirstPage; //导航条上的最后一页 private int navigateLastPage; public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getStartRow() { return startRow; } public void setStartRow(int startRow) { this.startRow = startRow; } public int getEndRow() { return endRow; } public void setEndRow(int endRow) { this.endRow = endRow; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getPrePage() { return prePage; } public void setPrePage(int prePage) { this.prePage = prePage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public boolean isFirstPage() { return isFirstPage; } public void setFirstPage(boolean firstPage) { isFirstPage = firstPage; } public boolean isLastPage() { return isLastPage; } public void setLastPage(boolean lastPage) { isLastPage = lastPage; } public boolean isHasPreviousPage() { return hasPreviousPage; } public void setHasPreviousPage(boolean hasPreviousPage) { this.hasPreviousPage = hasPreviousPage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } public int getNavigatePages() { return navigatePages; } public void setNavigatePages(int navigatePages) { this.navigatePages = navigatePages; } public int[] getNavigatepageNums() { return navigatepageNums; } public void setNavigatepageNums(int[] navigatepageNums) { this.navigatepageNums = navigatepageNums; } public int getNavigateFirstPage() { return navigateFirstPage; } public void setNavigateFirstPage(int navigateFirstPage) { this.navigateFirstPage = navigateFirstPage; } public int getNavigateLastPage() { return navigateLastPage; } public void setNavigateLastPage(int navigateLastPage) { this.navigateLastPage = navigateLastPage; } /** * 对list集合进行分页 * @param list 需要分页的集合 * @param pageNum 当前页 * @param pageSize 每页的数量 */ public MyPageInfoVo(List<T> list,Integer pageNum,Integer pageSize) { this(list, 8, pageNum, pageSize); } /** * 对list集合进行分页 * @param list 需要分页的集合 * @param navigatePages 导航页码数 * @param pageNum 当前页 * @param pageSize 每页的数量 */ public MyPageInfoVo(List<T> list, int navigatePages,int pageNum,int pageSize) { if (list instanceof Collection) { // 总记录数 this.total = list.size(); // 为了跟pageHelper一致,当pageNum<1时,按第一页处理 int yourPageNum = pageNum; pageNum= pageNum<1 ? 1:pageNum; // 当前页码 this.pageNum = pageNum; // 每页显示的记录数 this.pageSize = pageSize; // 总页码数 this.pages = (int)Math.ceil(this.total*1.0/this.pageSize); // 导航条页码数 this.navigatePages = navigatePages; // 开始行号 this.startRow = this.pageNum * this.pageSize - (this.pageSize - 1); // 结束行号 this.endRow = this.pageNum * this.pageSize; // 当结束行号>总行数,结束行号=0 if(this.endRow > this.total){ if(this.startRow > this.total){ this.endRow = 0; this.startRow = 0; }else { this.endRow =this.total; } } //计算导航页 calcNavigatepageNums(); //计算前后页,第一页,最后一页 calcPage(); //判断页面边界 judgePageBoudary(); // 当pageNum超过最大页数,则size=0,list为空 if(this.pageNum > this.pages){ this.size = 0; this.list = Collections.EMPTY_LIST; }else { this.list = list.subList(startRow - 1,endRow); this.size = this.list.size(); } this.pageNum = yourPageNum; } } /** * 计算导航页 */ private void calcNavigatepageNums() { //当总页数小于或等于导航页码数时 if (pages <= navigatePages) { navigatepageNums = new int[pages]; for (int i = 0; i < pages; i++) { navigatepageNums[i] = i + 1; } } else { //当总页数大于导航页码数时 navigatepageNums = new int[navigatePages]; int startNum = pageNum - navigatePages / 2; int endNum = pageNum + navigatePages / 2; if (startNum < 1) { startNum = 1; //(最前navigatePages页 for (int i = 0; i < navigatePages; i++) { navigatepageNums[i] = startNum++; } } else if (endNum > pages) { endNum = pages; //最后navigatePages页 for (int i = navigatePages - 1; i >= 0; i--) { navigatepageNums[i] = endNum--; } } else { //所有中间页 for (int i = 0; i < navigatePages; i++) { navigatepageNums[i] = startNum++; } } } } /** * 计算前后页,第一页,最后一页 */ private void calcPage() { if (navigatepageNums != null && navigatepageNums.length > 0) { navigateFirstPage = navigatepageNums[0]; navigateLastPage = navigatepageNums[navigatepageNums.length - 1]; if (pageNum > 1) { prePage = pageNum - 1; } if (pageNum < pages) { nextPage = pageNum + 1; } } } /** * 判定页面边界 */ private void judgePageBoudary() { isFirstPage = pageNum == 1; isLastPage = pageNum == pages; hasPreviousPage = pageNum > 1; hasNextPage = pageNum < pages; } }
1.3公共实体类
package com.aostar.ida.business.datasources.vo; import com.aostar.ida.framework.base.vo.BaseVO; import org.hibernate.validator.constraints.Length; import java.util.Date; public class FileDypeVo { /** * 主键 */ private Long id; /** * 文件名称 */ private String fileName; /** * 文件路径 */ private String filePath; /** * 文件所属类型 状态(1:a文件 2:B文件 3:C文件) */ @Length(max = 1, message = "状态不得超过1个字符") private String fileType; /** * 文件上传时间 */ private String filePutDate; /** * 上传分页类型 */ private String filePageType; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public String getFilePutDate() { return filePutDate; } public void setFilePutDate(String filePutDate) { this.filePutDate = filePutDate; } public String getFilePageType() { return filePageType; } public void setFilePageType(String filePageType) { this.filePageType = filePageType; } @Override public String toString() { return "FileDypeVo [id=" + id + ", fileName=" + fileName + ", filePath=" + filePath + ", fileType=" + fileType + ", filePutDate=" + filePutDate + ", filePageType=" + filePageType + "]"; } }
1.4 公共的 mapper.java/xml(都放在一起)
1.4.1 FileMapper .java
package com.aostar.ida.business.datasources.dao; import org.apache.ibatis.annotations.Param; import com.aostar.ida.business.datasources.vo.FileDypeVo; import java.util.List; /** * 上传文件 */ public interface FileMapper { int fileinsert(FileDypeVo fileDypeVo); List<FileDypeVo> selectFile(@Param("filePageType")String filePageType); FileDypeVo selectFileId(@Param("id")Long id); }
1.4.2 FileMapper .xml
<?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.aostar.ida.business.datasources.dao.FileMapper"> <resultMap id="FileResultMap" type="com.aostar.ida.business.datasources.vo.FileDypeVo"> <id column="id" property="id" jdbcType="BIGINT" /> <result column="file_Name" property="fileName" jdbcType="VARCHAR" /> <result column="file_Path" property="filePath" jdbcType="VARCHAR" /> <result column="file_Type" property="fileType" jdbcType="VARCHAR" /> <result column="file_Put_Date" property="filePutDate" jdbcType="VARCHAR" /> <result column="file_page_Type" property="filePageType" jdbcType="VARCHAR" /> </resultMap> <sql id="File_Column_List" > id, file_Name, file_Path, file_Type, file_Put_Date, file_page_Type </sql> <insert id="fileinsert" parameterType="com.aostar.ida.business.datasources.vo.FileDypeVo" > insert into com_ds_file (id, file_Name, file_Path, file_Type, file_Put_Date,file_page_Type ) values (#{id,jdbcType=BIGINT}, #{fileName,jdbcType=VARCHAR}, #{filePath,jdbcType=VARCHAR}, #{fileType,jdbcType=VARCHAR}, #{filePutDate,jdbcType=VARCHAR},#{filePageType,jdbcType=VARCHAR} ) </insert> <select id="selectFile" resultMap="FileResultMap" parameterType="com.aostar.ida.business.datasources.vo.FileDypeVo"> select <include refid="File_Column_List"/> from com_ds_file where file_page_Type=#{filePageType} </select> <select id="selectFileId" resultMap="FileResultMap" parameterType="com.aostar.ida.business.datasources.vo.FileDypeVo"> select <include refid="File_Column_List"/> from com_ds_file where id=#{id} </select> </mapper>
1.5 Dao层
package com.aostar.ida.business.datasources.service; import java.util.List; import com.aostar.ida.business.datasources.vo.DsDragVO; import com.aostar.ida.business.datasources.vo.DsTypeVO; import com.aostar.ida.framework.base.exception.ServiceException; public interface DataSourcesService{ /** * 上传文件 * @param request * @param uploadFile * @return * @throws ServiceException */ Long updateFile(HttpServletRequest request, MultipartFile uploadFile,String fileType,String filePageType)throws ServiceException; /** * 查询 * @param pageNum * @param pageSize * @param filePageType * @return */ MyPageInfoVo<FileDypeVo> selectFile(Integer pageNum,Integer pageSize,String filePageType); /** * 删除文件 * @param id * @return */ Long delectFile(Long id); /** * 下载预览 * @param response * @param id * @param isOnLine * @return * @throws IOException */ FileDypeVo downFile(HttpServletResponse response,Long id,Boolean isOnLine) throws IOException; }
1.6 调用常理
package com.aostar.ida.framework.base; /* * 定义文件 使用常量 yan 2022/10/13 * */ public interface FileController { /** * 上传文件 页面1 文件管理 */ String FILE_FIRSTLY = "10"; /** * 上传文件 页面2 成果管理 */ String FILE_SECOND = "20"; /** * 上传文件 页面3 典型场景管理管理 */ String FILE_THIRD = "30"; /** * 页面下对应子模块 * 页面1 * 1.帮助文档 * 2.规范 * 3.设计小样 * 4.场景解说词 * 页面2 * 1.设计小样 * 2.场景解说词 * 3.第三方应用成功 */ String FILE_ONE = "1"; String FILE_TWO = "2"; String FILE_THREE = "3"; String FILE_FOUR = "4"; /** * 路径拼接 页面1 * */ String FILE_FIRSTLY_PATH = "//FIRSTLY//"; /** * 路径拼接 页面2 * */ String FILE_SECOND_PATH = "//SECOND//"; /** * 路径拼接 页面3 * */ String FILE_THIRD_PATH = "//THIRD//"; }
1.7 数据库执行语句
/* Navicat Premium Data Transfer Source Server : 可视化项目 Source Server Type : MySQL Source Server Version : 50736 Source Host : localhost:3306 Source Schema : zhongdian Target Server Type : MySQL Target Server Version : 50736 File Encoding : 65001 Date: 14/10/2022 11:39:07 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for com_ds_file -- ---------------------------- DROP TABLE IF EXISTS `com_ds_file`; CREATE TABLE `com_ds_file` ( `id` bigint(11) NOT NULL AUTO_INCREMENT, `file_Name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `file_Path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `file_Type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `file_Put_Date` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `file_page_Type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 59766911012865 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;