手摸手2-springboot编写基础的增删改查

目录

    • 手摸手2-springboot编写基础的增删改查
      • 创建controller层
      • 添加service层接口
      • service层实现
      • 添加mapper层
      • mapper层对应的sql
      • 添加扫描注解,对应sql文件的目录

手摸手2-springboot编写基础的增删改查

创建controller层

实现 test 表中的添加、修改、删除及列表查询接口(未分页)

package com.onejson.ojmall.controller;import com.onejson.ojmall.entity.TestEntity;
import com.onejson.ojmall.entity.dto.TestDTO;
import com.onejson.ojmall.entity.vo.TestVO;
import com.onejson.ojmall.service.ITestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;
import java.util.List;/*** 测试表** @author 微信公众号 onejson* @date 2023-08-11*/
@RestController
@RequestMapping(path = "/test", produces = "application/json;charset=UTF-8")
@Api(value = "/test", tags = "测试表", produces = "application/json;charset=UTF-8")
public class TestController{@Resourceprivate ITestService testService;/*** 查询列表*/@ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")@GetMapping("/list")public List<TestEntity> list(TestEntity sysTest) {return testService.selectTestList(sysTest);}/*** 新增*/@ApiOperation(value = "新增")@PostMappingpublic boolean add(@Validated @RequestBody TestDTO testDTO) {return testService.insertTest(testDTO);}/*** 修改*/@ApiOperation(value = "更新")@PutMappingpublic boolean edit(@RequestBody TestDTO testDTO) {return testService.updateTest(testDTO);}/*** 详情*/@ApiOperation(value = "详情")@GetMapping(value = "/{id}")public TestVO getInfo(@PathVariable("id") Integer id) {return testService.getTestById(id);}/*** 删除*/@ApiOperation(value = "删除")@DeleteMapping("/{ids}")public boolean remove(@PathVariable Integer[] ids) {return testService.removeTestByIds(ids);}}

添加service层接口

package com.onejson.ojmall.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.onejson.ojmall.entity.TestEntity;
import com.onejson.ojmall.entity.dto.TestDTO;
import com.onejson.ojmall.entity.vo.TestVO;import java.util.List;/*** 测试表** @author 微信公众号 onejson* @date 2023-08-11 11:24:47*/
public interface ITestService extends IService<TestEntity> {/*** 查询测试表列表** @param testEntity 测试表Entity类* @return list列表*/List<TestEntity> selectTestList(TestEntity testEntity);/*** 新增测试表** @param testDTO 测试表DTO类* @return 结果*/boolean insertTest(TestDTO testDTO);/*** 更新测试表** @param testDTO 测试表DTO类* @return 结果*/boolean updateTest(TestDTO testDTO);/*** 详情测试表** @param id id值* @return 结果*/TestVO getTestById(Integer id);/*** 删除测试表** @param ids id数组* @return 结果*/boolean removeTestByIds(Integer[] ids);}

service层实现

package com.onejson.ojmall.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onejson.ojmall.entity.TestEntity;
import com.onejson.ojmall.entity.dto.TestDTO;
import com.onejson.ojmall.entity.vo.TestVO;
import com.onejson.ojmall.mapper.TestMapper;
import com.onejson.ojmall.service.ITestService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;/*** 测试表** @author 微信公众号 onejson* @date 2023-08-11*/
@Service
@Transactional
public class TestServiceImpl extends ServiceImpl<TestMapper, TestEntity> implements ITestService {@Resourceprivate TestMapper testMapper;/*** 查询测试表列表** @param testEntity 测试表Entity类* @return 测试表*/@Overridepublic List<TestEntity> selectTestList(TestEntity testEntity) {return testMapper.selectTestList(testEntity);}/*** 新增测试表** @param testDTO 测试表DTO类* @return 结果*/@Overridepublic boolean insertTest(TestDTO testDTO) {TestEntity testInfoEntity = new TestEntity();BeanUtils.copyProperties(testDTO, testInfoEntity);return this.save(testInfoEntity);}/*** 更新测试表** @param testDTO 测试表DTO类* @return 结果*/@Overridepublic boolean updateTest(TestDTO testDTO) {TestEntity testInfoEntity = new TestEntity();BeanUtils.copyProperties(testDTO, testInfoEntity);return this.updateById(testInfoEntity);}/*** 详情测试表** @param id id值* @return 结果*/@Overridepublic TestVO getTestById(Integer id) {TestEntity testEntity = this.getById(id);TestVO testVO = new TestVO();BeanUtils.copyProperties(testEntity, testVO);return testVO;}/*** 删除测试表** @param ids id数组* @return 结果*/@Overridepublic boolean removeTestByIds(Integer[] ids) {return this.removeByIds(Arrays.asList(ids));}}

添加mapper层

package com.onejson.ojmall.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.onejson.ojmall.entity.TestEntity;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** 测试表** @author 微信公众号 onejson* @date 2023-08-11 11:24:47*/
@Mapper
public interface TestMapper extends BaseMapper<TestEntity> {/*** 查询测试表列表** @param testEntity 测试表Entity类* @return list列表*/List<TestEntity> selectTestList(TestEntity testEntity);/*** 统计测试表个数** @param testEntity 测试表Entity类* @return 符合条件的记录个数*/Integer countTest(TestEntity testEntity);}

mapper层对应的sql

<?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.onejson.ojmall.mapper.TestMapper"><!-- 可根据自己的需求,是否要使用 --><resultMap type="com.onejson.ojmall.entity.TestEntity" id="testMap"><result property="id" column="id"/><result property="title" column="title"/></resultMap><sql id="selectTest">select *from test</sql><sql id="whereTest"><where><if test="id !=null and id !=''">AND id = #{id,jdbcType=VARCHAR}</if><if test="title !=null and title !=''">AND title = #{title,jdbcType=VARCHAR}</if></where></sql><select id="selectTestList" parameterType="com.onejson.ojmall.entity.TestEntity" resultMap="testMap"><include refid="selectTest"/><include refid="whereTest"/></select><select id="countTest" parameterType="com.onejson.ojmall.entity.TestEntity" resultType="java.lang.Integer">SELECT count(*)FROM (<include refid="selectTest"/><include refid="whereTest"/>) a</select></mapper>

添加扫描注解,对应sql文件的目录

@MapperScan("com.onejson.ojmall.mapper")

image-20230811175227748

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/62798.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity——工程与资源

本文将详细介绍Unity工程的文件夹结构&#xff0c;以及动态加载资源的技术要点 一、Unity项目的文件夹结构 1.工程文件夹 在新建工程时&#xff0c;Unity会创建所有必要的文件夹。第一级文件夹有Assets,Library,Logs,Packages,ProjectSettings。 Assets&#xff1a;最主要的文…

Oracle Scheduler学习

参考文档&#xff1a; Primary Note: Overview of Oracle Scheduler (Doc ID 1485539.1) Oracle Database Administrators Guide 12c Release 1 (12.1) E17636-21 Chapter(30) Administering Oracle Scheduler Examples of Using the Scheduler http://docs.oracle.com/cd/E166…

stable diffusion实践操作-提示词插件安装与使用

本文专门开一节写提示词相关的内容&#xff0c;在看之前&#xff0c;可以同步关注&#xff1a; stable diffusion实践操作 正文 1、提示词插件安装 1.1、 安装 1.2 加载【应用更改并重载前端】 1.3 界面展示 1.3.-4 使用 里面有个收藏列表&#xff0c;可以收藏以前的所有提示…

安全攻防基础以及各种漏洞库

安全攻防基础以及各种漏洞库 信息搜集企业信息搜集1. 企业架构2. ICP备案查询&#xff0c;确定目标子域名3. 员工信息&#xff08;搜集账号信息、钓鱼攻击&#xff09;4. 社交渠道 域名信息搜集IP搜集信息泄露移动端搜集打点进内网命令和控制&#xff08;持续控制&#xff09;穿…

CSC2121A

半桥架构的栅极驱动电路CSC2121A CSC2121系列是一款高性价比的半桥架构的栅极驱动专用电路&#xff0c;用于大功率MOS管、IGBT管栅极驱动。IC内部集成了逻辑信号处理电路、死区时间控制电路、欠压保护电路、电平位移电路、脉冲滤波电路及输出驱动电路&#xff0c;专用于无刷电…

10 行代码能做什么?

10 行代码能做什么&#xff1f; 写一串 SQL Join&#xff0c;统计全渠道市场营销的 ROI 用 JS 画个饼图&#xff0c;展示最有效的广告投放策略 用 Python 写段算法&#xff0c;分析销量骤降的原因是什么 …… 数据出错了&#xff0c;写个对数脚本 -_-|| AI 时代&#xff0c;…

SpringMVC之文件上传和下载

文章目录 前言一、文件下载二、文件上传总结 前言 实现下载文件和上传文件的功能。 一、文件下载 使用ResponseEntity实现下载文件的功能 RequestMapping("/testDown") public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOEx…

Zookeeper的使用

一、Zookeeper简介 分布式协调框架&#xff0c;小型的树形结构数据共享储存系统。 zookeeper的应用场景 集群管理 注册中心 配置中心 发布者将数据发布到ZooKeeper一系列节点上面&#xff0c;订阅者进行数据订阅&#xff0c;当数据有变化时&#xff0c;可及时得到数据的变…

Java基础二十三(枚举)

1> 枚举 在 Java 中&#xff0c;枚举&#xff08;Enum&#xff09;是一种特殊的数据类型&#xff0c;用于定义一组命名的常量。枚举常用于表示一组相关的常量&#xff0c;并且可以增加额外的属性和方法。 在 Java 中&#xff0c;枚举是一种类&#xff0c;可以定义成员变量和…

如何成为一个开发项目的负责人,需要哪些能力跟作为

目标&#xff1a;帮助项目解决一切障碍&#xff0c;让项目成员每天专心投入到工作中去&#xff0c;宁愿做错&#xff0c;也不能什么都不做。 最终目的&#xff1a;使项目在预期时间内完成&#xff0c;达到预期要求&#xff0c;甚至超出预期效果&#xff0c;同时形成公司自己的…

指针(个人学习笔记黑马学习)

1、指针的定义和使用 #include <iostream> using namespace std;int main() {int a 10;int* p;p &a;cout << "a的地址为&#xff1a;" << &a << endl;cout << "a的地址为&#xff1a;" << p << endl;…

Hive学习(12)Hive常用日期函数

1、hive返回当天三种方式 select current_date; --返回年月日 --2017-06-15 select current_timestamp; --返回年月日时分秒 --2017-06-15 19:54:44 SELECT from_unixtime(unix_timestamp()); --2017-06-15 19:55:042、from_unixtime&#xff1a;转化unix时间戳到当前时区的时…

如何自己实现一个丝滑的流程图绘制工具(八) 创建节点的文本标签

背景 节点的文本标签不希望是通过节点编辑实现&#xff0c;而是拿到节点名字渲染上去&#xff0c;包括连接线 createLabel(element, name, parent) {const modeling this.bpmnModeler.get(modeling)let labelCenter {}// 连接线上的标签if (element.type bpmn:SequenceFlo…

promethues监控postgres,emqx,redis

一、监控postgres 1、安装监控 docker pull wrouesnel/postgres_exporter2、执行 docker run -d -p 9187:9187 --name postgres_exporter --nethost -d -e DATA_SOURCE_NAME"postgresql://postgres:123456192.168.12.116:5432/rcc-manage?sslmodedisable" wroues…

centos 7的超详细安装教程

打开虚拟机&#xff0c;创建一个新电脑 我们选择经典&#xff0c;然后选择下一步 我们选择稍后安装&#xff0c;我们在后面进行改设备 因为centos系统是linux系统的一个版本&#xff0c;所有我们选择linux&#xff0c;版本选择centos 7 64位&#xff0c;然后就是点击下一步 这一…

Hive UDF自定义函数上线速记

0. 编写hive udf函数jar包 略 1. 永久函数上线 1.1 提交jar包至hdfs 使用命令or浏览器上传jar到hdfs,命令的话格式如下 hdfs dfs -put [Linux目录] [hdfs目录] 示例: hdfs dfs -put /home/mo/abc.jar /tmp1.2 将 JAR 文件添加到 Hive 中 注意hdfs路径前面要加上hdfs://na…

四、MySQL(表操作)如何添加字段?修改表?删除字段?修改表名?删除表?格式化某张表?

1、添加字段 &#xff08;1&#xff09;基础语法&#xff1a; alter table 表名 add 字段名 类型名(长度) [comment注释] [约束]; &#xff08;2&#xff09;示例&#xff1a;添加nickname这个字段 2、修改表 修改表中某个字段的【数据类型】/【数据类型&字段名】 &…

Microsoft Edge 主页启动diy以及常用的扩展、收藏夹的网站

一、Microsoft Edge 主页启动diy 二、常用的扩展 1、去广告&#xff1a;uBlock Origin 2、翻译&#xff1a; 页面翻译&#xff1a;右键就有了&#xff0c;已经内置了划词翻译 3、超级复制 三、收藏夹的网站

oracle中的(+)

一、()为何意&#xff1f; oracle中的&#xff08;&#xff09;是一种特殊的用法&#xff0c;&#xff08;&#xff09;表示外连接&#xff0c;并且总是放在非主表的一方。 二、举例 左外连接&#xff1a; select A.a,B.a from A LEFT JOIN B ON A.bB.b; 等价于 select A.a,B.…

Nacos 配置管理及相关使用

文章目录 Nacos 配置管理一、统一配置管理1、在Nacos 中添加配置文件2、从微服务拉取配置3、配置实现步骤&#xff08;1&#xff09;引入 nacos-config 依赖&#xff08;2&#xff09;添加 bootstrap.yml&#xff08;4&#xff09;在 nacos 中添加配置 二、配置热更新1、配置热…