建立数据表
名称 | 数据类型 | 允许NULL |
---|---|---|
id | BIGINT | 否 |
name | VARCHAR(50) | 是 |
des | VARCHAR(50) | 是 |
price | DOUBLE | 否 |
num | BIGINT | 是 |
img | VARCHAR(50) | 是 |
create_time | DATETIME | 是 |
update_time | DATETIME | 是 |
建立Menu类
public class Menu {
private Long id;
private String name;
private String des;
private Double price;
private Long num;
private String img;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
private Date create_time;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
private Date update_time;
}
注意生成get和set方法
建立MenuMapper
写入 id查看数据
、name查看数据
、添加数据
,修改数据
,删除数据
接口:
package com.kenvie.demo2.mapper;
import com.kenvie.demo2.entity.Menu;
import org.apache.ibatis.annotations.*;
@Mapper
public interface MenuMapper {
@Select("SELECT * FROM menu WHERE id = #{id}")
Menu selectMenuById(Long id);
@Select("SELECT * FROM menu WHERE name = #{name}")
Menu selectMenuByName(String name);
@Insert("insert into menu(name,des,price,num,img,create_time,update_time) values (#{name},#{des},#{price},#{num},#{img},#{create_time},#{update_time})")
int insertMenu(Menu menu);
@Update("update menu set name = #{name},des = #{des},price = #{price},num = #{num},img = #{img},create_time = #{create_time},update_time = #{update_time} where id = #{id}")
int updateMenuById(Menu menu);
@Delete("DELETE FROM menu WHERE id = #{id}")
int deleteMenuById(Long id);
}
建立MenuController
写方法调用这五个接口
package com.kenvie.demo2.controller;
import com.kenvie.demo2.entity.User;
import com.kenvie.demo2.mapper.UserMapper;
import com.kenvie.demo2.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("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
/**
* GET - 查看一条数据
* @param id
* @return
*/
@GetMapping("/selectuserbyid")
public ResponseMessage getUserById(Long id){
return ResponseMessage.success(userMapper.selectUserById(id));
}
@GetMapping("/selectuserbyname")
public ResponseMessage getUserByName(String name){
return ResponseMessage.success(userMapper.selectUserByName(name));
}
/**
* POST - 创建一条数据
* @param user
* @return
*/
@PostMapping ("/insertuser")
public ResponseMessage insertUser(@RequestBody User user){
if (user.getName() == null || "".equals(user.getName())){
return ResponseMessage.fail("返回值 ‘name’ 不能为null ");
}
if(user.getPasswd() == null || "".equals(user.getPasswd())){
return ResponseMessage.fail("返回值 ‘passwd’ 不能为null ");
}
Date date = new Date();
user.setCreate_time(date);
user.setUpdate_time(date);
return ResponseMessage.success(userMapper.insertUser(user));
}
/**
* PUT - 修改数据
* @param user
* @return user
*/
@PutMapping("/updatauser")
public ResponseMessage updataUser(@RequestBody User user){
if (user.getName() == null || "".equals(user.getName())){
return ResponseMessage.fail("返回值 ‘name’ 不能为null ");
}
if(user.getPasswd() == null || "".equals(user.getPasswd())){
return ResponseMessage.fail("返回值 ‘passwd’ 不能为null ");
}
user.setUpdate_time(new Date());
return ResponseMessage.success(userMapper.updataUserById(user));
}
@DeleteMapping("/deleteuser")
public ResponseMessage deleteUser(Long id){
return ResponseMessage.success(userMapper.deleteUser(id));
}
}
调用
根据id获取菜品数据
- 端口号:
8080
- 路径:
/menu/selectmenubyid
- key:
id
示例:
http://127.0.0.1:8080/menu/selectmenubyid?id=1
根据name获取菜品数据
- 端口号:
8080
- 路径:
/menu/selectmenubyname
- key:
name
示例:
http://127.0.0.1:8080/menu/selectmenubyname?name=aa
添加菜品
- 端口号:
8080
- 路径:
/menu/insertmenu
示例:
{
"name":"三鲜",
"des":"美味",
"num":65
}
根据id修改菜品数据
- 端口号:8080
- 路径:/menu/updatamenubyid
- key:id
示例:
{
"id":1,
"des":"很好吃",
"price":"9.9"
}
根据id删除菜品
- 端口号:
8080
- 路径:
/menu/updatamenubyid
- key:
id
示例:
http://127.0.0.1:8080/menu/deletemenubyid?id=1