欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+Vue項目新手快速入門指南

 更新時間:2022年06月13日 14:53:48   作者:smilecb  
最近剛剛做了一個基于vue+springboot的系統(tǒng),于是基于這點,對遇到的一些問題進行一些配置的匯總,下面這篇文章主要給大家介紹了關于SpringBoot+Vue項目新手快速入門的相關資料,需要的朋友可以參考下

前言:本人目前從事java開發(fā),但同時也在學習各種前端技術,下面是我做的一個前后端分離項目的一個小案例,不足之處請多多指教

1. 項目技術選型

Springboot+MyBatis-Plus+vue+element-ui+echarts

2.數(shù)據(jù)庫設計

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `fruit`;
CREATE TABLE `fruit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '名稱',
  `sale` double DEFAULT NULL COMMENT '價格',
  `num` int(11) DEFAULT NULL COMMENT '庫存',
  `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時間',
  `update_time` datetime DEFAULT NULL COMMENT '更新時間',
  `del_flag` tinyint(4) DEFAULT '0' COMMENT '刪除標記',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

3. 后臺搭建

3.1 引入依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>velocity</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
       </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        </dependencies>

3.2 swagger配置

@Configuration //什么此類為配置類
@EnableSwagger2 //開啟swagger2
public class SwaggerConfig {
}

3.3實體類

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@ApiModel
public class Fruit {
    @ApiModelProperty("id")
    private int id;


    @ApiModelProperty("name")
    private String name;

    @ApiModelProperty("sale")
    private double sale;

    @ApiModelProperty("num")
    private int num;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @TableLogic
    private boolean delFlag;
}

3.4 自動填充配置

@Component
public class DateHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    //使用mp實現(xiàn)更新操作,執(zhí)行此方法
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}

3.5 Mapper

@Repository
public interface FruitMapper extends BaseMapper<Fruit> {
}

3.6 service

public interface FruitService extends IService<Fruit> {
    public FruitVO FruitVOList();
}

實現(xiàn)類

@Service
public class FruitServiceImpl extends ServiceImpl<FruitMapper, Fruit> implements FruitService {
    @Autowired
    private FruitMapper fruitMapper;

    @Override
    public FruitVO FruitVOList() {
        List<Fruit> fruits = fruitMapper.selectList(null);
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Integer> num = new ArrayList<>();
        for(Fruit fruit:fruits){
            name.add(fruit.getName());
            num.add(fruit.getNum());
        }
        FruitVO fruitVO = new FruitVO();
        fruitVO.setName(name);
        fruitVO.setNum(num);
        return fruitVO;
    }
}

3.7 controller

@RequestMapping("/fruit")
@RestController
@Api(tags = "水果")
@CrossOrigin
public class FruitController {

    @Autowired
    private FruitService fruitService;

    @GetMapping("/list")
    @ApiOperation("獲取水果列表")
    public ResponseData list(){
        List<Fruit> list = fruitService.list();
        return new ResponseData(200,list,"ok");
    }

