文章目錄
- 一,Restful決議引數的練習
- --1,創建類
- --2,創建Order類
- --3,創建前端網頁
- 二,SpringMVC框架決議post提交的資料
- --1,創建網頁,提供表單
- --2,創建Maven Module
- --3,創建啟動類
- --4,創建StudentController決議請求引數
- --5,創建Student類
- --6,總結
- 三,擴展把資料入庫
- --0,添加jdbc的jar包
- --1,改造StudentController類
- --2,創建表
- --3,總結
- 四,Spring框架
- --1,概述
- --2,IOC的XML方式實作
- 創建Hello類
- 創建組態檔,配置類的資訊,進行IOC
- 創建測驗類,直接獲取物件
- 五,Git
- --1,概述
- --2,使用步驟
- --3,執行以下命令:
- --4,檢查是否成功:
- --5,查看上傳的資料
- 任務
一,Restful決議引數的練習
–1,創建類
package cn.tedu.mvc;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//MVC的C層Controller控制器,用來接受請求 給出回應
//總結:瀏覽器提交資料方式
//get:把資料用?進行拼接,多個資料之間用&連接(?id=10&price=100)
//java程式決議請求引數:在方法的引數串列中,依次決議(或者封裝成java物件)
//restful:可以簡化get提交資料(/10/100)
//java程式決議請求引數:使用{變數}來決議地址欄里的資料
//+使用@PathVariable獲取變數的值
@RestController
@RequestMapping("order")
public class OrderController {
//http://localhost:8080/order/get?id=10&price=100&name=phone
@RequestMapping("get")
// public String get(Integer id,Double price,String name){
public Order get(Order o){
return o;
}
//http://localhost:8080/order/find
@RequestMapping("find/{id}")
public Integer find(@PathVariable Integer id){
return id;
}
//http://localhost:8080/order/save/10/100/phone
@RequestMapping("save/{id}/{price}/{name}")
// public String save(@PathVariable Integer id,
// @PathVariable Double price,
// @PathVariable String name){
//注意:restful決議引數時,標準是兩步: {變數}+@PathVariable獲取變數的值
//但是,引數串列如果是一個java物件,就不許加@PathVariable否則500例外!!
public String save(Order o){
//jdbc入庫
return o+"" ;
}
}
–2,創建Order類
package cn.tedu.mvc;
public class Order {
private Integer id;
private Double price;
private String name;
//get set toString
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", price=" + price +
", name='" + name + '\'' +
'}';
}
}
–3,創建前端網頁
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>瀏覽器向 服務器發起請求</title>
</head>
<body>
<a href="http://localhost:8080/order/get?id=10&price=100&name=phone">練習1</a>
<a href="http://localhost:8080/order/find/2">練習2</a>
<a href="http://localhost:8080/order/save/10/100/phone">練習3</a>
<!--
<button @click="fun()">點我提交資料</button>
優化:
點擊按鈕,發起axios請求,訪問后端java程式,
java程式決議請求引數拿著引數,入庫.
-->
</body>
</html>
二,SpringMVC框架決議post提交的資料
–1,創建網頁,提供表單

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測驗 表單提交資料</title>
<style>
/* 設定輸入框的寬度高度 */
.a{
width:300px;
height: 30px;
}
/* 設定保存按鈕 */
#btn1{
background-color: #0000FF;/* 背景色 */
border-color: #0000FF;/* 邊框色 */
color: white;/* 文字顏色 */
width: 60px;/* 寬度高度 */
height: 30px;/* 寬度高度 */
}
/* 設定取消按鈕 */
#btn2{
background-color: hotpink;/* 背景色 */
border-color: hotpink;/* 邊框色 */
color: white;/* 文字顏色 */
width: 60px;/* 寬度高度 */
height: 30px;/* 寬度高度 */
}
</style>
</head>
<body>
<!--提交資料的要求:用form標簽+有submit按鈕+有name屬性-->
<form method="post" action="http://localhost:8080/student/save">
<h1>學生資訊管理系統MIS</h1>
<table>
<tr>
<td>姓名:</td>
</tr>
<tr>
<td>
<input class="a" type="text" placeholder="姓名..." name="user" />
</td>
</tr>
<tr>
<td>年齡:</td>
</tr>
<tr>
<td>
<input class="a" type="number" placeholder="年齡..." name="age" />
</td>
</tr>
<tr>
<td>
性別:(單選框)
<input type="radio" name="sex" value="1" checked="checked"/>男
<input type="radio" name="sex" value="0"/>女
</td>
</tr>
<tr>
<td>
愛好:(多選)
<input type="checkbox" name="hobby" value="ppq" checked="checked"/>乒乓球
<input type="checkbox" name="hobby" value="ps"/>爬山
<input type="checkbox" name="hobby" value="cg"/>唱歌
</td>
</tr>
<tr>
<td>
學歷:(下拉框)
<select name="edu">
<option value="0">小學</option>
<option value="1">初中</option>
<option value="2">高中</option>
<option value="3">本科</option>
<option value="4">博士</option>
</select>
</td>
</tr>
<tr>
<td>入學日期:</td>
</tr>
<tr>
<td>
<input type="date" name="intime"/>
</td>
</tr>
<tr>
<td>
<button type="submit" id="btn1">保存</button>
<button type="reset" id="btn2">取消</button>
</td>
</tr>
</table>
</form>
</body>
</html>
–2,創建Maven Module

–3,創建啟動類
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);
}
}
–4,創建StudentController決議請求引數
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
//http://localhost:8080/student/save
@RequestMapping("save")
public String save(Student s){
return "訪問成功!"+s;
}
}
–5,創建Student類
package cn.tedu.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Arrays;
import java.util.Date;
//提供的屬性用來: 封裝 瀏覽器發來的資料
//要求:
// 1,變數名 必須和 網頁中name屬性的值 一樣
// 2,變數型別 必須和 瀏覽器提交的資料型別 一樣
public class Student {
//?user=jack&age=20&sex=1
private String user;
private Integer age;
private Integer sex;
private String[] hobby;
//用來保存,瀏覽器提交來的多個愛好ppq ps cg
private Integer edu;
//問題:原因是瀏覽器上選的日期是String型別:2021/10/17
//無法把String型別的日期變成 java.util.Date型別,報錯400!!!!
// @DateTimeFormat用來轉換日期的格式.y表示年M表示月d表示日
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date intime;
//get set toString
@Override
public String toString() {
return "Student{" +
"user='" + user + '\'' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getEdu() {
return edu;
}
public void setEdu(Integer edu) {
this.edu = edu;
}
public Date getIntime() {
return intime;
}
public void setIntime(Date intime) {
this.intime = intime;
}
}
–6,總結

三,擴展把資料入庫
–0,添加jdbc的jar包
修改pom.xml檔案,添加jar包的坐標.
1, 整個工程的pom.xml檔案: 作用在每個Module里 --作用范圍大
2, Module的pom.xml檔案: 只作用在當前的Module里 --作用范圍小
找到dependencies標簽,添加以下代碼:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
–1,改造StudentController類
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@RestController
@RequestMapping("student")
public class StudentController {
//http://localhost:8080/student/save
@RequestMapping("save")
public String save(Student s) throws Exception {
//TODO JDBC入庫
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2,獲取連接(url user pwd)
String url= "jdbc:mysql://localhost:3306/cgb2108?characterEncoding=utf8";//指定要連接哪個資料庫
String user= "root" ; //使用的用戶名
String pwd= "root" ; //使用的密碼
Connection c = DriverManager.getConnection(url, user, pwd);
//3,獲取傳輸器(用新傳輸器,高效安全,先執行SQL骨架)
String sql = "insert into tb_student values(null,?,?,?,?,?,?)";
PreparedStatement ps = c.prepareStatement(sql);
//TODO 給SQL里的?設定引數--s.getXxx獲取資料
ps.setString(1,s.getUser());
ps.setInt(2,s.getAge());
ps.setInt(3,s.getSex());
//s.getHobby()獲取到了陣列,入庫,資料庫不認識陣列,需要變成字串才能入庫,否則500
ps.setObject(4, Arrays.toString( s.getHobby() ) );
ps.setObject(5,s.getEdu());
ps.setObject(6,s.getIntime());
//4,執行SQL(insert)
ps.executeUpdate();
//5,關閉資源
ps.close();
c.close();
System.out.println("資料入庫成功!");
return "訪問成功!"+s;
}
}
–2,創建表
CREATE TABLE tb_student(
id INT PRIMARY KEY AUTO_INCREMENT,
USER VARCHAR(100),
age INT,
sex INT,
hobby VARCHAR(200),
edu INT,
intime DATE
)
–3,總結

四,Spring框架
–1,概述
Spring框架可以和其他技術無縫銜接
BeanFactory: bean工廠, spring框架認為所有類都是bean. 從bean工廠可以獲取每個bean
IOC: 控制反轉, 不需要程式員來創建物件了,交給Spring框架來管理物件(從初始化…銷毀).程式員可以直接從 Spring框架中獲取Bean的物件
DI: 依賴注入,使用Spring框架明確兩個物件間的依賴關系
AOP: 面向切面編程,是一種思想,解決了OOP的不足
–2,IOC的XML方式實作
創建Hello類
package cn.tedu.spring;
public class Hello {
public void show(){
System.out.println("show()被呼叫成功!");
}
}
創建組態檔,配置類的資訊,進行IOC
在resources檔案夾位置,右鍵-new-XML config…-Spring config-輸入檔案名
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
spring認為萬物都是bean,只要你的類,交給spring框架,spring就能IOC
IOC是控制反轉:是指Spring框架會幫你創建物件,你來獲取物件
id屬性是每個bean標簽的唯一標識,class屬性是用來指定類的全路徑
IOC底層Map的資料結構{類,類的物件}
-->
<bean id="hello" class="cn.tedu.spring.Hello"></bean>
</beans>
創建測驗類,直接獲取物件
package cn.tedu.ioc;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
//junit單元測驗方法:測驗一段代碼的結果@Test
@Test
public void get(){
//1,讀取組態檔--引數是組態檔的名字
ClassPathXmlApplicationContext spring =
new ClassPathXmlApplicationContext(
"spring.xml");
//2,獲取物件--引數是組態檔里,bean標簽的id的屬性值
Object o = spring.getBean("hello");
//cn.tedu.spring.Hello@4550bb58
System.out.println(o);
}
}
五,Git
–1,概述
用來進行代碼的版本控制.
1,遠程倉庫: 是一個網站,用來存你上傳的代碼,國內用Gitee碼云,國外用GitHub
2,本地倉庫: 是你自己創建的一個檔案夾路徑,用來存你即將上傳的代碼(參考E:\workspace\gitee)
3,上傳資源
add: 把即將上傳的代碼從作業空間到本地索引
commit: 把即將上傳的代碼從本地索引到本地倉庫
push: 把即將上傳的代碼從本地倉庫到遠程倉庫
4,下載資源
clone/pull: 把代碼從遠程倉庫下載到自己電腦里
5,以后作業中: 每天下班前,需上傳資源. 每天上班時,下載資源.
6,使用Git的前提: 安裝Git的軟體, 在碼云上注冊賬號
–2,使用步驟
1,創建本地倉庫: E:\workspace\gitee,用來存即將上傳的代碼
2,創建遠程倉庫: 存你上傳的代碼.去碼云官網創建一個開源的倉庫(設定倉庫名字)
3,上傳前,先保證本地倉庫有東西能傳別空著
4,正式上傳:需要在本地倉庫的位置執行Git命令

–3,執行以下命令:
git config --global user.name "cgblpx"
git config --global user.email "2250432165@qq.com"
mkdir cgb210801
cd cgb210801
git init
在本地倉庫中,創建檔案1.txt
git add .
git commit -m "first commit"
git remote add origin https://gitee.com/cgblpx/cgb210801.git
git push -u origin master
第一次上傳的話,輸入Gitee注冊時的賬號和密碼就行了
–4,檢查是否成功:

–5,查看上傳的資料
打開Gitee網站,多重繪幾次,就看到上傳的內容了
任務
–1,安裝git.exe
–2,去 碼云 官網注冊 賬號
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/333784.html
標籤:java
