html css js原生前端+springboot实现前后端分离的计算器

1.git仓库链接和代码规范链接

2.psp表格

3.成品展示:

 4.设计实现过程:

5.代码说明:

         前端部分:

后端部分:

6.心路历程和收获


112100833_Calculator
这个作业属于哪个课程https://bbs.csdn.net/forums/ssynkqtd-05
这个作业要求在哪里https://bbs.csdn.net/topics/617377308
这个作业的目标继续实现更完善的计算器功能并实现前后端分离
其他参考文献

1.git仓库链接和代码规范链接

前端:112100833黄咏清 / 112100833hyq_calculator_temp · GitCode

后端:112100833黄咏清 / 112100833hyq_cal_backport · GitCode

2.psp表格

PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划3035
• Estimate• 估计这个任务需要多少时间1510
Development开发900990
• Analysis• 需求分析 (包括学习新技术)3030
• Design Spec• 生成设计文档6075
• Design Review• 设计复审3045
• Coding Standard• 代码规范 (为目前的开发制定合适的规范)30100
• Design• 具体设计60100
• Coding• 具体编码900970
• Code Review• 代码复审6060
• Test• 测试(自我测试,修改代码,提交修改)7050
Reporting报告9060
• Test Repor• 测试报告3020
• Size Measurement• 计算工作量1510
• Postmortem & Process Improvement Plan• 事后总结, 并提出过程改进计划4530
合计30253375

3.成品展示:

功能1:四则运算、取余、括号

功能2:清零回退

 功能3:错误提示

 功能4:读取历史记录

 附加功能1:科学计算器

 页面原型设计:

 利率计算器:

 4.设计实现过程:

1.功能结构图

2.前端:html+css+js 原生写代码,直接用Fetch API、XMLHttpRequest(XHR)(原生请求库)发送请求,成功和后端对接

3.后端:用springboot作为开发框架,在orm层用mybatis框架

4.数据库:存历史记录以及数据,用了可视化工具navicat,共有三个表

5.代码说明:

 前端部分:

1.使用了js中的Fetch API ,向本地主机 http://localhost:8082/history 发送了一个 GET 请求,并且在响应返回后对数据进行处理。

 
  1. function getHistory() {

  2. fetch('http://localhost:8082/history')

  3. .then(response => response.json())

  4. .then(data => {

  5. // 将数据传递给构建表格的函数

  6. buildTable(data);

  7. });

  8. }

  9. function buildTable(data) {

  10. let table = document.getElementById('history');

  11. // 创建表格行并添加到表格中

  12. data.forEach(function(rowData) {

  13. let row = table.insertRow();

  14. Object.values(rowData).forEach(function(value) {

  15. let cell = row.insertCell();

  16. cell.innerHTML = value;

  17. });

  18. });

  19. }

2.使用XMLHttpRequest(XHR)对象发送 GET 请求的 JavaScript 代码

 
  1. function interfun(){

  2. // 获取输入

  3. var year=parseFloat(document.getElementById("caltype").value);

  4. var xhr = new XMLHttpRequest();

  5. var principal=parseFloat(document.getElementById("principal").value);

  6. xhr.open('GET', `http://localhost:8082/interest?time=${year}`, true);

  7. xhr.onload = function () {

  8. if (xhr.status >= 200 && xhr.status < 300) {

  9. var rate = xhr.responseText;

  10. const interest=rate*principal*year/100;

  11. document.getElementById("result").innerHTML=interest.toFixed(2);

  12. } else {

  13. console.error('Request failed with status', xhr.status);

  14. }

  15. };

  16. xhr.onerror = function () {

  17. console.error('Request failed due to a network error');

  18. };

  19. xhr.setRequestHeader('Content-Type', 'text/plain');

  20. xhr.send();

  21. }

  22. function loanfun(){

  23. // 获取输入

  24. var year=parseFloat(document.getElementById("caltype").value);

  25. var xhr = new XMLHttpRequest();

  26. var principal=parseFloat(document.getElementById("principal").value);

  27. xhr.open('GET', `http://localhost:8082/loan?time=${year}`, true);

  28. xhr.onload = function () {

  29. if (xhr.status >= 200 && xhr.status < 300) {

  30. var rate = xhr.responseText;

  31. const loan=rate*principal*year/100;

  32. document.getElementById("result").innerHTML=loan.toFixed(2);

  33. } else {

  34. console.error('Request failed with status', xhr.status);

  35. }

  36. };

  37. xhr.onerror = function () {

  38. console.error('Request failed due to a network error');

  39. };

  40. xhr.setRequestHeader('Content-Type', 'text/plain');

  41. xhr.send();

  42. }

