springBoot使用

1.什么是SpringBoot

  • SpringBoot是由Pivotal团队提供的一套开源框架,可以简化spring应用的创建及部署。
  • SpringBoot是伴随Spring4.0的时候发布的一个框架。
  • SpringBoot用来简化Spring应用的开发,约定大于配置,去繁从简。

从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。它使用“约定优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来。使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器)、准生产级别的基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很少的Spring配置。

框架特点 

  • 创建独立的spring应用。
  • 嵌入Tomcat、Jetty,Undertow而且不需要部署他们。
  • 提供的“starters"来简化Maven配置。
  • 尽可能自动配置spring应用,绝对没有代码生成和XML配置需求。
  • 提供生产指标,健壮检查和外部化配置。

优缺点 

优点

  • 快速创建独立运行的Spring项目以及主流框架集成
  • 使用嵌入式Servlet容器,应用无需打包成war包
  • starters自动依赖于版本控制
  • 大量的自动配置,简化开发,也可修改默认值
  • 无需配置xml,没有代码生成,开箱即用
  • 准生产化环境的运行时应用监控

缺点

  • SpringBoot是spring的一个再封装,如果不了解spring,那么学Springboot很费劲。
  • 入门易,精通难,它没有增强spring的功能,只是帮助我们做了很多本需要我们自己做的配置整合工作。 

微服务

将子系统拆成一个一个的jar包运行就是微服务。Spring Boot算是微服务开发的入门级框架。

 微服务架构示例图

  • 单体应用:ALL IN ONE,以前的架构风格,所有的东西都写在一个应用里面。
  • 微服务:一个应用是一组小型服务。每一个服务通过http的方式进行互通,每个功能元素最终都是一个可独立替换和独立升级的软件单元。 

 能用单体应用开发的应用,尽量不用微服务。

  • 比如个人开发项目单体应用足够了,微服务成本比较大,一般企业级应用用的比较多。
  • 微服务就是一种架构风格,掌握还是必要的。

2.简单案例

创建Springboot项目

 选择需要的依赖:配置依赖有详细说明,因为还要一些其他的依赖,这里不配置也行,直接在pom.xml配置

配置依赖 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xygalaxy</groupId>
    <artifactId>SpringBoot-Study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot-Study</name>
    <description>SpringBoot-Study</description>
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <!-- Spring Web依赖,主要提供SpringMVC相关 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- springboot依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- springboot测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <!-- MybatisPlus依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.5</version>
        </dependency>
        <!-- 连接数据库依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <!--  lombok简化实体  -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

MVC分层

创建实体类

@Data
@TableName("student")
public class StudentPO {

    @TableId(value = "id",type = IdType.ASSIGN_ID)
    private Long id;

    @TableField(value = "name")
    private String name;

    @TableField(value = "age")
    private Integer age;

    @TableField(value = "email")
    private String email;

    @TableField(value = "sex")
    private String sex;

    @TableField(value = "version")
    @Version
    private Integer version;

    @TableField("is_delete")
    @TableLogic(value = "0",delval = "1")
    private Integer isDelete = 0;
}

创建Mapper接口

@Mapper
public interface StudentMapper extends BaseMapper<StudentPO> {

}

创建Service接口和实现类

接口

public interface IStudentService extends IService<StudentPO> {

}

实现类

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentPO> implements IStudentService {

}

创建Controller

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    @RequestMapping("/list")
    public List<StudentPO> userList(){
        return studentService.list();
    }

}

启动类配置

@SpringBootApplication
@MapperScan("com.test.mapper")
public class SpringBootStudyApplication {

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

}

application.yml配置

主要配置数据库和开启MyBatis-Plus的日志功能

# 配置访问端口
server:
  port: 8080

# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8
    username: root
    password: 123456

# 开启日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      table-prefix: test.

测试

启动类运行,浏览器访问

http://localhost:8080/student/list
// 或者
http://127.0.0.1:8080/student/list

3.配置文件

全局配置文件

SpringBoot的有两种格式的全局配置文件,使用任何一个功能都是一样的,配置文件名是固定的(放在resources目录下)。

  • application.properties:默认Spring initializr默认自动生成的配置文件,也是我们属性的文件格式。
  • application.yml:除了properties文件可以做为SpringBoot的配置文件以外,SpringBoot还支持yaml配置文件。

 

配置文件的作用

修改SpringBoot项目配置的默认值。

  • SpringBoot在底层都给我们自动配置好了默认值,如果想改就需要使用配置文件修改配置即可。
  • 约定大于配置的体现就是SpringBoot已经帮我们配置了所需的配置默认值,只是我们不想用默认值可以通过配置文件修改。

properties配置语法 

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

YAML语法

  • YAML(又称为 “Yet Another Markup Language”)是一种可读的数据序列化格式。它的设计目标是简洁、易读、易于编写,并可以被各种编程语言解析和生成。
  • YAML不是标记语言,它的主要拥堵是作为配置文件和数据传输的格式。
  • YAML的文件通常使用“.yaml"或".yml"的文件扩展名来标识文件的格式。这两种扩展名没有实质的差别,只是简单的表示文件使用YAML格式进行编写。

 语法格式

  • 基本格式: key: value 表示一对键值对,注意:键值对中的值前面必须有空格,多少个无所谓,但必须有。
  • 靠键左对齐来区分层级关系,也就是说凡是左对齐的键值对都是一个层次的

  • 大小写敏感,严格区分大小写
  • #进行注释

值写法 

  • 字符串默认不用引号引起来。
  • 对象或Map的表示方法有两种,一种是换行+缩进,另一种是利用大括号

  • 数组或List集合也有两种表达方式

YAML案例 

等价于properties配置文件配置数据库

# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8
    username: root
    password: 123456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值