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

目录

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

创建controller层

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

java 复制代码
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{

    @Resource
    private ITestService testService;

    /**
     * 查询列表
     */
    @ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")
    @GetMapping("/list")
    public List<TestEntity> list(TestEntity sysTest) {
        return testService.selectTestList(sysTest);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public boolean add(@Validated @RequestBody TestDTO testDTO) {
        return testService.insertTest(testDTO);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新")
    @PutMapping
    public 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层接口

java 复制代码
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层实现

java 复制代码
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 {

    @Resource
    private TestMapper testMapper;


    /**
     * 查询测试表列表
     *
     * @param testEntity 测试表Entity类
     * @return 测试表
     */
    @Override
    public List<TestEntity> selectTestList(TestEntity testEntity) {
        return testMapper.selectTestList(testEntity);
    }

    /**
     * 新增测试表
     *
     * @param testDTO 测试表DTO类
     * @return 结果
     */
    @Override
    public boolean insertTest(TestDTO testDTO) {

        TestEntity testInfoEntity = new TestEntity();
        BeanUtils.copyProperties(testDTO, testInfoEntity);

        return this.save(testInfoEntity);
    }

    /**
     * 更新测试表
     *
     * @param testDTO 测试表DTO类
     * @return 结果
     */
    @Override
    public boolean updateTest(TestDTO testDTO) {

        TestEntity testInfoEntity = new TestEntity();
        BeanUtils.copyProperties(testDTO, testInfoEntity);

        return this.updateById(testInfoEntity);
    }


    /**
     * 详情测试表
     *
     * @param id id值
     * @return 结果
     */
    @Override
    public TestVO getTestById(Integer id) {

        TestEntity testEntity = this.getById(id);
        TestVO testVO = new TestVO();
        BeanUtils.copyProperties(testEntity, testVO);

        return testVO;
    }


    /**
     * 删除测试表
     *
     * @param ids id数组
     * @return 结果
     */
    @Override
    public boolean removeTestByIds(Integer[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }


}

添加mapper层

java 复制代码
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 复制代码
<?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文件的目录

java 复制代码
@MapperScan("com.onejson.ojmall.mapper")
相关推荐
nice_lcj5206 小时前
MySQL 的锁:行锁、表锁、间隙锁与死锁
mysql·面试·锁·行锁·死锁·间隙锁
兴趣互联8 小时前
jeecgboot nacos2.3.2 使用pgsql
java
hudd6688 小时前
JavaEE 初阶 - 文件操作和IO
java·开发语言·java-ee
Flynn Liang9 小时前
HikariCP 连接池不是越大越好:I/O 密集型 Java 服务的数据库容量工程
java·数据库·mysql·高并发·连接池·hikaricp·sql 优化
学长毕设9 小时前
基于SpringBoot社区快递驿站物流配送系统-附源码
java·spring boot·后端
Flynt10 小时前
Java4Shell 实测:EPYC 一跑就中招,关掉 Base64 硬件加速,1KB 解码慢 14 倍
java·安全·性能优化
Resean022310 小时前
Spring 源码分析(七)AOP 原理(上):代理的创建
java·后端·spring
Tangq一定可以的10 小时前
Spring AI Advisor 详解 - 给大模型调用加上“切面“,日志/过滤/记忆一键搞定
java·后端·spring
小巫编程室11 小时前
我造了一个 Java 能力包:让 AI 直接调用你的业务代码
java·前端·后端
雾隐隐o11 小时前
优搭校园 01|项目概览、技术选型与初始化
spring boot·mysql·vue