cgb2108-day17

一,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,去 码云 官网注册 账号

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值