文章目錄
- 一,練習前后端整合,包括入庫
- --1,java代碼
- --2,前端代碼
- --3,測驗
- --4,總結
- 二,SpringMVC框架決議post提交的請求引數
- --0,專案結構
- --1,準備表單
- --2,準備啟動類
- --3,準備Controller類,決議請求資料
- --4,創建Student類,用來封裝資料
- --5,測驗
- --6,擴展:入庫
- 修改pom,添加jdbc的jar包
- 創建資料庫表
- 創建StudentController類,接受請求,包括入庫
- 總結
- --7,擴展:修改Tomcat埠號
- 三,Spring框架
- --1,概述
- --2,IOC的使用
- 創建類
- 創建組態檔,配置類的資訊
- 創建測驗類--預習 !!
一,練習前后端整合,包括入庫
–1,java代碼
package cn.tedu.hello;
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("car")
public class CarController2 {
//1,決議瀏覽器發來的請求引數
//http://localhost:8080/car/save3?
//id=1&color=red&price=100&pinpai=BMW&type=X7
@RequestMapping("save3")
public Object save3(Car c) throws Exception {
//TODO 把決議到的請求引數 getXxx()入庫--jdbc
//1,pom里加jdbc的坐標
//2,在資料庫里創建car表(提供id,color,price,pinpai,type欄位)
/* CREATE TABLE car(
id INT PRIMARY KEY AUTO_INCREMENT,
color VARCHAR(20),
price DOUBLE,
pinpai VARCHAR(10),
TYPE VARCHAR(10)
) */
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//獲取資料庫連接
String url="jdbc:mysql://localhost:3306/cgb2107?characterencoding=utf8";
Connection con = DriverManager.getConnection(url,"root","root");
//獲取傳輸器
String sql = "insert into car values(null,?,?,?,?)";
PreparedStatement p = con.prepareStatement(sql);
//給SQL設定引數---引數的順序一定要和欄位的順序一致
p.setObject(1,c.getColor());
p.setObject(2,c.getPrice());
p.setObject(3,c.getPinpai());
p.setObject(4,c.getType());
//執行SQL
p.executeUpdate();
//釋放資源
p.close(); con.close();
//{"id":1,"color":"red","price":100.0,"pinpai":"BMW","type":"X7"}
return c;
}
}
–2,前端代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>前后端整合</title>
</head>
<body>
<a href="http://localhost:8080/user/insert">普通訪問</a>
<a href="http://localhost:8080/user/insert?id=10&name=rose&age=20">普通的get提交方式</a>
<a href="http://localhost:8080/user/insert2/10/rose/20">restful方式</a>
<a href="http://localhost:8080/car/save3?id=1&color=red&price=100&pinpai=BMW&type=X7">點我獲取汽車資料</a>
<a href="http://localhost:8080/car/save3?id=1&color=red&price=100&pinpai=BMW&type=X7">點我保存汽車資料</a>
</body>
</html>
–3,測驗
啟動服務器后 ,打開瀏覽器測驗


–4,總結

二,SpringMVC框架決議post提交的請求引數
–0,專案結構

