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

Spring mvc是如何實現(xiàn)與數(shù)據(jù)庫的前后端的連接操作的?

 更新時間:2021年06月30日 14:21:15   作者:黃金龍666  
今天給大家?guī)淼氖顷P于Spring mvc的相關知識,文章圍繞著Spring mvc是如何實現(xiàn)與數(shù)據(jù)庫的前后端的連接操作的展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下

Spring mvc與數(shù)據(jù)庫的前后端的連接

springboot是基于maven的基礎上管理jar包的,只不過是使用springboot下載jar包只需選中即可,就會自動的在pom.xml文件中配置組件

在pom文件中的jar包的快捷鍵:右鍵--->generate---->depency---->搜索jar包

如果在前后端傳參數(shù)是輸入了參數(shù)卻返回null , 則說明屬性的名字(id,name等)寫錯了

 啟動類:注意 ,啟動類必須在啟動類中進行執(zhí)行.必能在idea的上面進行啟動,否則會啟動其他的啟動類導致報錯

package cn.tedu;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//啟動類
@SpringBootApplication
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class);
    }
}

創(chuàng)建car類(相當于model層)

注意:這里使用的是構造方法 主要的作用是方便new

package cn.tedu.pojo;
//Model用來封裝數(shù)據(jù)
public class Car {
    private int id;
    private String name;
    private double price;
    //Constructor構造方法,用來方便的new
    public Car(){}
    public Car(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

使用三種方式  < 對象 > 進行傳參數(shù);注意:使用此類型進行設置值必須有構造方法

對象的地址值:http://localhost:8080/car/get

package cn.tedu.controller;
//MVC里的C層,用來接受請求和做出響應(springmvc)
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController//接受請求,并把json數(shù)據(jù)返回
@RequestMapping("car")  //規(guī)定了url地址的寫法
public class CarController {
//方式一值會在網(wǎng)頁中出現(xiàn)
    @RequestMapping("get")
    public Car get(){
        Car c = new Car(10,"BMW",19.9);   //出發(fā)鉤造函數(shù),此處觸發(fā)的是含參構造;
        return c ;
    }
//方式二值會在網(wǎng)頁中出現(xiàn)
 @RequestMapping("save3")
    public Car save() {
        car.setAge(213);
        car.setSex("男");
        car.setId(32);
         car.setPrice(32);
        return car;
    }
方式三這種方式的值會在idea中打印不會再網(wǎng)頁中出現(xiàn)
@RequestMapping("save3")
    public Car save() {
        car.setAge(213);
        car.setSex("男");
        car.setId(32);
         car.setPrice(32);
        System.out.println(car);
}

使用return(值會網(wǎng)頁中出現(xiàn))的方式

package cn.tedu.controller;
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.naming.Name;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
//這是一個c層用來接收請求和做出響應
@RestController
//@RequestMapping("car")//規(guī)定了url的寫法此時的值可以任意寫
public class Controller {
 
 
   @RequestMapping("replace")
    public String replace(){
       // System.out.println(id+name+age);
 
        return "hkjds";
    }
//方式二值會在網(wǎng)頁中出現(xiàn)
 @RequestMapping("save3")
    public Car save() {
        car.setAge(213);
        car.setSex("男");
        car.setId(32);
         car.setPrice(32);
        return car;
    }
 
 
}
 
}

使用普通的get的方法進行上傳

package cn.tedu.controller;
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.naming.Name;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
//這是一個c層用來接收請求和做出響應
@RestController
//@RequestMapping("car")//規(guī)定了url的寫法此時的值可以任意寫
public class Controller {
   
 @RequestMapping("get2")
    public void get(Integer id,String name){//此處使用int類型必須賦值  引用類型不用必須賦值最好使用引用類型
        System.out.println(id+name);
    }
    @RequestMapping("get")
               public void get(Integer id){//此處使用int類型必須賦值  引用類型不用必須賦值
 
        System.out.println(id);
 
       }
 

restful風格進行傳參數(shù)

restful和普通的get的方法的區(qū)別:restful相對比較安全,寫法比較簡單

restful的地址值的:http://localhost:8080/car2/get2/10/jack/9

其他的url地址值://http://localhost:8080/car/get5?id=10&name=jack&price=9.9

package cn.tedu.controller;
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("car3")
//使用restful風格
public class CarController {
 
   @RequestMapping("get2/{sex}/{id}/{name}")//此地方的參數(shù)順序必須和下面以及地址值都必須一樣
public void  get2(@PathVariable String sex,
                 @PathVariable Integer id,
                 @PathVariable String name){
       System.out.println("數(shù)據(jù)插入成功"+sex+name+id);
      // System.out.println("數(shù)據(jù)插入成功"+name+id);
    }
   
 
}

spring mvc框架進行傳參數(shù)

package cn.tedu.controller;
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.naming.Name;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
//這是一個c層用來接收請求和做出響應
@RestController
//@RequestMapping("car")//規(guī)定了url的寫法此時的值可以任意寫
public class Controller {
    //使用框架接收網(wǎng)站參數(shù)
    @RequestMapping("get3")
   public void  get3(Car car){
       System.out.println(car.getSex()+car.getName()+car.getId());
   }
 
}

前后端參數(shù)傳入并且將數(shù)據(jù)傳入到數(shù)據(jù)庫中

package cn.tedu.controller;
 
import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.events.Event;
 
import javax.naming.Name;
import java.sql.*;
import java.util.Scanner;
 
@RestController
@RequestMapping("user")
public class UserContoller {
    @RequestMapping("save")
   public void save(Integer id,String name,Integer age) throws Exception {
        System.out.println(id+name+age);
        Class.forName("com.mysql.jdbc.Driver");
        //獲取連接
        String url ="jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false&amp;serverTimezone=Asia/Shanghai";
        Connection conn = DriverManager.getConnection(url,"root","root");
        //獲取傳輸器
//        String sql= "insert into user(id,name) values(?,?)";//給指定的字段設置值
        String sql= "insert into user values(?,?,?)";//所有字段設置值
        PreparedStatement ps = conn.prepareStatement(sql);
        //給SQL設置參數(shù)
        ps.setInt(1,id);//給第一個?設置值
        ps.setString(2,name);//給第二個?設置值
        ps.setInt(3,age);//給第三個?設置值
        //執(zhí)行SQL
        int rows = ps.executeUpdate();
        //釋放資源 -- OOM(OutOfMemory)
        ps.close();
        conn.close();
    }
 

到此這篇關于Spring mvc是如何實現(xiàn)與數(shù)據(jù)庫的前后端的連接操作的?的文章就介紹到這了,更多相關Spring mvc與數(shù)據(jù)庫的前后端的連接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot 快速實現(xiàn) api 加密的方法

    SpringBoot 快速實現(xiàn) api 加密的方法

    在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進行加密,常用的加密算法包括對稱加密(AES)和非對稱加密(RSA),本文給大家介紹SpringBoot 快速實現(xiàn) api 加密,感興趣的朋友一起看看吧
    2023-10-10
  • SpringBoot使用Apache Tika實現(xiàn)多種文檔的內(nèi)容解析

    SpringBoot使用Apache Tika實現(xiàn)多種文檔的內(nèi)容解析

    在日常開發(fā)中,我們經(jīng)常需要解析不同類型的文檔,如PDF、Word、Excel、HTML、TXT等,Apache Tika是一個強大的內(nèi)容解析工具,可以輕松地提取文檔中的內(nèi)容和元數(shù)據(jù)信息,本文將通過SpringBoot和Apache Tika的結(jié)合,介紹如何實現(xiàn)對多種文檔格式的內(nèi)容解析
    2024-12-12
  • Java之不通過構造函數(shù)創(chuàng)建一個對象問題

    Java之不通過構造函數(shù)創(chuàng)建一個對象問題

    這篇文章主要介紹了Java之不通過構造函數(shù)創(chuàng)建一個對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java數(shù)組實例練習題整理

    Java數(shù)組實例練習題整理

    在本篇文章中小編給各位分享的是關于Java數(shù)組實例練習題以及相關代碼整理,有需要的朋友們跟著學習下。
    2019-07-07
  • IDEA利用jclasslib 修改class文件的實現(xiàn)

    IDEA利用jclasslib 修改class文件的實現(xiàn)

    這篇文章主要介紹了IDEA利用jclasslib 修改class文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • spring security實現(xiàn)下次自動登錄功能過程解析

    spring security實現(xiàn)下次自動登錄功能過程解析

    這篇文章主要介紹了spring security實現(xiàn)記住我下次自動登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • spring5 webclient使用指南詳解

    spring5 webclient使用指南詳解

    本篇文章主要介紹了spring 5 webclient使用指南詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Java編程Iterator迭代器設計原理及實現(xiàn)代碼示例

    Java編程Iterator迭代器設計原理及實現(xiàn)代碼示例

    這篇文章主要介紹了Java編程Iterator迭代器設計原理及實現(xiàn)代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • 基于log4j2.properties踩坑與填坑

    基于log4j2.properties踩坑與填坑

    這篇文章主要介紹了log4j2.properties踩坑與填坑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring MVC處理參數(shù)中的枚舉類型通用實現(xiàn)方法

    Spring MVC處理參數(shù)中的枚舉類型通用實現(xiàn)方法

    這篇文章主要給大家介紹了關于Spring MVC處理參數(shù)中的枚舉類型通用實現(xiàn)方法的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-11-11

最新評論

方城县| 班玛县| 祁东县| 山西省| 甘肃省| 同江市| 长泰县| 峨眉山市| 连平县| 临沭县| 荔浦县| 新乡县| 咸丰县| 东明县| 武宁县| 淮滨县| 化德县| 肇庆市| 张家港市| 景东| 中超| 高雄县| 工布江达县| 温州市| 永宁县| 五指山市| 余姚市| 延安市| 平江县| 阜新市| 铜川市| 绥棱县| 揭东县| 和田市| 长子县| 永胜县| 黄山市| 阳泉市| 普陀区| 莱阳市| 千阳县|