SpringBoot

目录

一、SpringBoot的介绍

二、SpringBoot的入门 

三、SpringBoot的配置文件 

四、SpringBoot集成第三方框架 

五、SpringBoot版本控制的原理


一、SpringBoot的介绍

1、什么是SpringBoot?

        SpringBoot是一个基于Spring的一个局昂家。提供了一些自动配置的依赖包,自动嵌入了servlet的容器,简化了我们开发的配置,提高了快发人员的开发效率,并解决了包依赖的问题

2、SpringBoot的特性以及好处。

  • 创建独立的Spring应用程序
  • 嵌入了Tomacat、Jetty、Undertow(无须部署WAR文件)
  • 提供“入门”依赖项(起步依赖),以简化构建配置
  • 自动配置Spring和第三方库
  • 提供可用于生产的功能。例如指标,运行状况和外部化配置
  • 完全没有戴,啊生成,也不需要XML配置

二、SpringBoot的入门 

  1. 创建Maven工程。
  2. 配置pom.xml文件。
    • 添加parent
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.1.4.RELEASE</version>
      </parent>

    • 添加起步依赖
      <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>
  3. 创建启动类。
    @SpringBootApplication
    public class SpringbootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootApplication.class,args);
        }
    }

三、SpringBoot的配置文件 

一共两种配置文件的方式,application.properties或application.yml(application.yaml)自定义配置,两种文件的效果等价。SpringBoot默认从Resource目录加载自定义配置文件。如果同时存在两种配置文件,properties的优先级要高于yml。笔者习惯于yml文件。

 1、yml文件的基础语法

  •  大小写敏感
  • 数据值前必须有空格作为分隔符
  • #表示注释 
    # 基本格式 key:value
    name: zhangsan
    # 数组 - 用于区分
    city:
     - beijing
     - shagnhai
     - guangzhou
     - shenzhen
    # 集合中的元素是对象
    student:
        - name: zhagnsan
          age: 18
          sex: 1
        - naem: lisi
          age: 25
          sex: 2
    # map集合的形式
     maps: {"name":"zhansan","age":"18"}
    # 参数引用
    person:
        name: ${name} #可以取到上边的name定义的值

2、获取yml配置文件中的值三种方式

  •  @value注解的方式
    @RestController
    public class XXX{
        //获取简单类型
        @Value("${name}")
        private String name;//此时yml文件中的name的值就注入到了name变量中
        //获取数组的元素
        @Value("${city[0]}")
        private String city0;
        //获取集合中的是对象的元素
        @Value("${students[0].name}")
        private String studentname;
        
    }
  • Environment的方式
    @RestController
    public class XXX{
        //1、注入Environment对象
        @Autowired
        private Environment environment;
        //2、通过environment获取
        String value = environment.getProperty("name");
        System.out.println("environement方式获取值:"+value);
        
    }
    
  • @ConfigurationProperties,首先要建立pojo和yml中要获取的数据的映射关系,然后jiangpojo交给spring容器,最后注入。
    //pojo加上的注解
    @Component
    @ConfigurationProperties(prefix = "xxx")//xxx为yml文件中要映射的数据
    
    //注入
    @Autowired
    private Xxx xxx;
    //此时可以直接使用
    xxx.get属性()

3、profile

 用于切换环境

application.yml:

#通过active指定选用配置环境
spring:
  profiles:
    active: pro

application-dev.yml:

#开发环境
server:
  port: 8081

application-test.yml:

#测试环境
server:
  port: 8082

applicatioin-pro.yml

#生产环境
server:
  port: 8083

四、SpringBoot集成第三方框架 

  1. 整合MyBatis
    1. 添加依赖
      <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.1.4.RELEASE</version>
      </parent>
      <dependencies>
              <!--驱动-->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <scope>runtime</scope>
              </dependency>
              <!--mybatis的 起步依赖-->
              <dependency>
                  <groupId>org.mybatis.spring.boot</groupId>
                  <artifactId>mybatis-spring-boot-starter</artifactId>
                  <version>2.0.1</version>
              </dependency>
              <!--spring web起步依赖-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      
          </dependencies>
    2. 创建pojo
    3. 创建mapper接口
    4. 创建映射文件
    5. 配置yml指定映射文件的位置
      #配置数据源
      spring:
        datasource:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
          username: root
          password: 123456
      #配置mapper的映射文件的位置
      mybatis:
        mapper-locations: classpath:mappers/*Mapper.xml
    6. 创建启动类、加入接口注解扫描
      @SpringBootApplication
      @MapperScan(basePackages = "xxx.xxx.dao")
      //MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spring容器
      public class MybatisApplication {
          public static void main(String[] args) {
              SpringApplication.run(MybatisApplication.class,args);
          }
      }
    7. 测试
  2. 整合redis
    1.  准备好redis服务器并启动
    2. 添加起步依赖
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
    3. 配置yml,配置redis的服务器的地址
      spring:
        datasource:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
          username: root
          password: 123456
        redis:
          host: localhost
          port: 6379
      
    4. 在Service中的方法中使用 
      1. 注入redisTemplate
      2. 调用
            @Autowired
            private RedisTemplate redisTemplate;
            public Xxx xxx(){
                //获取值    
                redisTemplate.boundValueOps("xxx").get();
                //设置值
                redisTemplate.boundValueOps("xxx").set(allUser);
            }
      3. 解决redis字符乱码问题
        //在启动类当中配置RedisTemplate序列化规则
        @Bean
            public RedisTemplate<Object, Object> redisTemplate(
                RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
                RedisTemplate<Object, Object> template = new RedisTemplate<>();
                template.setConnectionFactory(redisConnectionFactory);
                //设置key的值为字符串序列化方式 那么在使用过程中key 一定只能是字符串
                template.setKeySerializer(new StringRedisSerializer());
                //设置value的序列化机制为JDK自带的方式
                template.setValueSerializer(new JdkSerializationRedisSerializer());
                return template;

五、SpringBoot版本控制的原理

 我们创建的SpringBoot工程继承了Spring-boot-starter-parent

 Spring-boot-starter-parent继承了spring-boot-dependencies

 spring-boot-dependencies里面已经做好了版本管理

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值