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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核