Springboot簡單練手的記賬本


Springboot簡單練手的記賬本

昨天看雷哥的教程寫了個簡單的記賬本練練手,沒有把筆記整理下來放在博客上,今天補上。言歸正傳,進入正題。

老規矩,我們還是先看看項目的目錄結構,以及登陸界面

每個包都是什么含義這里就不再細說了1
登陸界面:
2

一、准備工作

創建數據庫
3

二、生成代碼

添加對應的pom依賴,修改yml文件,以及對應的vo(domain),mapper,service,controller,還有mapper映射文件)

三、導入資源文件

4

四、完成登錄

  1. 創建ResultObj
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultObj {

    //業務響應碼
    private int code;
    //業務消息
    private String msg;
}
  1. 創建LoginController(代碼太多,這里不再詳細給出)
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 跳轉到登陸頁面
     */
    @RequestMapping("toLogin")
    public String toLogin() {
        return "login";
    }
  1. 創建static/index.html(代碼太多,這里不再詳細給出)
<!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>
  1. 創建templates/login.html
  2. 創建templates/list.html
    (上述兩條代碼過長,不再贅述,到時候會一並上傳的,有迫切需要的可以聯系我)

五、賬單列表(這里代碼就不再給出了,過於冗余,之后會上傳代碼的)

  1. 創建DataGridView
@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;
    }
}
  1. 創建MyBatisPlusConfig
/**
 *mybatisplus的配置類(分頁)
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

  1. 創建BillsController
@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, "錄入失敗");
        }
    }
}

  1. 創建Bills
  2. 創建BillsVo
  3. 創建BillsTypeServiceImpl
  4. 創建緩存處理切面類
  5. 加入依賴
  6. 創建BillTypeController
  7. 創建list.html

六、添加賬單

  1. 修改list.html
  2. 修改BillsController
七、這篇文章說到這里,關於Mapper(即dao層),service,serviceImpl,以及xxxMapper.xml文件,這里並沒有提到,不是沒有,只是這里是通過mybatis-plus,使用接口進行操作的,省去了自己寫代碼的那一步,本篇就不再介紹了,之后會專門寫一篇進行記錄的。

八、看看效果:

6
添加記錄:
7

九、 大致就是這么多,對了該項目使用的是mybatis-plus,還有huTool工具包,很多地方都是使用的接口,不用自己去寫代碼,之后,會寫一篇有關mybatis-plus和huTool工具包的博客,希望大家可以留意一下!謝謝!

天涯志


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM