1.概述
简化了get方式参数的写法
普通的get传递的参数http://localhost:8080/car/get?id=100&name=张三
restful传递的参数http://localhost:8080/car/get2/100/张三
2.测试
创建RunApp启动类
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);
}
}
创建arController类
package cn.tedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@Controller
//@ResponseBody
@RestController
@RequestMapping("car")
public class CarController {
//注意1:: 参数列表里的参数类型,最好使用引用类型,
//如果浏览器没有传值过来就用默认值,但使用基本类型会抛异常的
//解析普通的get传递的参数
//http://localhost:8080/car/get?id=100&name=张三
@RequestMapping("get")
// public String get(int id,String name){
public String get(Integer id,String name){
return id+name ;
}
//解析restful传递的参数:简化了get方式参数的写法
//http://localhost:8080/car/get2/100/张三
@RequestMapping("get2/{id}/{name}")
//{x}--通过{}获取访问路径中携带的参数,并且交给变量x保存
//@PathVariable -- 获取{}中间变量的值
public String get2(@PathVariable Integer id,
@PathVariable String name){
return id+name;
}
//http://localhost:8080/car/get3/100/张三/red/9.9
@RequestMapping("get3/{a}/{b}/{c}/{d}")
public String get3(@PathVariable Integer a,
@PathVariable String b,
@PathVariable String c,
@PathVariable double d){
return a+b+c+d ;
}
}
创建前端网页文件
解析get的参数 解析restful风格的参数 练习解析restful风格的参数
测试
二.SpringMVC解析post的请求参数
0.项目结构
1.准备form表单
body{ font-size: 17px; background-color: lightgray; } .a{ width: 320px; height: 50px; font-size: 20px; } input[type="submit"]{ background-color: #0000FF; border-color: #0000FF; color: white; width: 100px; height: 50px; font-size: 20px; } input[type="button"]{ background-color: #FF69B4; border-color: #FF69B4; color: white; width: 100px; height: 50px; font-size: 20px; } 解析get的参数 解析restful风格的参数 练习解析restful风格的参数
2.准备Student类
package cn.tedu.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Arrays;
import java.util.Date;
//是Model层,用来封装数据,就是一个pojo(封装的属性+get/set)
public class Student {
//属性(成员变量):变量类型 变量名
//提交数据的类型 页面上name属性的值
private String name ;
private Integer age ;//避免了一些异常
private Integer sex ;
private String[] hobby ;
private Integer edu ;
//浏览器上提交的日期默认是String类型,2012/8/12,报错400
//@DateTimeFormat把String的日期转成Date日期
//pattern属性规定了日期的格式y表示年M表示月d表示日
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date intime;
//get set tostring
public String getName() {
return name;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
}
3.准备StudentController类
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//是C层,控制层,用来接受请求和给出响应
@RestController
@RequestMapping("stu")
public class StudentController {
@RequestMapping("add")
public Object add(Student s){
//TODO 实现入库insert
return s;
}
}
4.利用jdbc把接受到的参数入库
操作cgb2106的库,创建tb_student表(参数Student类)
CREATE TABLE tb_student( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAr(50), age INT, sex INT, hobby VARCHAr(100), edu INT, intime DATE )
修改pom.xml文件,添加jdbc的jar包坐标
cgb2106boot03 cn.tedu 0.0.1-SNAPSHOT 4.0.0 day14mysql mysql-connector-java5.1.48
写jdbc代码
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;
//是C层,控制层,用来接受请求和给出响应
@RestController
@RequestMapping("stu")
public class StudentController {
@RequestMapping("add")
public Object add(Student s) throws Exception {
//TODO 利用jdbc,实现入库
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql:///cgb2106?characterEncoding=utf8";
Connection conn = DriverManager.getConnection(url,"root","root");
//获取传输器
String sql = "insert into tb_student values(null,?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
//给SQL设置参数
ps.setObject(1,s.getName());
ps.setObject(2,s.getAge());
ps.setObject(3,s.getSex());
//s.getHobby()得到一个数组,不能直接存入数据库,需要变成串入库
ps.setObject(4, Arrays.toString( s.getHobby() ) ) ;
ps.setObject(5,s.getEdu());
ps.setObject(6,s.getIntime());
//执行SQL
ps.executeUpdate();//执行增删改的SQL
System.out.println("数据插入成功!");
return s;
}
}
5.测试
6.总结
三.Git1.概述
是一个版本控制产品用来实现资源的版本控制
可以把资源随时上传到Git上,可以随时拉取下载
好处:快速恢复到历史版本,容错性高
远程仓库: 是指 Gitee官网 的网址,存你已经传上去的资源
本地仓库: 是指你磁盘里的一个路径,存你即将要上传的资源
本地索引: 是指将要提交的数据建立索引,方便查找定位
工作空间: 保存了资源的位置
过程: 工作空间 -> 本地索引 -> 本地仓库 -> 远程仓库
2.常用命令
add: 把工作空间的资源,添加到 本地索引
commit: 把本地索引的资源 提交到 本地仓库
push: 把本地仓库的资源 推送到 远程仓库
pull/clone: 把资源从远程仓库下载下来
3.使用步骤
1, 安装Git软件
2, 在Gitee官网注册账号,使用账号上传资源
3, 创建本地仓库, 就是在你的磁盘里建一个文件夹,存放即将上传的资源
仅供参考: D:workspacegiteecgb2106
4, 创建远程仓库, 去Gitee官网创建,存本地仓库上传的资源
Gitee官网右上角的加号,点新建仓库,设置仓库名称选成开源,ok
5, 需要在本地仓库那里,执行一些Git命令.
git config --global user.name "cgblpx" #设置了Gitee注册的用户名 git config --global user.email "2250432165@qq.com" #设置了Gitee注册的邮箱 git config --list # 查看设置信息 git init #初始化一个Git的环境 #在你的本地仓库创建一个文件,准备上传它 git add 1.txt #添加,从工作空间到本地索引 git commit -m "first commit" #从本地索引提交到本地仓库 git remote add origin https://gitee.com/cgblpx/cgb2106test.git #添加到指定远程仓库里 git push -u origin master #从本地仓库推送到远程仓库 Username for 'https://gitee.com': #输入自己注册的账号 Password for 'https://cgblpx@gitee.com': #输入自己注册的密码
4.检查
1, 把你要提交的资源拷贝到 本地仓库
2, 在本地仓库处, 执行以下Git命令提交资源
3, 把远程仓库的资源下载到本地
git add . git commit -m "test" git push -u origin master git clone 再加上要下载的资源的网址



