MybatisPlus增删改查,文件上传

使用Mybatis Plus,thymeleaf实现

显示删除(后)

@Controller
public class ResultController {
    @Resource
    private ResultService resultService;

    //显示
    @GetMapping("/index")
    public String text(Model model) {
        List<Result> list = resultService.list();
        model.addAttribute("list", list);
        return "index";
    };

    //删除
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        resultService.removeById(id);
        return "redirect:/index";
    } ;

 

显示删除html页面(前)

<table width="100%" border="1">
    <tr>
        <th>学号</th>
        <th>编号</th>
        <th>日期</th>
        <th>成绩</th>
        <th>操作</th>
    </tr>

    <tr th:each="lis:${list}">
        <td th:text="${lis.studentNo}"></td>
        <td th:text="${lis.subjectNo}"></td>
        <td th:text="${lis.examDate}"></td>
        <td th:text="${lis.studentResult}"></td>
        <td>
            <a th:href="@{/delete/{id}(id=${lis.studentNo})}">删除</a>
        </td>
    </tr>
</table>

 

 

新增

@GetMapping("/add")
public String delete2(){

    return "add";
}
//新增
@PostMapping("/add")
public String add(Inventory inventory){
    System.out.println(inventory.toString());
    inventoryService.save(inventory);
    return "redirect:/index";
}

 

显示新增html页面

<table border="1" width="100%">
    <tr>
        <th>编号</th>
        <th>物品</th>
        <th>价格</th>
        <th>日期</th>
        <th>操作</th>
    </tr>

    <tr th:each="inven:${inventories}">
        <td th:text="${inven.shoppingId}"></td>
        <td th:text="${inven.shoppingName}"></td>
        <td th:text="${inven.shoppingPrice}"></td>
        <!--日期转换-->
        <td th:text="${#dates.format(inven.shoppingTime, 'yyyy-MM-dd HH:mm')}"></td>
        <td>
            <a th:href="@{/delete/{id}(id=${inven.shoppingId})}">删除</a>
        </td>
    </tr>
    <a th:href="@{/add}">保存</a>
</table>

 

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Inventory implements Serializable {

  @TableId(value = "shopping_id",type = IdType.AUTO)
  private Long shoppingId;
  private String shoppingName;
  private double shoppingPrice;
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  private Date shoppingTime;

}

 

 

Service

@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper,Inventory> implements InventoryService {}
//service接口
public interface InventoryService extends IService<Inventory> {
}

 

Mapper

public interface InventoryMapper extends BaseMapper<Inventory> {
}

 

更新

//查询到id
@GetMapping("/update/{id}")
public String update(@PathVariable("id") Long id,Model model){
    System.out.println(id);
    Inventory inventory = inventoryService.getById(id);
    model.addAttribute("inventory",inventory);
    return "update";
}
//更新后返回
@PostMapping("/updateInfo")
public String updateInfo(Inventory inventory){
    System.out.println(inventory);
    boolean update = inventoryService.updateById(inventory);
    return "redirect:/index";
}

 

显示更新html

<body>
<form th:action="@{/updateInfo}" method="post">
    <input type="hidden" name="shoppingId" th:value="${inventory.shoppingId}"/>
    姓名:<input type="text" name="shoppingName" th:value="${inventory.shoppingName}">
    价格:<input type="text" name="shoppingPrice" th:value="${inventory.shoppingPrice}">
    日期:<input type="date" name="shoppingTime" th:value="${#dates.format(inventory.shoppingTime, 'yyyy-MM-dd')}">
    <button type="submit">提交</button>
</form>
</body>

 

文件上传

//文件上传
@GetMapping("/file")
public String File(){
    return "file";
}

@PostMapping("/upload")
public String upload(@RequestParam("headImg") MultipartFile multipartFile) throws IOException {
    if(!multipartFile.isEmpty()){
        String name = multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File("D:\\" + name));
    }
    return "file";
}

 

文件上传html

 

 

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="headImg" />
    <button type="submit">提交</button>
</form>

 

 

 

posted @ 2021-02-04 18:16  蜗丿牛  阅读(219)  评论(0)    收藏  举报