昨天看雷哥的教程写了个简单的记账本练练手,没有把笔记整理下来放在博客上,今天补上。言归正传,进入正题。
登陆界面:
创建数据库
添加对应的pom依赖,修改yml文件,以及对应的vo(domain),mapper,service,controller,还有mapper映射文件)
@Data @AllArgsConstructor @NoArgsConstructor public class ResultObj { //业务响应码 private int code; //业务消息 private String msg; }
@Controller @RequestMapping("/login") public class LoginController { @Autowired private UserService userService; /** * 跳转到登陆页面 */ @RequestMapping("toLogin") public String toLogin() { return "login"; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> window.location.href="/login/toLogin" </script> </body> </html>
@Data @AllArgsConstructor @NoArgsConstructor public class DataGridView { private Long code=0L; private String msg=""; private Long count; private Object data; public DataGridView(Long count,Object data){ super(); this.count=count; this.data=data; } }
/** *mybatisplus的配置类(分页) */ @Configuration @ConditionalOnClass(value = {PaginationInterceptor.class}) public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } }
@Controller @RequestMapping("/bills") public class BillsController { @Autowired private BillsService billsService; @Autowired private BilltypeService billtypeService; /** * 跳转到系统主页 */ @RequestMapping("toBillsList") public String toBillsList(){ return "list"; } /** *加载账单数据 */ @RequestMapping("loadAllBills") @ResponseBody public DataGridView loadAllBills(BillsVo billsVo){ //这里使用分页插件 IPage<Bills> page=new Page<>(billsVo.getPage(),billsVo.getLimit()); QueryWrapper<Bills> queryWrapper=new QueryWrapper<>(); //等于 queryWrapper.eq(null!=billsVo.getTypeid()&&billsVo.getTypeid()!=0,"typeid",billsVo.getTypeid()); //大于 queryWrapper.ge(billsVo.getStartDate()!=null,"billtime",billsVo.getStartDate()); //小于 queryWrapper.le(billsVo.getEndDate()!=null,"billtime",billsVo.getEndDate()); //排序 queryWrapper.orderByDesc("billtime"); billsService.page(page,queryWrapper); List<Bills> records=page.getRecords(); for (Bills bills:records){ Billtype billtype=this.billtypeService.getById(bills.getTypeid()); bills.setTypeName(billtype.getName()); } return new DataGridView(page.getTotal(),records); } /** * 添加账单 */ @RequestMapping("addBills") @ResponseBody public ResultObj addBills(BillsVo billsVo){ try { this.billsService.save(billsVo); return new ResultObj(200, "录入成功"); } catch (Exception e) { e.printStackTrace(); return new ResultObj(-1, "录入失败"); } } }
添加记录: