色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:3日期:2023-07-05 08:56:21
Spring mvc與數(shù)據(jù)庫的前后端的連接

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

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

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

啟動(dòng)類:注意 ,啟動(dòng)類必須在啟動(dòng)類中進(jìn)行執(zhí)行.必能在idea的上面進(jìn)行啟動(dòng),否則會(huì)啟動(dòng)其他的啟動(dòng)類導(dǎo)致報(bào)錯(cuò)

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

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

注意:這里使用的是構(gòu)造方法 主要的作用是方便new

package cn.tedu.pojo;//Model用來封裝數(shù)據(jù)public class Car { private int id; private String name; private double price; //Constructor構(gòu)造方法,用來方便的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; }}使用三種方式 < 對(duì)象 > 進(jìn)行傳參數(shù);注意:使用此類型進(jìn)行設(shè)置值必須有構(gòu)造方法

對(duì)象的地址值:http://localhost:8080/car/get

package cn.tedu.controller;//MVC里的C層,用來接受請(qǐng)求和做出響應(yīng)(springmvc) import cn.tedu.pojo.Car;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestController//接受請(qǐng)求,并把json數(shù)據(jù)返回@RequestMapping('car') //規(guī)定了url地址的寫法public class CarController {//方式一值會(huì)在網(wǎng)頁中出現(xiàn) @RequestMapping('get') public Car get(){Car c = new Car(10,'BMW',19.9); //出發(fā)鉤造函數(shù),此處觸發(fā)的是含參構(gòu)造;return c ; }//方式二值會(huì)在網(wǎng)頁中出現(xiàn) @RequestMapping('save3') public Car save() {car.setAge(213);car.setSex('男');car.setId(32); car.setPrice(32);return car; }方式三這種方式的值會(huì)在idea中打印不會(huì)再網(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(值會(huì)網(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; //這是一個(gè)c層用來接收請(qǐng)求和做出響應(yīng)@RestController//@RequestMapping('car')//規(guī)定了url的寫法此時(shí)的值可以任意寫public class Controller { @RequestMapping('replace') public String replace(){ // System.out.println(id+name+age); return 'hkjds'; }//方式二值會(huì)在網(wǎng)頁中出現(xiàn) @RequestMapping('save3') public Car save() {car.setAge(213);car.setSex('男');car.setId(32); car.setPrice(32);return car; } } }使用普通的get的方法進(jì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; //這是一個(gè)c層用來接收請(qǐng)求和做出響應(yīng)@RestController//@RequestMapping('car')//規(guī)定了url的寫法此時(shí)的值可以任意寫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風(fēng)格進(jìn)行傳參數(shù)

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

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風(fēng)格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框架進(jìn)行傳參數(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; //這是一個(gè)c層用來接收請(qǐng)求和做出響應(yīng)@RestController//@RequestMapping('car')//規(guī)定了url的寫法此時(shí)的值可以任意寫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(?,?)';//給指定的字段設(shè)置值String sql= 'insert into user values(?,?,?)';//所有字段設(shè)置值PreparedStatement ps = conn.prepareStatement(sql);//給SQL設(shè)置參數(shù)ps.setInt(1,id);//給第一個(gè)?設(shè)置值ps.setString(2,name);//給第二個(gè)?設(shè)置值ps.setInt(3,age);//給第三個(gè)?設(shè)置值//執(zhí)行SQLint rows = ps.executeUpdate();//釋放資源 -- OOM(OutOfMemory)ps.close();conn.close(); }

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美三级黄色大片 | 毛片一级做a爰片性色 | 日韩欧美精品在线观看 | 成人网18免费网站在线 | 亚洲天堂影院在线观看 | 新版天堂资源中文8在线 | 欧美精品国产制服第一页 | 日本亚洲免费 | 一级毛片免费播放视频 | 成 人免费视频l免费观看 | 日本午夜小视频 | 日本免费一区二区三区视频 | 精品国产一区二区三区久 | 99国产精品热久久久久久夜夜嗨 | 成年女人免费视频 | 欧美亚洲综合网 | 亚洲日韩中文字幕天堂不卡 | 91年精品国产福利线观看久久 | 国产综合在线观看 | 福利社色 | 亚洲国产日韩欧美综合久久 | 久草在线视频精品 | 国产三级做爰在线观看 | 99国产精品欧美久久久久久影院 | 三级网址在线观看 | 一级一片免费视频播放 | 在线国产一区二区 | 国产精品成人观看视频网站 | 欧美亚洲视频一区 | 颜值超高的女神啪啪 | 亚洲欧美综合一区二区三区四区 | 国厂自拍 | 美女午夜色视频在线观看 | 国内偷自第一二三区 | 国产精品亚洲欧美日韩区 | 成人国产精品视频 | 波多野结衣一级片 | 国产一区亚洲二区三区 | 国产乱弄视频在线观看 | 99热国产免费 | 国产精品1区2区3区在线播放 |