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

SpringBoot+Vue+Element-ui實(shí)現(xiàn)前后端分離

 更新時間:2023年06月30日 10:11:17   作者:亞不帥  
使用前后端分離的方式,可以減少代碼耦合,本文主要介紹了SpringBoot+Vue+Element-ui實(shí)現(xiàn)前后端分離,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

大家好!今天來給大家分享一下Springboot+Vue實(shí)現(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í)體類,屬性名最好是跟數(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,然后掛載點(diǎn)...這些就都不說了,這里我發(fā)的是axios請求。代碼如下:

   <script>
             new Vue({
				el: "#big",//掛載點(diǎn)
				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實(shí)現(xiàn)前后端分離的文章就介紹到這了,更多相關(guān)SpringBoot Vue 前后端分離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實(shí)現(xiàn)的日期處理類完整實(shí)例

    Java實(shí)現(xiàn)的日期處理類完整實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)的日期處理類,結(jié)合完整實(shí)例形式分析了Java針對日期的獲取、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹

    SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹

    Spring MVC 是 Spring 提供的一個基于 MVC 設(shè)計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)
    2023-02-02
  • Web容器啟動過程中如何執(zhí)行Java類

    Web容器啟動過程中如何執(zhí)行Java類

    這篇文章主要介紹了Web容器啟動過程中如何執(zhí)行Java類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • 我從jdk1.8升級到j(luò)dk11所遇到的坑都有這些

    我從jdk1.8升級到j(luò)dk11所遇到的坑都有這些

    這篇文章主要介紹了從jdk1.8升級到j(luò)dk11將會遇到的一些坑,本文給大家分享解決方案對大家的學(xué)習(xí)或工作具有參考借鑒價值,對jdk1.8升級到j(luò)dk11相關(guān)知識感興趣的朋友,快來看看吧
    2021-08-08
  • Mybatis中的延遲加載詳細(xì)解析

    Mybatis中的延遲加載詳細(xì)解析

    這篇文章主要介紹了Mybatis中的延遲加載詳細(xì)解析,延遲加載就是在真正需要使用數(shù)據(jù)時才發(fā)起查詢,不用的時候不進(jìn)行查詢,按需加載(懶加載),需要的朋友可以參考下
    2023-12-12
  • java在cmd運(yùn)行"-d"和"-cp"參數(shù)解讀

    java在cmd運(yùn)行"-d"和"-cp"參數(shù)解讀

    這篇文章主要介紹了java在cmd運(yùn)行"-d"和"-cp"參數(shù)用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Springboot通過谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Springboot通過谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Kaptcha是谷歌開源的一款簡單實(shí)用的圖形驗(yàn)證碼組件。我個人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項目中,這篇文章主要介紹了Springboot通過谷歌Kaptcha組件生成圖形驗(yàn)證碼的方法,需要的朋友可以參考下
    2023-05-05
  • SpringBoot關(guān)于List集合的校驗(yàn)方式

    SpringBoot關(guān)于List集合的校驗(yàn)方式

    這篇文章主要介紹了SpringBoot關(guān)于List集合的校驗(yàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring Boot開發(fā)中加密數(shù)據(jù)的模糊搜索解決方案詳解

    Spring Boot開發(fā)中加密數(shù)據(jù)的模糊搜索解決方案詳解

    在SpringBoot中實(shí)現(xiàn)加密手機(jī)號的模糊搜索,需在數(shù)據(jù)安全、業(yè)務(wù)功能和系統(tǒng)性能之間進(jìn)行權(quán)衡,簡單解密后查詢方案僅適用于極小規(guī)模應(yīng)用,本文介紹Spring Boot開發(fā)中加密數(shù)據(jù)的模糊搜索解決方案,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • java中枚舉的詳細(xì)使用介紹

    java中枚舉的詳細(xì)使用介紹

    本篇文章介紹了,在java中枚舉的詳細(xì)使用。需要的朋友參考下
    2013-04-04

最新評論

竹北市| 安福县| 晋宁县| 东莞市| 湖南省| 钟山县| 扶风县| 鄢陵县| 炉霍县| 霍州市| 清丰县| 象山县| 枣阳市| 太谷县| 京山县| 犍为县| 台中市| 呼和浩特市| 阳高县| 宝兴县| 喀喇| 西青区| 石台县| 大悟县| 天门市| 普宁市| 罗田县| 罗山县| 禹城市| 水富县| 禄劝| 邵武市| 大关县| 潮安县| 峡江县| 扎鲁特旗| 凤台县| 西城区| 万荣县| 陇西县| 正镶白旗|