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

初学--SpringBoot

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

初学--SpringBoot

SpirngBoot 1.SpingBoot简介

优点:

  • 简化开发配置
  • 开发速度快

特点

  • 自动装配,默认配置

  • 起步依赖(Start一站式)直接引入就可以了

  • 创建独立的Spring应用程序(无序打包,内嵌Tomcat)

  • 监控能力强

  • 少的XML配置

核心

  • IOC和AOP
  • 精简高效
  • 基于SpringBoot有了Spring Cloud
2.SpringBoot的版本选择

尽量选择稳定版,选择合适的版本,跟其他组件兼容性高的。

  • CURRENT:最新版本

  • GA:稳定版

  • SNAPOSHOT:快照版,随时更新

项目升级,需要参考版本升级文档,最新的不一定是最好的。

3.新建Spring Boot项目

两种方式

(1)SpringBoot官网进行生成(https://start.spring.io/)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nkWrsY8k-1632977668310)(C:UsersAdministratorAppDataRoamingTyporatypora-user-imagesimage-20210930095742145.png)]

(2)IDEA创建SpringBoot项目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jj0FjeI9-1632977668317)(C:UsersAdministratorAppDataRoamingTyporatypora-user-imagesimage-20210930100026387.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PvGc9OZS-1632977668319)(C:UsersAdministratorAppDataRoamingTyporatypora-user-imagesimage-20210930100434350.png)]

注意创建好工程后

  • 检查版本,选用合适项目开发的版本,避免版本不统一造成的失误
  • 标签是必要的,表示父启动
  • 必要的依赖
  • 必要的插件(项目运行和打包有用)

    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

4.案例演示

开发一个SpingBoot接口

  • 遵循MVC三层架构的开发模式
  • 新建一个controller接口
package com.imooc.springboot.controller;

import org.springframework.web.bind.annotation.*;


@RestController
public class fistSpringBoot {
    @GetMapping("/f")
    public String firstSpringBoot(){
        return "第一个SpringBoot的接口";
    }

    
    @GetMapping("/g")
public String get(@RequestParam String id){
    return "收到参数"+id;
}

    
    @PostMapping("/p")
    public String post(@RequestBody String id){
        return"收到参数"+id;
}

}

  • 进入主启动类进行项目启动
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

注意:

  • 此类是项目自动生成的,不需要编写
  • 必须在与启动类同一级目录下进行项目开发,否则无法运行
  • POST请求可以用postman工具在bady中传递参数
  • POST中参数传递最好用@RequestBody ,比较符合规范。
5.配置文件 1.SpringBoot有两种配置文件
  • properties

application.properties

#使用的端口号
server.port=8081
#应用的名称
spring.application.name=first-spring-boot
#配置统一前缀
server.servlet.context-path=/first
  • yml:分层级,冒号后面需要空格!空格都需要注意!

application.yml

server:
    port: 8082
    servlet:
        context-path: /first
spring:
    application:
        name: first-spring-boot

建议使用第一种,yml格式不容易保持。

2.自定义配置
package com.imooc.springboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController{
    @Value("${shool.sname}")
    String name;
    @Value("${shool.place}")
    String place;

@GetMapping("/s")
    public String shcool(){
        return "学校名叫"+name+",地点是在"+place;
    }
}

application.properties

#自定义配置
shool.sname=家里蹲大学
shool.place=成都
  • 静态变量的注入
..........
static String time;
 ...........
@GetMapping("/static")
public  String school(){
return "静态变量"+time;
}
@Value("${time}")
public void setTime(String time) {
    SampleController.time = time;
}

注意:要通过set方法进行设置参数,并在配置文件中书写对应的数据。

6.Servic和Dao编写(学生信息查询案例) 1.引入依赖
                    org.mybatis.spring.boot            mybatis-spring-boot-starter            2.1.1                            mysql            mysql-connector-java        
2.数据库的配置
  • 数据库的数据准备

  • application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&Unicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123456
3.MVC模式开发
  • 创建实体类
package com.imooc.springboot.entity;public class Strudent {    private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Strudent{" +                "id=" + id +                ", name='" + name + ''' +                '}';    }}
1.模型层Model
  • 创建Mapper接口
package com.imooc.springboot.mapper;import com.imooc.springboot.entity.Strudent;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import javax.annotation.Resource;@Mapper@Resourcepublic interface FirstMapper {    @Select("select*from student where id = #{id}")    Strudent findById(long id );}

@Resource:数据资源,能够进行管理

@Select:用来书写SQL语句

  • Service层
package com.imooc.springboot.service;import com.imooc.springboot.entity.Strudent;import com.imooc.springboot.mapper.FirstMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class FirstService {@Autowired    FirstMapper firstMapper;    public Strudent getStu(Integer id){        return firstMapper.findById(id);    }}

@Autowired:自动进行装配,实现对象实例化

2.控制层Controller
package com.imooc.springboot.controller;import com.imooc.springboot.service.FirstService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class FirstController {    @Autowired    FirstService firstService;    @GetMapping("/student")public String firstStudent(@RequestParam Integer id){        return firstService.getStu(id).toString();    }}
3.视图层View

输入对应URL进行参数传递

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

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

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