SpringBoot+Mybatis实现接口的增查改,以及json中拼接json数组

2024-01-20 03:48

本文主要是介绍SpringBoot+Mybatis实现接口的增查改,以及json中拼接json数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

增改查的评价表

DROP TABLE IF EXISTS `hpkh_police_review`;
CREATE TABLE `hpkh_police_review` (`id` int(11) NOT NULL AUTO_INCREMENT,`police_code` varchar(255) NOT NULL COMMENT '警员编号',`review_content` text COMMENT '点评内容',`timestamp_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '条目插入时间',`timestamp_modify` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '最后一次修改条目时间戳',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;INSERT INTO `hpkh_police_review` VALUES ('1', '209762', '今天辛苦了', '2020-05-26 11:22:01', '2020-05-26 11:22:01');
INSERT INTO `hpkh_police_review` VALUES ('2', '209762', '交通处罚很好', '2020-05-26 11:32:59', '2020-05-26 15:51:02');
INSERT INTO `hpkh_police_review` VALUES ('3', '017591', 'good', '2020-05-29 10:31:01', '2020-05-29 10:31:01');

增查改部分

实体类

package com.ruoyi.system.domain;import java.sql.Timestamp;/*** @Author zhang dongchao* @Date 2020/5/25 19:44* @title 点评实体类*/
public class PoliceReview {private int id;private String police_code;// 警员编号private String review_content;// 点评内容private Timestamp timestamp_create;//条目插入时间private Timestamp timestamp_modify;//条目修改时间public int getId() {return id;}public void setId(int id) {this.id = id;}public String getPolice_code() {return police_code;}public void setPolice_code(String police_code) {this.police_code = police_code;}public String getReview_content() {return review_content;}public void setReview_content(String review_content) {this.review_content = review_content;}public Timestamp getTimestamp_create() {return timestamp_create;}public void setTimestamp_create(Timestamp timestamp_create) {this.timestamp_create = timestamp_create;}public Timestamp getTimestamp_modify() {return timestamp_modify;}public void setTimestamp_modify(Timestamp timestamp_modify) {this.timestamp_modify = timestamp_modify;}}

Mapepr.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.PersonalResumeMapper"><!--添加留言                 hpkh_police_review 增--><insert id="addReviews" parameterType="String">insert into hpkh_police_review(police_code,review_content)values (#{policeCode},#{reviewContent});</insert><!--根据警员编号获取相关留言列表 hpkh_police_review 查--><select id="getReviewsListByCode" parameterType="String" resultType="PoliceReview">selectid,police_code,review_content,timestamp_create,timestamp_modifyfrom hpkh_police_reviewwhere police_code=#{policeCode}order by timestamp_create desc limit 4</select><!--更新留言                hpkh_police_review  更新--><update id="updateReviewsById">update hpkh_police_review set review_content=#{reviewContent},timestamp_modify=CURRENT_TIMESTAMP where id=#{id}</update><!--下面三个查询了三张表  拼接成个人履历--><!--根据警员编号获取警员概要--><select id="getPoliceInfo" parameterType="String" resultType="java.util.Map">SELECTuser_name,user_code,user_id_card,YEAR (NOW()) - SUBSTR(user_id_card, 7, 4) age,sex_name,user_type_nameFROMism_userWHEREuser_code = #{policeCode}</select><!--// 根据警员编号获取警员履历  --><select id="getPoliceProcessList" parameterType="String" resultType="java.util.Map">SELECTqsrq,jsrq,dw,zw,zwsmFROMhp_mj_llWHEREjh = #{policeCode} order by qsrq asc</select><!--// 根据警员编号获取警员嘉奖--><select id="getCommendOfPolice" parameterType="String" resultType="java.util.Map">SELECTjlmc,pzrqfromhp_mj_jlwherejh=#{policeCode} order by pzrq asc</select>

mapper

注意:不使用@Param注解时,参数只能有一个,当参数超过两个时,要用@Param

package com.ruoyi.system.mapper;import com.ruoyi.system.domain.PoliceEvaluate;
import com.ruoyi.system.domain.PoliceReview;
import com.ruoyi.system.domain.SysPoliceAnalysis;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;import java.util.List;
import java.util.Map;
/*** @Author zhang dongchao* @Date 2020/5/27 18:02* @title 个人简历持久层接口*/
@Repository
public interface PersonalResumeMapper {// 添加留言int addReviews(@Param("policeCode") String policeCode, @Param("reviewContent") String reviewContent);// 根据警员编号获取相关留言列表List<PoliceReview> getReviewsListByCode(String policeCode);// 更新留言int updateReviewsById(@Param("id") int id, @Param("reviewContent") String reviewContent);//下面三个拼接成简历// 根据警员编号获取警员概要Map<String,Object> getPoliceInfo(String policeCode);// 根据警员编号获取警员履历List<Map<String,String>> getPoliceProcessList(String policeCode);// 根据警员编号获取警员嘉奖List<Map<String,String>> getCommendOfPolice(String policeCode);
}

service和impl

package com.ruoyi.system.service;import org.springframework.web.multipart.MultipartFile;import java.util.Map;
/*** @Author zhang dongchao* @Date 2020/5/27 18:02* @title 个人简历页面服务层接口*/
public interface IPersonalResumeService {// 添加留言Map<String,Object> addReviews(String policeCode, String reviewContent);// 根据警员编号获取相关留言列表Map<String,Object> getReviewsListByCode(String policeCode);// 更新留言Map<String,Object> updateReviewsById(int id, String reviewContent);// 根据警员编号获取个人简历Map<String,Object> getPersonalResumeByCode(String policeCode);
}
package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PoliceReview;
import com.ruoyi.system.domain.SysPoliceAnalysis;
import com.ruoyi.system.mapper.PersonalResumeMapper;
import com.ruoyi.system.service.IPersonalResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*** @Author zhang dongchao* @Date 2020/5/27 18:02* @title 个人简历页面服务层接口实现类*/
@Service
public class PersonalResumeServiceImpl implements IPersonalResumeService {@Autowiredprivate PersonalResumeMapper personalResumeMapper;Map<String,Object> result = new HashMap<String,Object>();// 留言@Overridepublic Map<String, Object> addReviews(String policeCode, String reviewContent) {result.clear();// 添加留言personalResumeMapper.addReviews(policeCode,reviewContent);result.put("status",200);result.put("msg","成功点评!");return result;}// 根据警员编号获取相关留言列表@Overridepublic Map<String, Object> getReviewsListByCode(String policeCode) {List<PoliceReview> policeReviewList = personalResumeMapper.getReviewsListByCode(policeCode);result.clear();result.put("reviewlist",policeReviewList);result.put("status",200);return result;}// 更新留言信息@Overridepublic Map<String, Object> updateReviewsById(int id,String reviewContent) {personalResumeMapper.updateReviewsById(id, reviewContent);result.clear();result.put("msg","成功更新");result.put("status",200);return result;}//系统分析导入Excel文件,将条目存库,并封装返回@Overridepublic Map<String, Object> importAnalysis(MultipartFile file) throws Exception {// 创建excel工具类ExcelUtil<SysPoliceAnalysis> excelUtil = new ExcelUtil<SysPoliceAnalysis>(SysPoliceAnalysis.class);// 解析excel数据List<SysPoliceAnalysis> sysAnalysisList = excelUtil.importExcel(file.getInputStream());// 将集合插入数据库personalResumeMapper.importAnalysis(sysAnalysisList);result.clear();result.put("list",sysAnalysisList);result.put("status",200);return result;}// 根据警员编号获取个人简历   调用了mapper中的三个方法拼接成一个自己要的@Overridepublic Map<String, Object> getPersonalResumeByCode(String policeCode) {// 获取警员信息概要   第一个方法返回了一条数据,一个map即可Map<String,Object> policeInfo = personalResumeMapper.getPoliceInfo(policeCode);// 获取警员履历       第二个方法返回了多条数据成了list<map<string,string>>,下面拼接成listList<Map<String,String>> policeProcessList = personalResumeMapper.getPoliceProcessList(policeCode);List<StringBuffer> policeProcessContent = new ArrayList<StringBuffer>();// 遍历履历拼接封装if(policeProcessList.size()>0 && !policeProcessList.isEmpty()){for (Map<String, String> map : policeProcessList) {if(map!=null){StringBuffer content = new StringBuffer();// 如果结束日期为空,则至今if(map.get("jsrq")==null || map.get("jsrq").equals("")){content.append(map.get("qsrq")+" 至今").append("\n").append(map.get("dw")).append("\t").append(map.get("zwsm"));}else{content.append(map.get("qsrq")+" 至 ").append(map.get("jsrq")).append("\n").append(map.get("dw")).append("\t").append(map.get("zwsm"));}policeProcessContent.add(content);}}}// 加入履历   第二个方法的数据组成了jsonArraypoliceInfo.put("policeProcess",policeProcessContent);// 获取警员嘉奖列表   第三个方法返回了多条数据,要用list<map<string,string>>,下面重组成listList<Map<String,String>> commendOfPoliceList = personalResumeMapper.getCommendOfPolice(policeCode);List<StringBuffer> policeCommendContent = new ArrayList<StringBuffer>();// 遍历嘉奖,拼接封装if(commendOfPoliceList.size()>0 && !commendOfPoliceList.isEmpty()) {for (Map<String, String> map : commendOfPoliceList) {if(map != null){StringBuffer content = new StringBuffer();content.append(map.get("jlmc")).append("\t").append(map.get("pzrq"));policeCommendContent.add(content);}}}// 加入警员嘉奖列表  第三个方法的数据组成了jsonArraypoliceInfo.put("policeCommend",policeCommendContent);policeInfo.put("status",200);return policeInfo;}
}

controller。事务处理PostMapping,查询GetMapping

package com.ruoyi.web.controller.system;import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.service.IPersonalResumeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import java.util.Map;
/*** @Author zhang dongchao* @Date 2020/5/27 18:02* @title 个人简历页面接口控制器*/
@Controller
@RequestMapping("/system/personalresume")
@ResponseBody
public class PersonalResumeController extends BaseController {@Autowiredprivate IPersonalResumeService personalResumeService;//LoggerFactory.getLogger可以在IDE控制台打印日志,便于开发,一般加在最上面。调试日志private static final Logger log = LoggerFactory.getLogger(PersonalResumeController.class);/*** 添加留言* @Param policeCode 警员的编号* @Param reviewContent 留言内容*/@PostMapping("/addReviews")public Map<String,Object> addReviews(String policeCode,String reviewContent){return personalResumeService.addReviews(policeCode,reviewContent);}/*** 根据警员编号获取相关留言列表,查询最新四条,按时间降序* @Param policeCode 警员的编号*/@GetMapping("/reviewsList")public Map<String, Object> getReviewsListByCode(String policeCode){return personalResumeService.getReviewsListByCode(policeCode);}/***  根据点评id修改点评信息* @Param id 被修改的点评id*/@PostMapping("/updateReviewsById")public Map<String,Object> updateReviewsById(int id,String reviewContent){log.info("更新点评事件");return personalResumeService.updateReviewsById(id, reviewContent);}/*** 根据警员编号获取个人简历* @param policeCode 警员编号* @return*/@GetMapping("/getPersonalResumeByCode")public Map<String,Object> getPersonalResumeByCode(String policeCode){return personalResumeService.getPersonalResumeByCode(policeCode);}
}

mapper中的三个方法在某条件下取得数据如下

 

 

结果为

这篇关于SpringBoot+Mybatis实现接口的增查改,以及json中拼接json数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/624686

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2