后端部分:

API代码(计算器部分):

 
  1. @Controller

  2. public class calController {

  3. @Resource

  4. private ResultRepository ResultRepository;

  5. @Resource

  6. private ResultService ResultService;

  7. @GetMapping("/test1")

  8. @ResponseBody

  9. public String test1(){

  10. return ResultService.list().toString();

  11. }

  12. @GetMapping("/save")

  13. @CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})

  14. @ResponseBody

  15. public void resSave(@RequestParam("formula") String formula,@RequestParam("result")String result){

  16. ResultService.save(new Result(formula,result));

  17. }

  18. @GetMapping("/history")

  19. @CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})

  20. @ResponseBody

  21. public List<Result> GetHistory(){

  22. QueryWrapper<Result> resultQueryWrapper=new QueryWrapper<>();

  23. return ResultService.list();

  24. }

  25. }

API代码(存款):

 
  1. public class InterestRateController {

  2. @Resource

  3. private rateServer rateserver;

  4. @Resource

  5. private InterestRateRepository rateRepository;

  6. // @RequestMapping("/helloo")

  7. // @ResponseBody

  8. // public String hello(){return "Hello world!";}

  9. @GetMapping("/interest")

  10. @CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})

  11. @ResponseBody

  12. public String interestCal(@RequestParam("time") String time){

  13. QueryWrapper<InterestRate> intQueWrapper=new QueryWrapper<>();

  14. double year= Double.parseDouble(time);

  15. if(year==0.25)

  16. {

  17. intQueWrapper.eq("rate_type","三个月");

  18. }

  19. else if(year==0.5)

  20. {

  21. intQueWrapper.eq("rate_type","半年");

  22. }

  23. else if(year==1)

  24. {

  25. intQueWrapper.eq("rate_type","一年");

  26. }

  27. else if(year==2)

  28. {

  29. intQueWrapper.eq("rate_type","二年");

  30. }

  31. else if(year==3)

  32. {

  33. intQueWrapper.eq("rate_type","三年");

  34. }

  35. else if(year==5)

  36. {

  37. intQueWrapper.eq("rate_type","五年");

  38. }

  39. else

  40. {

  41. intQueWrapper.eq("rate_type","活期存款");

  42. }

  43. InterestRate intRate=rateRepository.selectOne(intQueWrapper);

  44. String result=Double.toString(intRate.getRate());

  45. return result;

  46. }

  47. }

API代码(贷款):

public class InterestRateController {

    @Resource
    private rateServer rateserver;
    @Resource
    private InterestRateRepository rateRepository;
//    @RequestMapping("/helloo")
//    @ResponseBody
//    public String hello(){return "Hello world!";}
    @GetMapping("/interest")
    @CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String interestCal(@RequestParam("time") String time){
        QueryWrapper<InterestRate> intQueWrapper=new QueryWrapper<>();
        double year= Double.parseDouble(time);
        if(year==0.25)
        {
            intQueWrapper.eq("rate_type","三个月");
        }
        else if(year==0.5)
        {
            intQueWrapper.eq("rate_type","半年");
        }
        else if(year==1)
        {
            intQueWrapper.eq("rate_type","一年");
        }
        else if(year==2)
        {
            intQueWrapper.eq("rate_type","二年");
        }
        else if(year==3)
        {
            intQueWrapper.eq("rate_type","三年");
        }
        else if(year==5)
        {
            intQueWrapper.eq("rate_type","五年");
        }
        else
        {
            intQueWrapper.eq("rate_type","活期存款");
        }
        InterestRate intRate=rateRepository.selectOne(intQueWrapper);
        String result=Double.toString(intRate.getRate());
        return result;
    }
}

连接数据库

 
  1. server:

  2. port: 8082

  3. spring:

  4. datasource:

  5. driver-class-name: com.mysql.cj.jdbc.Driver

  6. url: jdbc:mysql://localhost:3306/calculator?serverTimezone=GMT%2b8

  7. username: root

  8. password: password

6.心路历程和收获

通过这次作业,我充分了解到了前后端开发的基本模式。我使用html,css,js原生来开发前端,遇到了响应式变量不便于管理的问题,在接口对接上,直接使用了原生请求库,成功与后端对接。后端经过了解,选用了springboot作为开发框架,在orm层一开始使用jpa。遇到一些问题后切换到了更合适的mybatis框架成功解决问题。也第一次的部署了自己的mysql数据库。总的来说这次作业收获很大,是一次完整的前后端分离开发体验,也学习到了许多新知识,接下来我会去了解vue框架,希望能在之后的项目中做得更好

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值