    @GetMapping("/list1")
    @ApiOperation(("獲取水果列表1"))
    public List<Fruit> list1(){
        List<Fruit> list = fruitService.list();
        return list;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(("通過id刪除水果信息"))
    public ResponseData deleteFruit(@PathVariable("id") int id){
//        int id=1;
//        System.out.println(name);
            System.out.println(id);
        boolean b = fruitService.removeById(id);
        return new ResponseData().ok(b,"ok");
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("通過id獲取水果信息")
    public ResponseData getById(@PathVariable("id") int id){
        Fruit fruit = fruitService.getById(id);
        return new ResponseData().ok(fruit,"查找成功");
    }

    @PutMapping("/update")
    @ApiOperation("添加水果信息")
    public ResponseData update(@RequestBody Fruit fruit){
        boolean b = fruitService.updateById(fruit);
        if(b == true){
            return new ResponseData().ok(fruit,"更新成功");
        }else {
            return new ResponseData(404,fruit,"更新失敗");
        }
    }

    @PostMapping("/save")
    @ApiOperation("保存水果信息")
    public ResponseData save(@RequestBody Fruit fruit){
        boolean save = fruitService.save(fruit);
        if(save == true){
            return new ResponseData().ok(fruit,"保存成功");
        }else {
            return new ResponseData().error(fruit,"保存失敗");
        }
    }

    @GetMapping("barVo")
    @ApiOperation("獲取統(tǒng)計信息")
    public ResponseData barVo(){
        FruitVO fruitVO = fruitService.FruitVOList();
        return new ResponseData().ok(fruitVO,"查找成功");
    }
}

4. 前端搭建

4.1 環(huán)境搭建

4.1.1 Node環(huán)境

官方下載node

檢查安裝情況

node –v
npm –v

安裝cnpm

npm install –g cnpm --registry=https://registry.npm.taobao.org

安裝vue-cli

cnpm install vue-cli -g

4.1.2 項目構建

vue init webpack 項目名稱

創(chuàng)建成功后,進入項目根目錄,初始化項目并運行

cnpm install
cnpm run dev

4.1.3 安裝插件

安裝element-ui插件

cnpm install element-ui

安裝axios插件

cnpm install axios

安裝echarts插件

cnpm install echarts -S

4.1.4 引入插件

在main.js中引入插件

import Vue from 'vue'
import App from './App'
import router from './router'
import echarts from 'echarts'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
Vue.prototype.$axios = axios
Vue.use(ElementUI)
Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

4,2.搭建路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import About from '@/views/About'
import Pie from '@/views/Pie'
import Table from '@/views/Table'
import Edit from '@/views/Edit'
import Add from '@/views/Add'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/about',
      name: 'About',
      component: About
    },
    {
      path: '/pie',
      name: 'Pie',
      component: Pie
    },
    {
      path: '/table',
      name: 'Table',
      component:Table
    },
    {
      path: '/edit',
      name: 'Edit',
      component:Edit
    },
    {
      path: '/add',
      name: 'Add',
      component:Add
    }
  ]
})

4.3. echarts使用

在pages下創(chuàng)建一個名為pie.vue的文件

<template>
  <div>
    <h2>vue中插入Echarts示例</h2>
    <div id="chart_example" :style="{width:'800px',height:'600px'}">
    </div>
  </div>
</template>
 
<script>
const axios = require('axios'); 
export default {
    data() {

    },
    mounted(){
        let _this = this;
        axios.get('http://localhost:9001/fruit/barVo').then(function(response) {
            console.log(response.data.data)

        let myCharts = _this.$echarts.init(document.getElementById('chart_example'))
        myCharts.setOption( {
        title:{
            text:'數(shù)量統(tǒng)計表',
            top:20
        },
        xAxis: {
            type: 'category',
            data: response.data.data.name
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
            data: response.data.data.num,
            type: 'bar',
            name:'銷量',
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
            }
            }
        ]
        })
        })
    }
}
</script>

4.4 element-ui使用

表格的使用

在views下面創(chuàng)建table.vue

<template>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      fixed
      prop="id"
      label="id"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="名稱"
      width="120">
    </el-table-column>
    <el-table-column
      prop="num"
      label="數(shù)量"
      width="120">
    </el-table-column>
    <el-table-column
      prop="sale"
      label="價格"
      width="120">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="fruitDel(scope.row)" type="text" size="small">刪除</el-button>
        <el-button @click="getFruitById(scope.row)" type="text" size="small">編輯</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
const axios = require('axios');
  export default {
    methods: {
      handleClick(row) {
        console.log(row);
      },
      fruitDel(row){
        alert(row.id);
        axios.delete("http://localhost:9001/fruit/delete/"+row.id)
        location.reload();
      },
      getFruitById(object){
       this.$router.push('/edit?id='+object.id)
      }
    },
    created(){
        let _this=this;
        axios.get("http://localhost:9001/fruit/list")
        .then(response => {
            console.log(response);
            console.log(response.data.data)
            _this.tableData=response.data.data
        })
    },
    data() {
     return{
       tableData:null
     }
    }
  }
</script>

表單的使用

在views下面常見文件Add.vue

<template>
<el-form ref="form" :model="fruit" label-width="80px">
   <el-form-item label="水果名稱">
    <el-input v-model="fruit.name"></el-input>
  </el-form-item>
   <el-form-item label="水果數(shù)量">
    <el-input v-model="fruit.num"></el-input>
  </el-form-item> 
  <el-form-item label="水果售價">
    <el-input v-model="fruit.sale"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="onSubmit('fruit')">立即創(chuàng)建</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>    
