菜单模块

该博客介绍了如何使用MyBatis框架在Java中创建一个菜单管理系统的实现。内容包括创建数据库表`menu`,定义`Menu`实体类,设置日期格式,并在`MenuController`中实现增删改查操作。`MenuMapper`接口提供了与数据库交互的方法,如新增菜品、修改菜品、删除菜品和根据ID或名称查询菜品。

先在数据库建立一个菜单menu   

CREATE TABLE `menu` (
	`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
	`name` VARCHAR(50) NOT NULL COMMENT '菜单' COLLATE 'utf8_general_ci',
	`descc` VARCHAR(50) NOT NULL COMMENT '描述' COLLATE 'utf8_general_ci',
	`count` INT(11) NOT NULL DEFAULT '0' COMMENT '数量',
	`picture` VARCHAR(255) NULL DEFAULT NULL COMMENT '图片地址' COLLATE 'utf8_general_ci',
	`create_time` DATETIME NULL DEFAULT NULL COMMENT '创建时间',
	`update_time` DATETIME NULL DEFAULT NULL COMMENT '修改时间',
	PRIMARY KEY (`id`) USING BTREE
)

在Menu类中建立

package com.sdcet.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;//private Date

public class Menu {
    private Long id;
    private String name;
    private String descc;
    private Integer count;
    private String picture;
//    private Date createTime;
//    private Date updateTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone ="Asia/Shanghai")
    private Date createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone ="Asia/Shanghai")
    private Date updateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescc() {
        return descc;
    }

    public void setDescc(String descc) {
        this.descc = descc;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

在MenuController类中建立

package com.sdcet.constroller;

import com.sdcet.entity.Menu;
import com.sdcet.mapper.MenuMapper;
import com.sdcet.mapper.UserMapper;
import com.sdcet.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@ResponseBody
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuMapper menuMapper;

    @PostMapping("/save")//新增菜品
    public ResponseMessage save(@RequestBody Menu menu){
        Date date = new Date();
        menu.setCreateTime(date);
        menu.setUpdateTime(date);
        return ResponseMessage.success(menuMapper.save(menu));
    }

    @PutMapping("/update")//修改菜品
    public ResponseMessage update(@RequestBody Menu menu){
        return ResponseMessage.success(menuMapper.update(menu));
    }

    @DeleteMapping("/delete")//删除菜品
    public ResponseMessage delete(@RequestBody Long id){
        return ResponseMessage.success(menuMapper.delete(id));
    }

    @GetMapping("getMenuById")//id查询
    public ResponseMessage getMenuById(@RequestBody Long id){
        return ResponseMessage.success(menuMapper.selectMenuById(id));
    }

    @GetMapping("getMenuByName")//名字查询
    public ResponseMessage getMenuByName(@RequestBody String name){
        return ResponseMessage.success(menuMapper.selectMenuByName(name));
    }



}

在MenuMapper类建立与数据库连接

package com.sdcet.mapper;

import com.sdcet.entity.Menu;
import com.sdcet.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MenuMapper {
    @Insert("INSERT INTO menu (name,descc,count,picture,create_time,update_time) VALUES (#{name},#{descc},#{count},#{picture},#{createTime},#{updateTime})")
    int save (Menu menu);//新增菜品

    @Update("UPDATE menu SET name=#{name},descc=#{descc},count=#{count},picture=#{picture},create_time=#{createTime},update_time=#{updateTime} WHERE `id`=#{id}" )
    int update(Menu menu);//WHERE前面加空格 修改菜品



    @Delete("DELETE FROM menu WHERE `id`=#{id}")
    int delete(Long id);//删除菜品


    @Select("SELECT *FROM menu WHERE `id`=#{id}")
    int selectMenuById(Long id);//通过id查询菜品

    @Select("SELECT id,`name`, descc,count,picture,create_time,update_time FROM menu WHERE `name`=#{name}")
    List<User> selectMenuByName(String name);//名字查询

}

INSERT INTO 语句用于向表格中插入新的行。INSERT INTO 表名称 VALUES (值1, 值2,....)

UPDATE menu SET name=#{name}    updata更新   set设置

SELECT + 表字段名 + FROM +数据表名+ WHERE + 筛选条件       

SELECT * FROM 获取每条信息全部字段的值

注意在Postman中   eg:http://127.0.0.1:8080/menu/delete?id=5     不要留有空格

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值