栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot 基础知识 - 1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot 基础知识 - 1

文章目录
  • 1. SpringBoot
    • 1.1 概述
    • 1.2 使用
  • 2 SpringMVC框架
    • 2.1 概述
    • 2.2 工作原理
    • 2.3 解析请求参数
  • 3 SpringMVC解析get方式的请求参数
    • 3.1 测试
    • 3.2 总结
  • 4 RestFul数据的解析
    • 4.1 测试
  • 5 练习
    • 5.1 解析请求参数
    • 5.2 Restful解析参数的练习
  • 6 SpringMVC框架解析post提交的数据
    • 6.1 创建网页,提供表单
    • 6.2 创建Maven Module
    • 6.3 创建启动类
    • 6.4 创建StudentController解析请求参数
    • 6.5 创建Student类
    • 6.6 总结
  • 7 扩展把数据入库
    • 7.1 添加jdbc的jar包
    • 7.2 改造StudentController类
    • 7.3 创建表
    • 7.4 总结
  • 8 Spring框架
    • 8.1 概述
    • 8.2 IOC的XML方式实现
  • 9 Git
    • 9.1 概述
    • 9.2 使用步骤
    • 9.3 执行以下命令
    • 9.4 检查是否成功
    • 9.5 查看上传的数据


1. SpringBoot 1.1 概述

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。成为SpringBoot全家桶。

特点:

  • 创建独立的Spring应用程序
  • 嵌入的Tomcat,无需部署WAR文件
  • 简化Maven配置
  • 自动配置Spring
  • 提供生产就绪型功能,如指标,健康检查和外部配置
1.2 使用
  1. 创建SpringBoot项目–配置Maven
  2. 启动服务器,并且访问服务器里的资源
  3. 添加类
package cn.tedu.cgb2108boot01;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello {
    @RequestMapping("abc")
    public String get(){
        return "hello boot~";
    }
}


  1. 打开浏览器测试
    http://localhost:8080/abc
2 SpringMVC框架 2.1 概述

是Spring团队的产品,遵循MVC设计模式
MVC设计模式: 最终实现松耦合
M是Model是模型层,用来封装数据
V是View是视图层,用来展示数据
C是Controller是控制层, 接受浏览器发来的请求,并做出数据的响应
SpringMVC框架用来接受请求 + 做出响应

2.2 工作原理

涉及五个组件:

  1. 前端控制器DispatcherServlet: 接受请求,并且调度
  2. 处理器映射器HandlerMapping: 根据地址栏的写法,找到能处理这次请求的类和方法
  3. 处理器适配器HandlerAdapter: 真正开始找到方法,执行方法体处理业务,并返回结果
  4. 视图解析器ViewResolver: 找到能够展示数据的页面
  5. 视图渲染View: 把数据展示在页面上
2.3 解析请求参数

准备:

浏览器发送数据给服务器有两种方式? get 和 post
get 的数据.在地址栏展示,用?拼接的参数
post的数据,安全不在地址栏展示
开发步骤: 1,导入SpringMVC相关的jar包(被Springboot整合了) 2, 使用注解开发

创建maven module:
右键-new -Module-选择Maven-next-设置module name-finish

准备启动类:

package cn.tedu.mvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//启动类,用来启动Module
public class RunApp {
    public static void main(String[] args) {
        //利用springboot启动当前类
        SpringApplication.run(RunApp.class);
    }
}


准备资源:

package cn.tedu.mvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//springmvc框架核心:接受请求
public class CarController {
    //规定浏览器用什么规则访问资源
    @RequestMapping("get")
    public String get(){
       return "欢迎~";
    }
}


测试:
http://localhost:8080/car/get

3 SpringMVC解析get方式的请求参数 3.1 测试
package cn.tedu.mvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//springmvc框架核心:接受请求
@RequestMapping("car")//规定浏览器用什么规则访问资源
public class CarController {
    //http://localhost:8080/car/get
    @RequestMapping("get")//规定浏览器用什么规则访问资源
    public String get(){
       return "欢迎~";//直接把结果返回给浏览器展示
    }
    //携带着请求参数: http://localhost:8080/car/insert?id=666
    //要求:如果方法有参数,调用时必须传入参数,否则500报错
    //要求:参数列表里分为参数类型(参考地址栏里的参数的类型666)
    // 参数名(参考地址栏里的参数名id)
    @RequestMapping("insert")
    public void insert(Integer id){
        System.out.println(id);
    }
    //http://localhost:8080/car/save?id=666&price=9.9
    @RequestMapping("save")
    public String save(int id,String name,double price){
        return id+name+price ;//给浏览器返回数据做展示
    }

//http://localhost:8080/car/find?id=666&name=BMW&price=9.9&color=red
    @RequestMapping("find")
//问题:想要解析很多请求参数太时,方法的参数列表太长...
//public String find(Integer id,String name,Double price,String color){
    public Car find(Car a){
        return a;//把a值的返回给浏览器
    }
}
3.2 总结

