最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

手把手教你如何搭建SpringBoot+Vue前后端分離

 更新時(shí)間:2023年03月30日 16:24:13   作者:海風(fēng)極客  
這篇文章主要介紹了手把手教你如何搭建SpringBoot+Vue前后端分離,前后端分離是目前開(kāi)發(fā)中常用的開(kāi)發(fā)模式,達(dá)成充分解耦,需要的朋友可以參考下

1 什么是前后端分離

前后端分離是目前互聯(lián)網(wǎng)開(kāi)發(fā)中比較廣泛使用的開(kāi)發(fā)模式,主要是將前端和后端的項(xiàng)目業(yè)務(wù)進(jìn)行分離,可以做到更好的解耦合,前后端之間的交互通過(guò)xml或json的方式,前端主要做用戶界面的渲染,后端主要負(fù)責(zé)業(yè)務(wù)邏輯和數(shù)據(jù)的處理。

在這里插入圖片描述

2 Spring Boot后端搭建

2.1 Mapper層

請(qǐng)參閱這篇文章 手把手教你SpringBoot整合Mybatis

此次項(xiàng)目的后端搭建就是在這個(gè)項(xiàng)目的基礎(chǔ)上

2.2 Service層

接口:

/**
 * @author 17122
 */
public interface StudentService {
    /**
     * 添加一個(gè)學(xué)生
     *
     * @param student
     * @return
     */
    public int saveStudent(Student student);

    /**
     * 根據(jù)ID查看一名學(xué)生
     *
     * @param id
     * @return
     */
    public Student findStudentById(Integer id);

    /**
     * 查詢?nèi)繉W(xué)生
     *
     * @return
     */
    public List<Student> findAllStudent();

    /**
     * 根據(jù)ID刪除一個(gè)
     *
     * @param id
     * @return
     */
    public int removeStudentById(Integer id);

    /**
     * 根據(jù)ID修改
     *
     * @param student
     * @return
     */
    public int updateStudentById(Student student);


}

實(shí)現(xiàn)類:

/**
 * @author 17122
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private XmlStudentMapper xmlStudentMapper;

    @Override
    public int saveStudent(Student student) {
        return xmlStudentMapper.saveStudent(student);
    }

    @Override
    public Student findStudentById(Integer id) {
        return xmlStudentMapper.findStudentById(id);
    }

    @Override
    public List<Student> findAllStudent() {
        return xmlStudentMapper.findAllStudent();
    }

    @Override
    public int removeStudentById(Integer id) {
        return xmlStudentMapper.removeStudentById(id);
    }

    @Override
    public int updateStudentById(Student student) {
        return xmlStudentMapper.updateStudentById(student);
    }
}

2.3 Controller層

/**
 * @author 17122
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加學(xué)生
     *
     * @param student
     * @return
     */
    @PostMapping("/save")
    public int saveStudent(@RequestBody Student student) {
        int result;
        try {
            result = studentService.saveStudent(student);
        } catch (Exception exception) {
            return -1;
        }
        return result;
    }

    /**
     * 查看全部
     *
     * @return
     */
    @GetMapping("/findAll")
    public List<Student> findAll() {
        return studentService.findAllStudent();
    }

    /**
     * 根據(jù)ID查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") Integer id) {
        return studentService.findStudentById(id);
    }

    /**
     * 刪除一個(gè)
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public int remove(@PathVariable("id") Integer id) {
        return studentService.removeStudentById(id);
    }

    /**
     * 修改學(xué)生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/update")
    public int update(@RequestBody Student student) {
        return studentService.updateStudentById(student);
    }

}

2.4 配置類

解決跨域請(qǐng)求

/**
 * @author 17122
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

圖解跨域問(wèn)題:

在這里插入圖片描述

3 Vue前端搭建

3.1 新建Vue_cli2.x項(xiàng)目

3.2 引入路由

npm install vue-router --save

3.3 新建文件

在這里插入圖片描述

3.4 配置和測(cè)試路由

main.js配置

import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false

new Vue({
    render: h => h(App),
    router
}).$mount('#app')

index.js

//注冊(cè)路由
import Vue from 'vue';
import VueRouter from 'vue-router';
//引入路由
import index from '../view/index'
import update from "../view/update";
import selectAll from "../view/selectAll";
import selectOne from "../view/selectOne";
import insert from "../view/insert";

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [
        {
            name: "主頁(yè)重定向",
            path: "/",
            redirect: "/index"
        }, {
            name: "主頁(yè)",
            path: "/index",
            component: index,
            children: [
                {
                    name: "修改操作",
                    path: "/update",
                    component: update,
                }, {
                    name: "查看全部",
                    path: "/selectAll",
                    component: selectAll,
                }, {
                    name: "查看一個(gè)",
                    path: "/selectOne",
                    component: selectOne,
                }, {
                    name: "添加一個(gè)",
                    path: "/insert",
                    component: insert,
                }
            ]
        }
    ]
})

export default router

App.vue

<template>
    <div id="app">
        <router-view/>
    </div>
</template>

<script>

export default {
    name: 'App',
}
</script>

index.vue

<template>
    <div>
        <router-link to="update">update</router-link>
        <br>
        <router-link to="selectAll"> selectAll</router-link>
        <br>
        <router-link to="selectOne"> selectOne</router-link>
        <br>
        <router-link to="insert"> insert</router-link>
        <br>
        <br>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    name: "index"
}
</script>

<style scoped>

</style>

insert.vue

<template>
    <div>
        insert
    </div>
</template>

<script>
export default {
    name: "insert"
}
</script>

<style scoped>

</style>

selectOne.vue

<template>
    <div>
        selectOne
    </div>
</template>

<script>
export default {
    name: "selectOne"
}
</script>

<style scoped>

</style>

selectAll.vue

<template>
    <div>
        selectAll
    </div>
</template>

<script>
export default {
    name: "selectAll"
}
</script>

<style scoped>

</style>

update.vue

<template>
    <div>
        update
    </div>
</template>

<script>
export default {
    name: "update"
}
</script>

<style scoped>

</style>

測(cè)試

啟動(dòng)項(xiàng)目

npm run serve

訪問(wèn):http://localhost:8080/

在這里插入圖片描述

點(diǎn)擊相關(guān)標(biāo)簽時(shí)會(huì)顯示響應(yīng)頁(yè)面

3.5 引入Element UI

npm i element-ui -S

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.config.productionTip = false
Vue.use(ElementUI)
new Vue({
    render: h => h(App),
    router
}).$mount('#app')

3.6 使用Element UI美化頁(yè)面

index.vue

<template>
    <div>
        <el-menu class="el-menu-demo" mode="horizontal" :router="true">
            <el-menu-item index="/selectAll">全部學(xué)生</el-menu-item>
            <el-menu-item index="/insert">添加學(xué)生</el-menu-item>
            <el-menu-item index="/selectOne">查看學(xué)生</el-menu-item>
            <el-menu-item index="/update">修改學(xué)生</el-menu-item>
        </el-menu>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    name: "index"
}
</script>

<style scoped>

</style>

insert.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon  label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;">
            <el-form-item label="姓名" prop="pass">
                <el-input type="text" v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="年齡" prop="checkPass">
                <el-input type="text" v-model="ruleForm.age" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "insert",
    data() {
        return {
            ruleForm: {
                name: '',
                age: ''
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    }
}
</script>

<style scoped>

</style>

selectOne.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm"
                 style="margin-top:30px;width: 30%;">
            <el-form-item label="ID" prop="pass">
                <el-input type="text" v-model="ruleForm.id"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="pass">
                <el-input type="text" v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年齡" prop="checkPass">
                <el-input type="text" v-model="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "selectOne",
    data() {
        return {
            ruleForm: {
                id: '',
                name: '',
                age: ''
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>

</style>

selectAll.vue

<template>
    <div>
        <template>
            <el-table
                  :data="tableData"
                  style="width: 60%;margin-top:30px;">
                <el-table-column
                      prop="id"
                      label="ID"
                      width="180">
                </el-table-column>
                <el-table-column
                      prop="name"
                      label="姓名"
                      width="180">
                </el-table-column>
                <el-table-column
                      prop="age"
                      label="年齡">
                </el-table-column>
                <el-table-column
                      label="操作">
                    <template>
                        <el-button type="warning" size="small">修改</el-button>
                        <el-button type="danger" size="small">刪除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
    </div>
</template>

<script>
export default {
    name: "selectAll",
    data() {
        return {
            tableData: []
        }
    }
}
</script>

<style scoped>

</style>

update.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm"
                 style="margin-top:30px;width: 30%;">
            <el-form-item label="ID" prop="pass">
                <el-input type="text" v-model="ruleForm.id"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="checkPass">
                <el-input type="text" v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年齡" prop="age">
                <el-input type="text" v-model="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="warning" @click="submitForm('ruleForm')">修改</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "update",
    data() {
        return {
            ruleForm: {
                id: '',
                name: '',
                age: ''
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>

</style>

效果

在這里插入圖片描述

在這里插入圖片描述

3.7 整合axios與Spring Boot后端交互

npm install axios --save

insert.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm"
                 style="margin-top:30px;width: 30%;">
            <el-form-item label="姓名" prop="pass">
                <el-input type="text" v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年齡" prop="checkPass">
                <el-input type="text" v-model="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm()">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from 'axios'

export default {
    name: "insert",
    data() {
        return {
            ruleForm: {
                name: '',
                age: ''
            }
        };
    },
    methods: {
        submitForm() {
            axios.post("http://localhost:8081/student/save", this.ruleForm).then(function (resp) {
                console.log(resp)
            })
        },
    }
}
</script>

<style scoped>

</style>

selectOne.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm"
                 style="margin-top:30px;width: 30%;">
            <el-form-item label="ID" prop="pass">
                <el-input type="text" v-model="ruleForm.id"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="pass">
                <el-input type="text" v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年齡" prop="checkPass">
                <el-input type="text" v-model="ruleForm.age"></el-input>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from "axios";

export default {
    name: "selectOne",
    data() {
        return {
            ruleForm: {
                id: '',
                name: '',
                age: ''
            }
        };
    },
    methods: {
        getStudent() {
            const _this = this;
            axios.get("http://localhost:8081/student/findById/" + this.$route.query.id).then(function (resp) {
                _this.ruleForm = resp.data;
            })
        }
    },
    created() {
        this.getStudent();
    }
}
</script>

<style scoped>

</style>

selectAll.vue

<template>
    <div>
        <template>
            <el-table
                  :data="tableData"
                  style="width: 60%;margin-top:30px;">
                <el-table-column
                      prop="id"
                      label="ID"
                      width="180">
                </el-table-column>
                <el-table-column
                      prop="name"
                      label="姓名"
                      width="180">
                </el-table-column>
                <el-table-column
                      prop="age"
                      label="年齡">
                </el-table-column>
                <el-table-column
                      label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="small" @click="select(scope.row)">查看</el-button>
                        <el-button type="warning" size="small" @click="update(scope.row)">修改</el-button>
                        <el-button type="danger" size="small" @click="remove(scope.row)">刪除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
    </div>
</template>

<script>
import axios from "axios";

export default {
    name: "selectAll",
    data() {
        return {
            tableData: []
        }
    },
    methods: {
        getData() {
            const _this = this;
            axios.get("http://localhost:8081/student/findAll").then(function (resp) {
                _this.tableData = resp.data;
            })
        },
        remove(stu) {
            const _this = this;
            if (confirm("確定刪除嗎?")) {
                axios.delete("http://localhost:8081/student/remove/" + stu.id).then(function (resp) {
                    if (resp.data == 1) {
                        _this.getData();
                    }
                })
            }
        },
        select(stu) {
            this.$router.push({
                path: "/selectOne",
                query:{
                    id: stu.id
                }
            })
        },
        update(stu) {
            this.$router.push({
                path: "/update",
                query:{
                    id: stu.id
                }
            })
        }
    },
    created() {
        this.getData();
    }
}
</script>

<style scoped>

</style>

update.vue

<template>
    <div>
        <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm"
                 style="margin-top:30px;width: 30%;">
            <el-form-item label="ID">
                <el-input type="text" v-model="ruleForm.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="姓名">
                <el-input type="text" v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年齡">
                <el-input type="text" v-model="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="warning" @click="submitForm()">修改</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from "axios";

export default {
    name: "update",
    data() {
        return {
            ruleForm: {
                id: '',
                name: '',
                age: ''
            }
        };
    },
    methods: {
        submitForm() {
            axios.post("http://localhost:8081/student/update", this.ruleForm).then(function (resp) {
                console.log(resp)
            })
        },
        getStudent() {
            const _this = this;
            axios.get("http://localhost:8081/student/findById/" + this.$route.query.id).then(function (resp) {
                _this.ruleForm = resp.data;
            })
        }
    },
    created() {
        this.getStudent();
    }
}
</script>

<style scoped>

</style>

4 總結(jié)

在這里插入圖片描述

到此這篇關(guān)于手把手教你如何搭建SpringBoot+Vue前后端分離的文章就介紹到這了,更多相關(guān)SpringBoot+Vue前后端分離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解

    Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解

    這篇文章主要介紹了Java SpringBoot如何實(shí)現(xiàn)帶界面的代碼生成器,幫助大家更好的理解和使用Java SpringBoot編程語(yǔ)言,感興趣的朋友可以了解下
    2021-09-09
  • Mybatis批量更新三種方式的實(shí)現(xiàn)

    Mybatis批量更新三種方式的實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis批量更新三種方式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Spring Boot 簡(jiǎn)介(入門(mén)篇)

    Spring Boot 簡(jiǎn)介(入門(mén)篇)

    Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。下面通過(guò)本文給大家介紹spring boot相關(guān)知識(shí),需要的的朋友參考下吧
    2017-04-04
  • Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置

    Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置

    這篇文章主要介紹了Spring boot如何通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及多線程配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • idea導(dǎo)入jar包的詳細(xì)圖文教程

    idea導(dǎo)入jar包的詳細(xì)圖文教程

    這篇文章主要給大家介紹了關(guān)于idea導(dǎo)入jar包的詳細(xì)圖文教程,文中通過(guò)圖文將導(dǎo)入的步驟介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Spring依賴注入多種類型數(shù)據(jù)的示例代碼

    Spring依賴注入多種類型數(shù)據(jù)的示例代碼

    這篇文章主要介紹了Spring依賴注入多種類型數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心流程分析

    SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心流程分析

    OAuth?2.0?主要用于在互聯(lián)網(wǎng)上安全地委托授權(quán),廣泛應(yīng)用于身份驗(yàn)證和授權(quán)場(chǎng)景,這篇文章介紹SpringSecurity+OAuth2.0?搭建認(rèn)證中心和資源服務(wù)中心,感興趣的朋友一起看看吧
    2024-01-01
  • javaWeb如何實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼詳解

    javaWeb如何實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼詳解

    這篇文章主要給大家介紹了關(guān)于javaWeb如何實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 常用數(shù)字簽名算法RSA與DSA的Java程序內(nèi)實(shí)現(xiàn)示例

    常用數(shù)字簽名算法RSA與DSA的Java程序內(nèi)實(shí)現(xiàn)示例

    這篇文章主要介紹了常用數(shù)字簽名算法RSA與DSA的Java程序內(nèi)實(shí)現(xiàn)示例,一般來(lái)說(shuō)DSA算法用于簽名的效率會(huì)比RSA要快,需要的朋友可以參考下
    2016-04-04
  • springmvc @RequestBody String類型參數(shù)的使用

    springmvc @RequestBody String類型參數(shù)的使用

    這篇文章主要介紹了springmvc @RequestBody String類型參數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論

萍乡市| 六安市| 云阳县| 普兰县| 波密县| 江北区| 河津市| 昭平县| 个旧市| 安庆市| 禹城市| 临清市| 馆陶县| 离岛区| 法库县| 泰安市| 张家界市| 革吉县| 阳东县| 阿图什市| 三穗县| 九江市| 千阳县| 合山市| 正镶白旗| 肥西县| 乐昌市| 合川市| 锦屏县| 略阳县| 定南县| 咸阳市| 隆德县| 北流市| 镇沅| 德格县| 巴林左旗| 离岛区| 凤山市| 布拖县| 岳普湖县|