SpringBoot+Vue+Element-ui實現(xiàn)前后端分離
大家好!今天來給大家分享一下Springboot+Vue實現(xiàn)前后端分離!
一、Springboot
前后端分離很好理解,就是前端專門寫前端,后端專門寫后端,寫完之后前端調(diào)一下后端的接口即可。
我們先從簡單的做起,寫一個查詢,不應(yīng)傳參數(shù)的。首先我們要新建一個Springboot項目,配置好我們的pom.xml,注意我這里用的是mysql數(shù)據(jù)庫,以及整合了mybatis。所以需要配置mysql以及mybatis。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency>
完整的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot_mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_mybatis</name> <description>springboot_mybatis</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </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-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置完之后我們就可以寫代碼了,首先我們要先分層,在這里我省略了service層,正常的話還學(xué)要一個service層,那么其他的不多說了,configuration包是為了解決跨域問題的,我們下面會用到。
建完之后再來配置一下數(shù)據(jù)庫,在yaml文件里,配置好mysql數(shù)據(jù)的路徑以及登錄名跟密碼,不然連接不上數(shù)據(jù)庫。
接下來我們先在po包建好實體類,屬性名最好是跟數(shù)據(jù)庫的列名一致。
有了po我們就可以寫mapper層以及controller層,我們先來寫mapper層吧。在這里我們建的是一個接口類,然后需要注解聲明是mapper層,我在這里用的注解寫的sql ,大家也可以用其他方式。
@Mapper public interface UserMapper { @Select("select * from smbms_user") public List<User> list(); }
有了mapper層就到了我們的controller層。我們首先要寫controller的對外映射路徑,在這里我們要注意的是前端如果發(fā)的是ajax或者axios請求的話需要寫@RestController注解來聲明controller,如果用@Controller注解的話需要在每一個方法上面多加一個@ResponseBody注解,這樣就可以解決ajax或者axios請求。
@RequestMapping("/user") @RestController public class UserController { @Autowired private UserMapper mapper; @RequestMapping("/list") public List<User> test(Map map){ //System.out.println("連接成功!"); List<User> userList = mapper.list(); map.put("userList",userList); /*model.addAttribute(); model.addAttribute("userList",userList);*/ return userList; } }
Controller調(diào)mapper,之后再將查出來的數(shù)據(jù)存到Map或者M(jìn)odel中,再將數(shù)據(jù)返回到我們的前端頁面即可。到了這里很多人都覺得后端寫完了。然而不是,Springboot想要連接前端還需要解決一個跨域問題,不然請求是發(fā)送不到后端的。這里就用到了我們的configuration包,我們在里面寫一個配置文件。
package com.example.springboot_mybatis.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { @Bean public CorsFilter corsFilter(){ CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**",corsConfiguration); return new CorsFilter(source); } }
我們用@Configuration聲明這個類是一個配置文件,然后使用Bean注入,就解決掉了跨域問題,這樣前端的請求就可以正常的發(fā)送到了我們的后端。
二、Vue+Element-ui
寫前端還是老樣子,先引入Vue,然后掛載點...這些就都不說了,這里我發(fā)的是axios請求。代碼如下:
<script> new Vue({ el: "#big",//掛載點 data: { userList: [],//接收數(shù)據(jù)的數(shù)組 }, mounted:function(){ this.show()//頁面加載時執(zhí)行的方法 }, methods:{ show:function(){ axios.get("http://localhost:8080/user/list").then(resp=>{ this.userList = resp.data//接收數(shù)據(jù) }) } </script>
調(diào)完接口接收到數(shù)據(jù)之后就該展示數(shù)據(jù)了,代碼如下
<el-table :data="userList" > <el-table-column prop="id" label="序號"></el-table-column> <el-table-column prop="userCode" label="編碼"></el-table-column> <el-table-column prop="userName" label="姓名"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="danger" @click="del(scope.row.id)">刪除</el-button> <el-button type="warning" @click="updateinit(scope.row.id)">修改</el-button> </template> </el-table-column> </el-table>
然后我們同時啟動Springboot和前端 。
數(shù)據(jù)展示成功 。
總結(jié)
到此這篇關(guān)于SpringBoot+Vue+Element-ui實現(xiàn)前后端分離的文章就介紹到這了,更多相關(guān)SpringBoot Vue 前后端分離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue+springboot+element+vue-resource實現(xiàn)文件上傳教程
- SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能
- vue+springboot前后端分離實現(xiàn)單點登錄跨域問題解決方法
- 部署vue+Springboot前后端分離項目的步驟實現(xiàn)
- 解決前后端分離 vue+springboot 跨域 session+cookie失效問題
- vue+springboot前后端分離工程跨域問題解決方案解析
- 詳解springboot和vue前后端分離開發(fā)跨域登陸問題
- SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題
- SpringBoot+mybatis+Vue實現(xiàn)前后端分離項目的示例
相關(guān)文章
在Spring項目中引入高版本依賴并解決低版本沖突問題的解決方法
在Spring項目的開發(fā)過程中,依賴管理是一個非常重要且復(fù)雜的問題,我們可能需要引入更高版本的依賴來使用新特性或修復(fù)舊版本的Bug,然而,這些高版本依賴可能會與項目中已有的低版本依賴產(chǎn)生沖突,本文將詳細(xì)探討如何在Spring中引入高版本依賴,并解決低版本依賴沖突的問題2025-03-03Javaweb基礎(chǔ)入門HTML之table與form
HTML的全稱為超文本標(biāo)記語言,是一種標(biāo)記語言。它包括一系列標(biāo)簽.通過這些標(biāo)簽可以將網(wǎng)絡(luò)上的文檔格式統(tǒng)一,使分散的Internet資源連接為一個邏輯整體。HTML文本是由HTML命令組成的描述性文本,HTML命令可以說明文字,圖形、動畫、聲音、表格、鏈接等2022-03-03Springboot集成jsp及部署服務(wù)器實現(xiàn)原理
這篇文章主要介紹了Springboot集成jsp及部署服務(wù)器實現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08空指針HttpSession異常之SpringBoot集成WebSocket的方法
文章介紹了在Spring?Boot中集成WebSocket時遇到的HttpSession為空的問題,并探討了三種解決方法,方法一涉及域名配置,方法二通過監(jiān)聽創(chuàng)建Session,而方法三是從request中獲取session并存入數(shù)據(jù),感興趣的朋友一起看看吧2025-01-01IDEA關(guān)閉SpringBoot程序后仍然占用端口的排查與解決方法
在使用 IntelliJ IDEA 開發(fā) Spring Boot 應(yīng)用時,有時即使關(guān)閉了應(yīng)用,程序仍然占用端口,這會導(dǎo)致重新啟動應(yīng)用時出現(xiàn)端口被占用的錯誤,所以本文給大家介紹了IDEA關(guān)閉SpringBoot程序后仍然占用端口的排查與解決方法,需要的朋友可以參考下2025-02-02基于Java數(shù)組實現(xiàn)循環(huán)隊列的兩種方法小結(jié)
下面小編就為大家分享一篇基于Java數(shù)組實現(xiàn)循環(huán)隊列的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12java讀寫excel文件實現(xiàn)POI解析Excel的方法
在日常工作中,我們常常會進(jìn)行Excel文件讀寫操作,這篇文章主要介紹了java讀寫excel文件實現(xiàn)POI解析Excel的方法,實例分析了java讀寫excel的技巧,非常具有實用價值,需要的朋友可以參考下2018-10-10java學(xué)習(xí)筆記之eclipse+tomcat 配置
俗話說:工欲善其事必先利其器,既然要學(xué)習(xí)java,首先把java的開發(fā)環(huán)境搗鼓一下吧,這里我們來談?wù)別clipse+tomcat的配置方法。2014-11-11