4 RestFul数据的解析 4.1 测试
package cn.tedu.mvc;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("student")
public class StudentController {
//get方式:http://localhost:8080/student/save?id=666&name=jack&age=20
    @RequestMapping("save")
    public String save(Integer id,String name,Integer age){
        return id+name+age;
    }

//restful方式:http://localhost:8080/student/save2/666/jack/20
    @RequestMapping("save2/{id}/{name}/{age}")
  //{id}用来获取地址栏中的数据,并交给id变量保存--专门用来获取restful方式提交的数据
    public String save2(@PathVariable Integer id,
  //@PathVariable用来绑定地址栏里声明的变量的值,并交给同名变量保存
                        @PathVariable String name,
                        @PathVariable Integer age){
        return id+name+age;
    }

}

5 练习 5.1 解析请求参数

制作一个前端HTML网页



	
		
		浏览器向 服务器发起请求
	
	
		练习1
		练习2
		练习3
	


SpringMVC框架解析请求参数

package cn.tedu.mvc;

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){
        return id+""+price+name ;
    }
    //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){
        return id+price+name ;
    }
}
5.2 Restful解析参数的练习

创造类

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+"" ;
    }
}

创建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 + ''' +
                '}';
    }
}


创建前端网页



	
		
		浏览器向 服务器发起请求
	
	
		练习1
		练习2
		练习3
		
		
		
	

6 SpringMVC框架解析post提交的数据 6.1 创建网页,提供表单



	
		
		测试 表单提交数据
		
		
	
	

		

学生信息管理系统MIS

姓名:
年龄:
性别:(单选框)
爱好:(多选) 乒乓球 爬山 唱歌
学历:(下拉框)
入学日期:
6.2 创建Maven Module

6.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);
    }
}
6.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;
    }

}
6.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.6 总结

7 扩展把数据入库 7.1 添加jdbc的jar包

修改pom.xml文件,添加jar包的坐标.

  1. 整个工程的pom.xml文件: 作用在每个Module里 --作用范围大
  2. Module的pom.xml文件: 只作用在当前的Module里 --作用范围小
找到dependencies标签,添加以下代码:
 
    mysql
    mysql-connector-java
    5.1.48


7.2 改造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;
    }
}

7.3 创建表
CREATE TABLE tb_student(
  id INT PRIMARY KEY AUTO_INCREMENT,
  USER VARCHAR(100),
  age INT,
  sex INT,
  hobby VARCHAR(200),
  edu INT,
  intime DATE
)
7.4 总结

8 Spring框架 8.1 概述

Spring框架可以和其他技术无缝衔接
BeanFactory: bean工厂, spring框架认为所有类都是bean. 从bean工厂可以获取每个bean
IOC: 控制反转, 不需要程序员来创建对象了,交给Spring框架来管理对象(从初始化…销毁).程序员可以直接从 Spring框架中获取Bean的对象
DI: 依赖注入,使用Spring框架明确两个对象间的依赖关系
AOP: 面向切面编程,是一种思想,解决了OOP的不足

8.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-输入文件名



    
    



创建测试类,直接获取对象

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);
    }
}

9 Git 9.1 概述

用来进行代码的版本控制:

  1. 远程仓库: 是一个网站,用来存你上传的代码,国内用Gitee码云,国外用GitHub
  2. 本地仓库: 是你自己创建的一个文件夹路径,用来存你即将上传的代码(参考E:workspacegitee)
  3. 上传资源
    add: 把即将上传的代码从工作空间到本地索引
    commit: 把即将上传的代码从本地索引到本地仓库
    push: 把即将上传的代码从本地仓库到远程仓库
  4. 下载资源
    clone/pull: 把代码从远程仓库下载到自己电脑里
  5. 以后工作中: 每天下班前,需上传资源. 每天上班时,下载资源.
  6. 使用Git的前提: 安装Git的软件, 在码云上注册账号
9.2 使用步骤
  1. 创建本地仓库: E:workspacegitee,用来存即将上传的代码
  2. 创建远程仓库: 存你上传的代码.去码云官网创建一个开源的仓库(设置仓库名字)
  3. 上传前,先保证本地仓库有东西能传别空着
  4. 正式上传:需要在本地仓库的位置执行Git命令

9.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注册时的账号和密码就行了

9.4 检查是否成功

9.5 查看上传的数据

打开Gitee网站,多刷新几次,就看到上传的内容了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号