当前位置:   article > 正文

springboot+vue2+elementui实现时间段查询

springboot+vue2+elementui实现时间段查询

1.前端代码


使用elementui的时间段选择器:

  1. <el-date-picker v-model="queryPage.itemTime" type="daterange"
  2. value-format="yyyy-MM-dd" class="filter-item"
  3. range-separator="至" start-placeholder="创建日期"
  4. end-placeholder="创建日期">
  5. </el-date-picker>

设置查询字段,为数组形式:

  1. queryPage: {
  2. page: 1,
  3. limit: 20,
  4. itemTime:[],
  5. },

编写方法,调用后端接口:

  1. search() { // 查询
  2. this.loadList()
  3. },
  4. loadList() { // 加载列表
  5. this.tableData = []
  6. this.listLoading = true
  7. getPage(this.queryPage).then(response => {
  8. const { data } = response
  9. this.tableData = data.records
  10. this.total = parseInt(data.total)
  11. this.listLoading = false
  12. }).catch(response => {
  13. this.listLoading = false
  14. })
  15. },

在api文件夹对应的js添加

  1. export function getPage(data) {
  2. return request({
  3. url: '/operationMaintenance/getPage',
  4. method: 'post',
  5. data
  6. })
  7. }

2.后端代码

controller

  1. @PostMapping("/getPage")
  2. public ResultData getPage(@RequestBody QueryEntry queryEntry) {
  3. IPage<OperationMaintenance> wappers = operationMaintenanceService.getPage(queryEntry);
  4. return ResultData.success("OK", wappers);
  5. }

serviceimpl

  1. @Override
  2. public IPage<OperationMaintenance> getPage(QueryEntry queryEntry) {
  3. Page<OperationMaintenance> page = new Page<>(queryEntry.getPage(), queryEntry.getLimit());
  4. IPage<OperationMaintenance> iPage = operationMaintenanceMapper.getPage(page, queryEntry);
  5. return iPage;
  6. }

mapper

  1. @Mapper
  2. public interface OperationMaintenanceMapper extends BaseMapper<OperationMaintenance> {
  3. IPage<OperationMaintenance> getPage(Page<OperationMaintenance> page, @Param("queryEntry") QueryEntry queryEntry);
  4. OperationMaintenance getInfo(Long code);
  5. List<OperationMaintenance> getSelected2Page(@Param("queryEntry") QueryEntry queryEntry);
  6. }

mapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.todod.mapper.OperationMaintenanceMapper">
  4. <select id="getPage" resultType="com.todod.model.OperationMaintenance">
  5. SELECT om.om_id id,mc.kh_code khCode,mc.kh_name khName,mpi.xg_pk_id mid, mpi.xg_code xmCode,mpi.xg_name xmName,om.om_title title,
  6. su.su_nick_name nickName, om.om_start_time startTime,om.om_end_time endTime,om.om_content content,om.om_message message,
  7. om.om_report_id reportId,om.om_file_id fileId,
  8. om.om_state state,om.om_create_user_id createUserId,om.om_create_time createTime
  9. FROM operation_maintenance om
  10. left JOIN data_governance_db_dev.md_project_info mpi ON mpi.xg_pk_id = om.om_dir_id
  11. left JOIN data_governance_db_dev.sys_user su ON su.su_id = om.om_staff_id
  12. left JOIN data_governance_db_dev.md_customer mc ON mc.kh_pk_id = om.om_user_id AND mpi.xg_flag_fl = 'KH'
  13. <where> 1 = 1
  14. <if test="queryEntry.getKhCode != null and queryEntry.getKhCode != '' and queryEntry.getKhCode != 'null'">
  15. and mc.kh_code like '${queryEntry.getKhCode}'
  16. </if>
  17. <if test="queryEntry.getLevel != null and queryEntry.getLevel != '' and queryEntry.getLevel != 'null'">
  18. and mpi.xg_pk_id like '${queryEntry.getLevel}'
  19. </if>
  20. <if test="queryEntry.getKhName != null and queryEntry.getKhName != '' and queryEntry.getKhName != 'null'">
  21. and mc.kh_name like '%${queryEntry.getKhName}%'
  22. </if>
  23. <if test="queryEntry.getXmCode != null and queryEntry.getXmCode != '' and queryEntry.getXmCode != 'null'" >
  24. and mpi.xg_code like '${queryEntry.getXmCode}'
  25. </if>
  26. <if test="queryEntry.getXmName != null and queryEntry.getXmName != '' and queryEntry.getXmName != 'null'" >
  27. and mpi.xg_name = '${queryEntry.getXmName}'
  28. </if>
  29. <if test="queryEntry.getTitle != null and queryEntry.getTitle != '' and queryEntry.getTitle != 'null'" >
  30. and om.om_title like '%${queryEntry.getTitle}%'
  31. </if>
  32. <if test="queryEntry.get_userid != null and queryEntry.get_userid != '' and queryEntry.get_userid != 'null'" >
  33. and om.om_create_user_id like '${queryEntry.get_userid}'
  34. </if>
  35. <if test="queryEntry.startTime != null and queryEntry.startTime != ''">
  36. <![CDATA[ AND om.om_start_time >= #{queryEntry.startTime, jdbcType=TIMESTAMP} ]]>
  37. </if>
  38. <if test="queryEntry.endTime != null and queryEntry.endTime != ''">
  39. <![CDATA[ AND om.om_end_time <= #{queryEntry.endTime, jdbcType=TIMESTAMP} ]]>
  40. </if>
  41. <if test="queryEntry.getState != null and queryEntry.getState != '' and queryEntry.getState != 'null'" >
  42. and om.om_state = '${queryEntry.getState}'
  43. </if>
  44. </where>
  45. ORDER BY om.om_create_time desc
  46. </select>
  47. <select id="getInfo" resultType="com.todod.model.OperationMaintenance">
  48. SELECT om.om_id id,om.om_dir_id dirId,om.om_user_id userId,om.om_staff_id staffId,om.om_title title,
  49. om.om_start_time startTime,om.om_end_time endTime,om.om_content content,om.om_message message,
  50. om.om_report_id reportId,om.om_file_id fileId, om.om_state state,om.om_create_user_id createUserId,om.om_create_time createTime
  51. FROM operation_maintenance om
  52. <where> 1 = 1
  53. <if test="code != null">
  54. and om.om_id = '${code}'
  55. </if>
  56. </where>
  57. </select>
  58. <select id="getSelected2Page" resultType="com.todod.model.OperationMaintenance">
  59. select om.om_id id,om.om_dir_id dirId,om.om_user_id userId,om.om_staff_id staffId,om.om_title title,
  60. om.om_start_time startTime,om.om_end_time endTime,om.om_content content,om.om_message message,
  61. om.om_state state,om.om_create_user_id createUserId,om.om_create_time createTime
  62. from operation_maintenance om
  63. </select>
  64. </mapper>

查询条件实体类

  1. package com.todod.entity;
  2. /**
  3. * 查询条件
  4. *
  5. * @ClassName: QueryEntry
  6. * @Description:
  7. * @author gsh
  8. * @date 2019年10月11日
  9. *
  10. */
  11. public class QueryEntry {
  12. private Long menuId;
  13. //private Long parentId;
  14. private String name;
  15. private String code;
  16. private String loginName;
  17. private String nickName;
  18. private String beginDate;
  19. private String endDate;
  20. private String state;
  21. private Long _userid;
  22. private String khName;
  23. private String khCode;
  24. private String xmCode;
  25. private String xmName;
  26. private String title;
  27. private String startTime;
  28. private String endTime;
  29. private String type;
  30. private String priority;
  31. private String status;
  32. private String itTitle;
  33. private String itCreatTime;
  34. private Long maintenanceId;
  35. private String[] itemTime;
  36. public String[] getItemTime() {
  37. return itemTime;
  38. }
  39. public void setItemTime(String[] itemTime) {
  40. if (itemTime != null && itemTime.length == 2) {
  41. this.startTime = itemTime[0]; // 假设第一个元素是开始时间
  42. this.endTime = itemTime[1]; // 假设第二个元素是结束时间
  43. } else {
  44. // 处理错误情况,比如数组为null或长度不为2
  45. // 可以抛出异常,或者设置默认值等
  46. this.startTime = null;
  47. this.endTime = null;
  48. }
  49. }
  50. private String[] createTime;
  51. public String[] getCreateTime() {
  52. return createTime;
  53. }
  54. public void setCreateTime(String[] createTime) {
  55. if (createTime != null && createTime.length == 2) {
  56. this.createTimeOne = createTime[0]; // 假设第一个元素是开始时间
  57. this.createTimeTwo = createTime[1]; // 假设第二个元素是结束时间
  58. } else {
  59. // 处理错误情况,比如数组为null或长度不为2
  60. // 可以抛出异常,或者设置默认值等
  61. this.createTimeOne = null;
  62. this.createTimeTwo = null;
  63. }
  64. }
  65. private String createTimeOne;
  66. public String getCreateTimeOne() {
  67. return createTimeOne;
  68. }
  69. public void setCreateTimeOne(String createTimeOne) {
  70. this.createTimeOne = createTimeOne;
  71. }
  72. private String createTimeTwo;
  73. public String getCreateTimeTwo() {
  74. return createTimeTwo;
  75. }
  76. public void setCreateTimeTwo(String createTimeTwo) {
  77. this.createTimeTwo = createTimeTwo;
  78. }
  79. private Long level;
  80. public Long getLevel() {
  81. return level;
  82. }
  83. public void setLevel(Long level) {
  84. this.level = level;
  85. }
  86. public Long getMaintenanceId() {
  87. return maintenanceId;
  88. }
  89. public void setMaintenanceId(Long maintenanceId) {
  90. this.maintenanceId = maintenanceId;
  91. }
  92. public String getItTitle() {
  93. return itTitle;
  94. }
  95. public void setItTitle(String itTitle) {
  96. this.itTitle = itTitle;
  97. }
  98. public String getPriority() {
  99. return priority;
  100. }
  101. public void setPriority(String priority) {
  102. this.priority = priority;
  103. }
  104. public String getStatus() {
  105. return status;
  106. }
  107. public void setStatus(String status) {
  108. this.status = status;
  109. }
  110. public String getItCreatTime() {
  111. return itCreatTime;
  112. }
  113. public void setItCreatTime(String itCreatTime) {
  114. this.itCreatTime = itCreatTime;
  115. }
  116. public String getType() {
  117. return type;
  118. }
  119. public void setType(String type) {
  120. this.type = type;
  121. }
  122. public String getKhCode() {
  123. return khCode;
  124. }
  125. public void setKhCode(String khCode) {
  126. this.khCode = khCode;
  127. }
  128. public String getXmCode() {
  129. return xmCode;
  130. }
  131. public void setXmCode(String xmCode) {
  132. this.xmCode = xmCode;
  133. }
  134. public String getXmName() {
  135. return xmName;
  136. }
  137. public void setXmName(String xmName) {
  138. this.xmName = xmName;
  139. }
  140. public String getTitle() {
  141. return title;
  142. }
  143. public void setTitle(String title) {
  144. this.title = title;
  145. }
  146. public String getStartTime() {
  147. return startTime;
  148. }
  149. public void setStartTime(String startTime) {
  150. this.startTime = startTime;
  151. }
  152. public String getEndTime() {
  153. return endTime;
  154. }
  155. public void setEndTime(String endTime) {
  156. this.endTime = endTime;
  157. }
  158. public String getKhName() {
  159. return khName;
  160. }
  161. public void setKhName(String khName) {
  162. this.khName = khName;
  163. }
  164. public Long get_userid() {
  165. return _userid;
  166. }
  167. public void set_userid(Long _userid) {
  168. this._userid = _userid;
  169. }
  170. private int page = 1; // 页码
  171. private int limit = 10; // 每页条数
  172. public int getStart() {
  173. int start = (page - 1) * limit; // 起始页码
  174. return start;
  175. }
  176. public int getPage() {
  177. return page;
  178. }
  179. public void setPage(int page) {
  180. this.page = page;
  181. }
  182. public int getLimit() {
  183. return limit;
  184. }
  185. public void setLimit(int limit) {
  186. this.limit = limit;
  187. }
  188. public String getLoginName() {
  189. return loginName;
  190. }
  191. public void setLoginName(String loginName) {
  192. this.loginName = loginName;
  193. }
  194. public String getNickName() {
  195. return nickName;
  196. }
  197. public void setNickName(String nickName) {
  198. this.nickName = nickName;
  199. }
  200. public String getBeginDate() {
  201. return beginDate;
  202. }
  203. public void setBeginDate(String beginDate) {
  204. this.beginDate = beginDate;
  205. }
  206. public String getEndDate() {
  207. return endDate;
  208. }
  209. public void setEndDate(String endDate) {
  210. this.endDate = endDate;
  211. }
  212. public String getState() {
  213. return state;
  214. }
  215. public void setState(String state) {
  216. this.state = state;
  217. }
  218. public String getName() {
  219. return name;
  220. }
  221. public void setName(String name) {
  222. this.name = name;
  223. }
  224. public String getCode() {
  225. return code;
  226. }
  227. public void setCode(String code) {
  228. this.code = code;
  229. }
  230. public Long getMenuId() {
  231. return menuId;
  232. }
  233. public void setMenuId(Long menuId) {
  234. this.menuId = menuId;
  235. }
  236. @Override
  237. public String toString() {
  238. return "QueryEntry [name=" + name + ", code=" + code + ", loginName=" + loginName + ", nickName=" + nickName
  239. + ", beginDate=" + beginDate + ", endDate=" + endDate + ", state=" + state + ", _userid=" + _userid
  240. + ", page=" + page + ", limit=" + limit + "]";
  241. }
  242. public Long getParentId() {
  243. return _userid;
  244. }
  245. }

这是关键:

将前端传来的时间数组,赋值给对应的属性;

vue可以使用插值表达式将后端传来的两个时间拼接在一起:

  1. <el-table-column align="left" header-align="center" label="运维开始-至-结束日期" min-width="200">
  2. <template slot-scope="scope">
  3. <span>{{ scope.row.startTime }} 至 {{ scope.row.endTime }}</span>
  4. </template>
  5. </el-table-column>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/610311
推荐阅读
相关标签
  

闽ICP备14008679号