【问题标题】:How to remove multiple rows via checkbox如何通过复选框删除多行
【发布时间】:2022-01-31 00:16:02
【问题描述】:

我是 spring 新手,我需要能够通过复选框删除选定的行。如何从数据库中删除多行?我使用 Thymeleaf 作为视图。

这是我的代码:

HTML

<div class="col-md-12">
     <table class="table" id="tableImport">
         <thead>
             <tr>
                <th scope="col">Remove</th>
                <th scope="col">Debt Age Rule</th>
                <th scope="col">Reminder</th>
                <th scope="col">Frequency</th>
                <th scope="col">Reorder</th>
             </tr>
         </thead>
         <tbody>
             <tr th:each="configCampaign:${listConfigCampaigns}">
                <td>
                   <input type="checkbox" name="my-checkbox">
                </td>
                <td th:text="${configCampaign.debtagerule}"></td>
                <td th:text="${configCampaign.remindebttype}"></td>
                <td th:text="'Every '+${configCampaign.every} + ' ' + ${configCampaign.unit}"></td>
                <td></td>
             </tr>
         </tbody>
     </table>
     <div class="col-md-12" style="text-align: center">
         <input class="btn btn-primary" type="button" value="- Remove Selected Action(s)"/>
     </div>
</div>

此表显示来自内存中 arrayList 的数据,没有数据库,我需要从数组中删除那些选定的对象。目前我有这样的控制器

实体

private int configid;
private String debtagerule;
private String remindebttype;
private int every;
private String unit;
private boolean selected;

//getters and setters

除了上述之外,这是我目前正在使用的控制器

控制器

@GetMapping("/deleteConfigureCampaign")
public String deleteConfig(@ModelAttribute ConfigCampaign configCampaign, Model model) {
    listConfigCampaigns.remove(configCampaign);
    return "redirect:/configureCampaign";
}

【问题讨论】:

  • 你的ControllerService 在哪里。从数据库中删除项目或从页面中临时删除?
  • @GetMapping("/deleteConfigureCampaign") public String deleteConfig(@ModelAttribute ConfigCampaign configCampaign, 模型模型) { listConfigCampaigns.remove(configCampaign);返回“重定向:/configureCampaign”; } 这是我的控制器,想法是删除临时对象
  • 回答正确请采纳

标签: java spring-boot thymeleaf


【解决方案1】:

在 Spring Boot 中,您必须使用JpaRepository&lt;&gt; 从数据库中删除数据,并且需要了解 Spring Boot 项目的结构。

这是 Spring Boot 项目的结构:

Entity -> Repository -> Service -> Controller -> View.

下面是代码:

实体

@Table(name = "config_master")
public class ConfigCampaign {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer configid;
    private String debtagerule;
    private String remindebttype;
    private Integer every;
    private String unit;
    private boolean selected;
    // Constructor, Getter and Setter
}

存储库

  • @Modifying注解的使用:用于增强@Query注解,使我们不仅可以执行SELECT查询,还可以执行INSERT、UPDATE、DELETE甚至DDL查询
@Repository
public interface ConfigRepo extends JpaRepository<ConfigCampaign, Integer>{
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "DELETE FROM config_master WHERE configid IN(?1)")
    void delConfig(List<Integer> configId);
}

服务

@Service
public class PojoServiceImpl{
    
    @Autowired
    private ConfigRepo configRepo;

    @Override
    public void delConfig(Integer[] configId) {
        configRepo.delConfig(Arrays.asList(configId));
    }
}

控制器


// Show table of Config Campaign
@RequestMapping(value = "/showconfig", method = RequestMethod.GET)
public String getConfig(Model mdl)
{
    List<ConfigCampaign> getConfig = pojoService.getAllConfig();
    mdl.addAttribute("config", getConfig);
    return "showConfigCampaign";
}

// Delete item from Config Campaign
@RequestMapping(value = "/delcampaign", method = RequestMethod.GET)
public String deleteConfig(@RequestParam("cid") Integer[] configId)
{
    pojoService.delConfig(configId);
    return "redirect:/showconfig";
}

showConfigCampaign

您必须在复选框中添加configData.configid

<form th:action="@{/delcampaign}" th:object="${sconfig}">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <table class="table" style="text-align: center">
                  <thead>
                    <tr>
                      <th scope="col">Remove</th>
                      <th scope="col">Debt Age Rule</th>
                      <th scope="col">Reminder</th>
                      <th scope="col">Frequency</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr th:each="configData: ${config}">
                      <td><input type="checkbox" name="cid" th:value="${configData.configid}"/></td>
                      <td th:text="${configData.debtagerule}"></td>
                      <td th:text="${configData.remindebttype}"></td>
                      <td th:text="${configData.every}"></td>
                    </tr>
                  </tbody>
                </table>
                
                <input type="submit" value="Delete Users" />
            </div>
        </div>
    </div>
</form>

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 2013-01-06
    • 2010-11-20
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多