MybatisPlus,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";
    }

    ;

显示删除jsp页面

<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>

日志

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

新增

@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";
}

显示新增jsp页面

<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 {}

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";
}

显示更新jsp

<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";
}

文件上传jsp

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

模糊查询

  @PostMapping("/likeQuery")
    public String likeQuery(@RequestParam("loveName") String loveName, ModelMap modelMap, QueryWrapper<Love> wrapper) {
        wrapper.like("love_name", loveName);
        List<Love> pol = loveService.list(wrapper);
        modelMap.addAttribute("pol",pol);
        //回填
        modelMap.addAttribute("lovename",loveName);
        return "index";
    }

JS页面

<p>
    <form action="/likeQuery" method="post">
        模糊查询:<input type="text" name="loveName" th:value="${lovename}" />
        <button type="submit">查询</button>
    </form>
</p>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值