SpringBoot实现增删改查

Contrller层

DishContrller

package com.example.springboot.controller;

import com.example.springboot.entity.Dish;
import com.example.springboot.mapper.DishMapper;
import com.example.springboot.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: bw
 * @Data: 2024/3/11 16:31
 * @description: TODO
 */
@RestController
@RequestMapping("/dish")
public class DishContrller {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishService dishService;

    //查询所有菜品信息
    @GetMapping("/findAll")
    public List<Dish> index(){
        Dish dish = new Dish();
        dish.setDish_id(1);
        List<Dish> all = dishMapper.findAll();
        return all;
    }
    //新增菜品信息,新增菜品信息时,返回数据库更改的条数
    @PostMapping("/insertDish")
    public Integer save(@RequestBody Dish dish){//前台传入一个json数据的时候,它可以把json数据映射成dish对象
        //新增或编辑
        return dishService.save(dish);
    }
    //删除菜品信息
    @DeleteMapping("/deleteDish/{id}")/*和这个id要保持一致(@PathVariable Integer id){*/
    public Integer delete(@PathVariable Integer id){
        return dishMapper.deleteById(id);
    }
}

Service层

DishService

package com.example.springboot.service;

import com.example.springboot.entity.Dish;
import com.example.springboot.mapper.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: bw
 * @Data: 2024/3/12 13:12
 * @description: TODO
 */
@Service /*把这个类注入到springboot容器中 要不然找不到这个类*/
public class DishService {
    @Autowired
    private DishMapper dishMapper;
    public int save(Dish dish){
        if(dish.getDish_id() == null){//dish没有id则表示是新增
            return dishMapper.insert(dish);
        }else{//否则为更新
            return dishMapper.update(dish);
        }
    }
}

Mapper层

DishMapper

package com.example.springboot.mapper;

import com.example.springboot.entity.Dish;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: bw
 * @Data: 2024/3/11 16:16
 * @description: TODO
 */
@Mapper
public interface DishMapper {
    @Select("SELECT * FROM sys_dish")
    List<Dish> findAll();

    @Insert("INSERT INTO sys_dish(dish_name,dish_category,dish_price,dish_description,dish_image_path) VALUES (#{dish_name},#{dish_category},#{dish_price},#{dish_description},#{dish_image_path})")
    int insert(Dish dish);

    int update(Dish dish);

    @Delete("delete from sys_dish where dish_id=#{dish_id}")
    Integer deleteById(@Param("dish_id") Integer id);/*@Param("dish_id")要和dish_id=#{dish_id}一一对应*/
}

Vo层

Dish

package com.example.springboot.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @Author: bw
 * @Data: 2024/3/11 13:57
 * @description: TODO
 */
@Data//创造get set方法
@NoArgsConstructor//创造无参构造
@AllArgsConstructor//创造有参构造
public class Dish {
    private Integer dish_id;
    private String dish_name;
    private String dish_category;
    private String dish_description;
    private BigDecimal dish_price;
    private String dish_image_path;
}

动态SQL

dish.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.example.springboot.mapper.DishMapper">
    <!--id="update" 对应DishMapper中的int update(Dish dish);-->
    <update id="update">
        update sys_dish
        <set>
            <if test="dish_name!=null">
                dish_name=#{dish_name},
            </if>
            <if test="dish_category!=null">
                dish_category=#{dish_category},
            </if>
            <if test="dish_price!=null">
                dish_price=#{dish_price},
            </if>
            <if test="dish_description!=null">
                dish_description=#{dish_description},
            </if>
            <if test="dish_image_path!=null">
                dish_image_path=#{dish_image_path}
            </if>
        </set>
        <where>
            dish_id=#{dish_id}
        </where>
    </update>
</mapper>

配置文件

application.yml

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bw_db?serverTimezone=GMT%2b8
    username: root
    password: 123456
mybatis:
  mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的xml文件
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.2.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

查询测试结果

在这里插入图片描述

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值