–1,準備表單

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body{
font-size: 17px; /* 字號 */
background-color: lightpink; /* 背景色 */
}
/* 輸入框 */
.a{
width: 300px; /* 寬度*/
height: 40px; /* 高度 */
font-size: 20px; /* 字號 */
}
/* 保存按鈕 */
input[type="submit"]{
/* 背景色 字的顏色 邊框顏色 寬度 高度 */
background-color: #0000FF;
border-color: #0000FF;
color: white;
width: 100px;
height: 40px;
font-size: 20px; /* 字號 */
}
/* 取消按鈕 */
input[type="button"]{
/* 背景色 字的顏色 邊框顏色 寬度 高度 */
background-color: #FF69B4;
border-color: #FF69B4;
color: white;
width: 100px;
height: 40px;
font-size: 20px; /* 字號 */
}
</style>
</head>
<body>
<!-- 表單可以提交資料,默認get方式.
method="post"指定提交方式,action=""指定跳轉的網址
要求:1,必須是form 2,必須有submit按鈕 3,必須配置name屬性
-->
<form method="post" action="http://localhost:8080/student/save">
<table>
<tr>
<td>
<h2>學生資訊管理系統MIS</h2>
</td>
</tr>
<tr>
<td>姓名:</td>
</tr>
<tr>
<td>
<input class="a" type="text" placeholder="請輸入姓名" name="name">
</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="1">本科</option>
<option value="2">專科</option>
<option value="3">博士</option>
</select>
</td>
</tr>
<tr>
<td>
入學日期:
<input type="date" name="intime"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="保存"/>
<input type="button" value="取消"/>
</td>
</tr>
</table>
</form>
</body>
</html>
–2,準備啟動類
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);
}
}
–3,準備Controller類,決議請求資料
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 {
//決議請求引數
@RequestMapping("save")
// public String save(String name,Integer age,String sex,String hobby){
public String save(Student s){
//Student{name='jack', age=20, sex=0, hobby=[ppq, ps, cg], edu=2, intime=Wed Sep 15 00:00:00 CST 2021}
System.out.println(s);
return "保存成功!" ;
}
}
–4,創建Student類,用來封裝資料
package cn.tedu.pojo;
import java.util.Arrays;
import java.util.Date;
//充當了MVC的M層,model層,用來封裝資料
//pojo類里只有屬性和set() get()
public class Student {
//規則: 屬性的型別 屬性的名字
//和HTML頁面保持一致:參考頁面輸入的值 參考頁面中name屬性的值
private String name;//用來封裝用戶在瀏覽器輸入的用戶名
private Integer age;//用來封裝用戶在瀏覽器輸入的年齡
private Integer sex;//用來封裝用戶在瀏覽器選擇的性別
private String[] hobby;//用來封裝用戶在瀏覽器選擇的愛好--多選的結果存入陣列
private Integer edu;
//400例外的原因:頁面上輸入的日期是String型別
//String->Date: @DateTimeFormat,需要指定日期的格式y是年M是月d是天
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date intime;//用來封裝用戶在瀏覽器選擇的日期
//get set tostring
public String getName() {
return name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
public void setName(String name) {
this.name = name;
}
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;
}
}
–5,測驗

–6,擴展:入庫
修改pom,添加jdbc的jar包
直接在最外面的pom里加就行了,里面的每個module都可以用
<!--添加了jdbc的jar包 右鍵-generate-dependency-搜 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
創建資料庫表
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT,
sex INT,
hobby VARCHAR(100),
edu INT,
intime DATE
)
創建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;
import java.util.Arrays;
//和瀏覽器互動,接受請求,給出回應
@RestController
@RequestMapping("student")
public class StudentController {
//決議請求引數
@RequestMapping("save")
// public String save(String name,Integer age,String sex,String hobby){
public String save(Student s) throws Exception {
//TODO 把決議成功的學生資料 入庫
//1,修改pom添加jar包(直接改了project里的pom.xml)
//2,建表student
/*
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT,
sex INT,
hobby VARCHAR(100),
edu INT,
intime DATE
)
*/
//3,寫jdbc
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//獲取資料庫連接
String url="jdbc:mysql://localhost:3306/cgb2107?characterencoding=utf8";
Connection con = DriverManager.getConnection(url,"root","root");
//獲取傳輸器--插入的值的個數參考欄位的個數,順序也是參考欄位的順序
String sql="insert into student values(null,?,?,?,?,?,?)";
PreparedStatement p = con.prepareStatement(sql);
//設定SQL的引數
p.setObject(1,s.getName());
p.setObject(2,s.getAge());
p.setObject(3,s.getSex());
//注意:資料庫里根本沒有陣列的型別,需要String[]->String,否則Data too long for column 'hobby' at row 1
p.setObject(4, Arrays.toString(s.getHobby()) );
p.setObject(5,s.getEdu());
p.setObject(6,s.getIntime());
//執行SQL
p.executeUpdate();
//釋放資源
p.close(); con.close();
return "保存成功!" ;
}
}
總結

–7,擴展:修改Tomcat埠號

三,Spring框架
–1,概述
Spring框架重點提供的: IOC DI AOP
IOC : 控制翻轉, 是指把創建物件的權利交給spring.
DI : 依賴注入, 是指把有依賴關系的物件也同時new出來.
AOP : 面向切面編程, 補充了OOP不足
–2,IOC的使用

創建類
package cn.tedu.spring;
public class Hello {
public void hi(){
System.out.println("hello springioc~");
}
}
創建組態檔,配置類的資訊
<?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,配置bean的位置,spring自動完成ioc
class屬性用來描述類的全路徑, id屬性用來作為bean的唯一標識
-->
<bean class="cn.tedu.spring.Hello" id="hello"></bean>
</beans>
創建測驗類–預習 !!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300465.html
標籤:java
上一篇:2021-09-15
