关键词搜索

源码搜索 ×
×

Java&前端笔记-后端Springboot,前端vue,Nginx使前后端分离

发布2019-07-29浏览5363次

详情内容

目录

 

基本概念

代码实例


 

基本概念

这是一个很6的模式,以目前本人C++ Qt的技术,是做不出的,但Java已经有雏形了。

后端采用Spring Boot主要是回json数据,如下所示:

这里的数据都是来自数据库:

前端通过点击按钮向后端发送请求,从而获取数据:

相关的Nginx代理如下:

这里Vue application是192.168.164.1:8081

而Spring Boot为192.168.164.1:8080

在后期,这Spring Boot可以放到内网里面,这样外网就不能直接访问他了,

这里前端和后端是2个服务,但通过Nginx,这种方式,用户一般是体会不到的。

这里是vue中的请求代码:

  1. methods: {
  2. handleSubmit(){
  3. this.$axios({
  4. type: 'get',
  5. url: '/gra/list'
  6. }).then(res => {
  7. console.log(res)
  8. }).catch(err=>{
  9. alert(err)
  10. })
  11. }
  12. }

 

 

代码实例

因为目前还是半成品,就不发项目了,等实例完成,将会在其他的博文中给去全部的链接下载

Spring Boot结构

源码如下:

BarController.java

  1. package com.gratestdemo.demo.controller;
  2. import com.gratestdemo.demo.dataobject.BarInfo;
  3. import com.gratestdemo.demo.service.BarInfoService;
  4. import com.gratestdemo.demo.vo.BarInfoVO;
  5. import com.gratestdemo.demo.vo.BarVO;
  6. import com.gratestdemo.demo.vo.ResultVO;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. @RequestMapping("/")
  14. @RestController
  15. public class BarController {
  16. @Autowired
  17. private BarInfoService barInfoService;
  18. @GetMapping("/list")
  19. public ResultVO list(){
  20. List<BarInfo> barInfoList = barInfoService.findAll();
  21. List<BarInfoVO> barInfoVOList = new ArrayList<>();
  22. BarVO barVO = new BarVO();
  23. barVO.setName("柱状图");
  24. for(BarInfo barInfo : barInfoList){
  25. BarInfoVO barInfoVO = new BarInfoVO();
  26. barInfoVO.setCount(barInfo.getCount());
  27. barInfoVO.setValue1(barInfo.getValue1());
  28. barInfoVO.setValue2(barInfo.getValue2());
  29. barInfoVO.setValue3(barInfo.getValue3());
  30. barInfoVO.setValue4(barInfo.getValue4());
  31. barInfoVOList.add(barInfoVO);
  32. }
  33. barVO.setBarInfoVOList(barInfoVOList);
  34. ResultVO resultVO = new ResultVO();
  35. resultVO.setCode(0);
  36. resultVO.setMsg("成功");
  37. resultVO.setData(barVO);
  38. return resultVO;
  39. }
  40. }

BarInfo.java

  1. package com.gratestdemo.demo.dataobject;
  2. import lombok.Data;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. @Data
  8. @Entity
  9. @Table(name = "gra_bar")
  10. public class BarInfo {
  11. @Id
  12. private Integer id;
  13. private Integer count;
  14. private float value1;
  15. private float value2;
  16. private float value3;
  17. private float value4;
  18. }

BarInfoRepository.java

  1. package com.gratestdemo.demo.repository;
  2. import com.gratestdemo.demo.dataobject.BarInfo;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. public interface BarInfoRepository extends JpaRepository<BarInfo, Integer> {
  5. }

BarInfoServiceImpl

  1. package com.gratestdemo.demo.service.impl;
  2. import com.gratestdemo.demo.dataobject.BarInfo;
  3. import com.gratestdemo.demo.repository.BarInfoRepository;
  4. import com.gratestdemo.demo.service.BarInfoService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. @Service
  9. public class BarInfoServiceImpl implements BarInfoService {
  10. @Autowired
  11. private BarInfoRepository repository;
  12. @Override
  13. public BarInfo findOne(Integer id) {
  14. return repository.findOne(id);
  15. }
  16. @Override
  17. public List<BarInfo> findAll() {
  18. return repository.findAll();
  19. }
  20. }

BarInfoService.java

  1. package com.gratestdemo.demo.service;
  2. import com.gratestdemo.demo.dataobject.BarInfo;
  3. import java.util.List;
  4. public interface BarInfoService {
  5. BarInfo findOne(Integer id);
  6. List<BarInfo> findAll();
  7. }

BarInfoVO.java

  1. package com.gratestdemo.demo.vo;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import lombok.Data;
  4. @Data
  5. public class BarInfoVO {
  6. @JsonProperty("count")
  7. private Integer count;
  8. @JsonProperty("value1")
  9. private float value1;
  10. @JsonProperty("value2")
  11. private float value2;
  12. @JsonProperty("value3")
  13. private float value3;
  14. @JsonProperty("value4")
  15. private float value4;
  16. }

BarVO.java

  1. package com.gratestdemo.demo.vo;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import lombok.Data;
  4. import java.util.List;
  5. @Data
  6. public class BarVO {
  7. @JsonProperty("name")
  8. private String name;
  9. @JsonProperty("content")
  10. private List<BarInfoVO> barInfoVOList;
  11. }

Result.java

  1. package com.gratestdemo.demo.vo;
  2. import lombok.Data;
  3. @Data
  4. public class ResultVO<T> {
  5. private Integer code;
  6. private String msg;
  7. private T data;
  8. }

Vue前端关键代码:

main.js

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import Axios from 'axios'
  5. import TodoList from './TodoList'
  6. Vue.config.productionTip = false
  7. Vue.prototype.$axios=Axios
  8. /* eslint-disable no-new */
  9. new Vue({
  10. el: '#app',
  11. components: { TodoList },
  12. template: '<TodoList/>',
  13. })

TodoList.vue

  1. <template>
  2. <div>
  3. <div>
  4. <button @click="handleSubmit">获取</button>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. methods: {
  11. handleSubmit(){
  12. this.$axios({
  13. type: 'get',
  14. url: '/gra/list'
  15. }).then(res => {
  16. console.log(res)
  17. }).catch(err=>{
  18. alert(err)
  19. })
  20. }
  21. }
  22. }
  23. </script>
  24. <style>
  25. </style>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载