</template>
<script>
const axios = require('axios');
  export default {
    data() {
      return {
        fruit: {
          id:'',
          name:'',
          num:'',
          sale: ''
        }
      }
    },
    methods: {
        onSubmit(){
           let _this = this;
           axios.post('http://localhost:9001/fruit/save',this.fruit)
           .then(function (response) {
             if(response.data==200) {
                this.$message({
                  message: '保存水果成功',
                  type: 'success'
                });
             }
            _this.$router.push('/table') 
           })
        }
    }
  }
</script>

在views項目常見edit.vue

<template>
<el-form ref="form" :model="fruit" label-width="80px">
  <el-form-item label="水果ID">
    <el-input v-model="fruit.id"></el-input>
  </el-form-item>
   <el-form-item label="水果名稱">
    <el-input v-model="fruit.name"></el-input>
  </el-form-item>
   <el-form-item label="水果數(shù)量">
    <el-input v-model="fruit.num"></el-input>
  </el-form-item> 
  <el-form-item label="水售價">
    <el-input v-model="fruit.sale"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="onSubmit('fruit')">編輯</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>    
</template>
<script>
const axios = require('axios');
  export default {
    data() {
      return {
        fruit: {
          id:'',
          name:'',
          num:'',
          sale: ''
        }
      }
    },
    created() {
        let id= this.$route.query.id
        let _this=this
        axios.get('http://localhost:9001/fruit/getById/'+id)
        .then(response=>{
            console.log(response)
            _this.fruit=response.data.data
        })
    },
    methods: {
        onSubmit(){
            alert(1)
                   let _this = this
                   axios.put("http://localhost:9001/fruit/update",this.fruit)
                   .then(function (response) {
                       console.log(response)
                       if(response.data.code==200){
                           _this.$alert(_this.fruit.name+'修改成功',"修改數(shù)據(jù)",{
                               confirmButtonText:'確定',
                               callback:action=>{
                                   _this.$router.push('/table')
                               }
                           })
                       }
                   })
        }
    }
  }
</script>

總結

到此這篇關于SpringBoot+Vue項目新手快速入門指南的文章就介紹到這了,更多相關SpringBoot+Vue項目入門內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java原生操作JDBC連接以及原理詳解

    Java原生操作JDBC連接以及原理詳解

    這篇文章主要給大家介紹了關于Java原生操作JDBC連接以及原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 關于@Component和@Bean使用注意

    關于@Component和@Bean使用注意

    這篇文章主要介紹了關于@Component和@Bean使用注意,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • grails不能運行fork模式解決方法

    grails不能運行fork模式解決方法

    這篇文章主要介紹了如何解決grails2.3.2中不能運行fork模式的異常,大家參考使用吧
    2013-11-11
  • Spring?Data?Exists查詢最佳方法編寫示例

    Spring?Data?Exists查詢最佳方法編寫示例

    這篇文章主要為大家介紹了Spring?Data?Exists查詢最佳方法編寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • java使用反射給對象屬性賦值的兩種方法

    java使用反射給對象屬性賦值的兩種方法

    JAVA反射機制是在運行狀態(tài)中,對于任意一個類,都能夠知道這個類的所有屬性和方法,下面這篇文章主要給大家介紹了關于java使用反射給對象屬性賦值的兩種方法,需要的朋友可以參考下
    2023-04-04
  • MyBatis如何使用selectKey返回主鍵的值

    MyBatis如何使用selectKey返回主鍵的值

    這篇文章主要介紹了MyBatis如何使用selectKey返回主鍵的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • JavaWeb踩坑記錄之項目訪問不到html文件

    JavaWeb踩坑記錄之項目訪問不到html文件

    這篇文章主要給大家介紹了關于JavaWeb踩坑記錄之項目訪問不到html文件的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用JavaWeb具有一定的參考學習價值,需要的朋友可以參考下
    2022-03-03
  • SpringMVC 單文件上傳與多文件上傳實例

    SpringMVC 單文件上傳與多文件上傳實例

    這篇文章主要介紹了SpringMVC 單文件上傳與多文件上傳實例的相關資料,需要的朋友可以參考下
    2017-06-06
  • java生成pdf表格,調(diào)用itext創(chuàng)建的實例

    java生成pdf表格,調(diào)用itext創(chuàng)建的實例

    這篇文章主要介紹了java生成pdf表格,調(diào)用itext創(chuàng)建的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • JAVASE系統(tǒng)實現(xiàn)抽卡功能

    JAVASE系統(tǒng)實現(xiàn)抽卡功能

    這篇文章主要為大家詳細介紹了JAVASE系統(tǒng)實現(xiàn)抽卡功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11

最新評論