本文主要是介绍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数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!