银行业务管理系统
package com.example.bankaccesssystem.controller;
import com.example.bankaccesssystem.model.Bank;
import com.example.bankaccesssystem.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class BankController {
@Autowired
private BankService bankService;
// display list of employees
// @GetMapping("/")
// public String viewHomePage(Model model) {
// ("listEmployees", ());
// return "index";
// }
// 跳转
@GetMapping("/")
public String viewHomePage(Model model) {
return findPaginated(1, "bookName", "asc", model);
}
@GetMapping("/showNewBankForm")
public String showNewBankForm(Model model) {
// create model attribute to bind form data
Bank bank = new Bank();
model.addAttribute("bank", bank);
return "new_bank";
}
@GetMapping("/query{inpname}")
public String query(@PathVariable(value = "inpname") String inpame, Model model) {
List<Bank> listgender = bankService.findAllByBookName(inpame);
model.addAttribute("listBank", listgender);
return "index";
}
//添加
@PostMapping("/saveBank")
public String saveBank(@ModelAttribute("bank") Bank bank) {
// save employee to database
bankService.saveBank(bank);
return "redirect:/";
}
//更新跳转
@GetMapping("/showFormForUpdate/{id}")
public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
// get employee from the service
Bank bank = bankService.getBankByTd(id);
// set employee as a model attribute to pre-populate the form
model.addAttribute("bank", bank);
return "update_bank";
}
//删除
@GetMapping("/deleteBank/{id}")
public String deleteBank(@PathVariable(value = "id") long id) {
// call delete employee method
this.bankService.deleteBankById(id);
return "redirect:/";
}
//获取分页数据
@GetMapping("/page/{pageNo}")
public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
@RequestParam("sortField") String sortField,
@RequestParam("sortDir") String sortDir,
Model model) {
int pageSize = 5;
//分页查询
Page<Bank> page = bankService.findPaginated(pageNo, pageSize, sortField, sortDir);
List<Bank> listBank = page.getContent();
model.addAttribute("currentPage", pageNo);
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("sortField", sortField);
model.addAttribute("sortDir", sortDir);
model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
model.addAttribute("listBank", listBank);
return "index";
}
}