zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记

    SpringBoot

    Author:Yu

    Version:2.2.11

    一.SpringBoot介绍

    1.1引言

    • 为了使用SSM框架去开发,准备SSM框架的模板配置
    • 为了Spring整合第三方框架,单独的去编写xml文件。
    • 导致SSM项目后期xml文件特别多,维护xml文件是很高的。
    • SSM工程部署也是很麻烦,依赖第三方的容器。
    • SSM开发方式很笨重。

    1.2SpringBoot介绍

    SpringBoot是由Pivot团队研发的,SpringBoot并不是一门新技术,只是将之前常用的spring,SpringMVC,data-jpa等常用框架封装到了一起,帮助你隐藏的整合细节,实现快速开发。

    SpringBoot就是一个工具。

    SpringBoot特点:

    • SpringBoot项目不需要模板化的配置。
    • SpringBoot整合第三方框架时,只需要导入相应的start依赖包,就自动整合了。
    • SpringBoot只有一个默认的.properties的配置文件,不推荐使用xml,后期会采用.java的文件去编写配置信息。
    • SpringBoot工程在部署时,就采用jar包的方式,内部自动依赖Tomcat容器,提供多环境的配置。
    • 学习微服务SpringCloud要建立在SpringBoot的基础上。

    二.SpringBoot快速入门

    2.1快速构建SpringBoot

    1.选择项目的类型

    2.项目的描述

    3.指定SpringBoot项目的版本和依赖

    4.第一次创建SpringBoot工程,下载大量依赖,保证maven已经配置好了阿里云的私服

    修改pom.xml文件中的依赖

    <!-- 导入下面的依赖 -->
    <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

    5.编写Controller类

    @RestController
    public class TestController {
        @GetMapping("/test")
        public String test(){
            return "Hello SpringBoot!";
        }
    }
    

    6.启动Spring Boot工程,运行主线程的main方法

    得到 Hello SpringBoot! 界面

    2.2 Spring Boot的目录结构

    1.pom.文件

    ​ 1.指定了一个父工程:指定当前项目为Spring Boot,帮助我们声明starter依赖的版本。

    ​ 2.项目的元数据:包名项目号,版本号。

    ​ 3.指定properties信息:指定Java版本为1.8

    ​ 4.导入依赖:默认情况导入spring -boot-starter,spring-boot-starter-test

    ​ 5.插件:spring-boot-maven-plugin


    .gitignore文件:默认帮我们忽略了一些文件和目录


    3.src目录

    -src
    	-main
    		-java
    			-包名
    				启动类.java    #需要controller类,放在启动类的子包中或者同级别包下
    		-resources
        		-static          #存放静态资源的
        		-templates		#存放模板页面的
        		application.properties #提供的唯一配置文件
    		
    	-test   #只是为了测试用的
    

    2.3 SpringBoot 三种启动方式

    1,运行启动类的main方法即可运行Spring Boot工程


    2.采用maven的命令去运行Spring Boot工程

    mvn spring-boot:run


    3.采用jar包的方式运行

    • 将当前项目打包成一个jar文件
    • 通过java-jar jar文件

    三.SpringBoot常用注解

    3.1@Configuration和@Bean

    之前使用SSM去开发时,在xml文件中编写bean标签。

    但是Spring Boot不推荐使用xml文件。

    @Configuration注解相当于bean标签

    @Bean注解相当于bean标签

    id=“方法名|注解中的name属性(优先级更高)“

    class=“方法返回的结果”

    @Configuration   //代表当前是配置类
    
    public class UserConfig {
    
    
        @Bean(name = "user1")       //构建一个实例 ,放到spring容器中
            public User user(){
            User user = new User();
            user.setId(1);
            user.setName("张三");
            return user;
    
            /*
            user 就是id  User就是class
            * <beans...>
                        <bean id="user1" class="  com.yu.firstspringboot.entity.User;"/>
               </beans>
               * */
        }
     }   
    

    3.2@SpringBootApplication

    @SpringBootApplication就是一个组合注解


    1.@SpringBootConfiguration就是@Configuration注解,代表启动类就是一个配置类。


    2.@EnableAutoConfiguration帮你实现自动装配的,SpringBoot工程启动时,运行一个SpringFactoriesLoader中的load方法,以for循环的方式,一个一个加载。

    好处:无需编写大量的整合配置信息,只需要安卓SpringBoot提供好了的约定方式去整合即可。

    坏处:如果你导入了一个start依赖,那么你需要他必要的配置信息。

    手动关闭自动装配指定内容:@SpringBootApplication(exclude = QuartzAutoConfiguration.class)


    3.@ComponentScan就相当于<context:component-scan basePackage="包名"/>帮助注解扫描的。

    四.SpringBoot常用配置

    4.1SpringBoot的配置文件格式

    SpringBoot的配置文件支持properties和yml,甚至他还指出json

    更推荐使用yml文件格式:

    ​ 1.yml文件,会根据换行和缩进帮助咱们管理配置文件所在位置。

    ​ 2.yml文件,相比properties更轻量级一些

    yml文件的劣势:

    ​ 1.严格遵循换行和缩进。

    ​ 2.在填写value时,一定要在:后面空格

    4.2多环境配置

    在application.yml文件中添加一个配置选项:

    spring:
    profiles:
     active: 环境名
    

    在resource目录下,创建多个application-环境名.yml 文件即可


    在部署工程时,通过Java -jar jar文件 --spring.profiles.active=环境

    4.3引入外部配置文件信息

    和传统的SSM方法一样,通过@Value的注解去获取properties/yml文件的内容。

    如果yml文件中需要编写大量的自定义配置,并且具有统一的前缀时,采用如下方式


    @ConfigurationProperties(prefix = "aliyun")
    @Component
    @Data
    public class AliyunProperties {
        private String xxxx;
        
        ...
    
    }
    
    aliyun:
      xxxx: xxxxxxxxxx
      ...    
    

    4.4热加载

    1.导入依赖

    <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-devtools</artifactId>
    			<optional>true</optional>
    </dependency>
    

    2.修改settings中的配置

    3.修改内容后,可以通过build重新构建工程

    五.SpringBoot整合Mybatis

    5.1xml方式整合Mybatis

    1.导入依赖

    	<!--mysql驱动-->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    		<!--druid连接-->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid-spring-boot-starter</artifactId>
    			<version>1.1.10</version>
    		</dependency>
    		<!--mybatis-->
    		<dependency>
    			<groupId>org.mybatis.spring.boot</groupId>
    			<artifactId>mybatis-spring-boot-starter</artifactId>
    			<version>1.3.2</version>
    		</dependency>
    

    2.编写配置文件。

    准备实体类
    @Data
    public class Air implements Serializable {
        private Integer id;
        private Integer districtId;
        private java.util.Date monitorTime;
        private Integer pm10;
        private Integer pm25;
        private String monitoringStation;
        private java.util.Date  lastModifyTime;
    }
    
    //==========================
    @Data
    public class District implements Serializable {
        private Integer id;
        private String name;
    }
    

    2.2//准备Mapper接口
    public interface AirMapper {
        List<Air>    findAll();
    }
    //在启动类添加扫描Mapper接口所在的包
    @MapperScan(basePackages = "com.yu.firstspringboot.mapper")
    
    

    <!--2.3准备映射文件-->
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.yu.firstspringboot.mapper.AirMapper">
        <!--    LIst<Air> findAll();    -->
        <select id="findAll" resultType="Air">
            select *from air
        </select>
    </mapper>
    
    <!--添加yml文件配置信息 -->
    #mybatis配置
    mybatis:
    扫描映射文件
      mapper-locations: classpath:mapper/*.xml
      配置别名扫描的包
      type-aliases-package: com.yu.firstspringboot.entity
      configuration:
      开启驼峰映射配置
        map-underscore-to-camel-case: true
    

    #2.3指定连接数据库的信息
    #连接数据库信息
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql:///air?serverTimezone=UTC
        username:  root
        password:  123456
        type: com.alibaba.druid.pool.DruidDataSource
    

    3.测试。

    在Mapper接口的位置,直接右键转到测试类,在test目录下。

    让当前测试类,继承FirstSpringbootApplicationTests测试类(在class前,添加public).

    class AirMapperTest extends FirstSpringbootApplicationTests {
    
        @Autowired
        private AirMapper airMapper;
        @Test
        void findAll() {
            List<Air> list = airMapper.findAll();
            for (Air air : list) {
                System.out.println(air);
            }
        }
    
    

    5.2注解方式 整合Mybatis

    1.创建District和Mapper接口

    public interface DistrictMapper {
        List<District> findAll();
    }
    

    2.添加Mybatis注解

    针对增删改查:@Insert,@Delete,@Update,@Select

    需要在启动类添加@MapperScan注解

    @Select("select *from district")
    List<District> findAll();
    
    @Select("select *from district where id = #{id}")
    District findByOneById(@Param("id") Integer id);
    

    3.测试,看到执行的sql语句

    logging:
      level:
        com.yu.firstspringboot.mapper: DEBUG
    

    class DistrictMapperTest extends FirstSpringbootApplicationTests {
    
        @Autowired
        private DistrictMapper mapper;
        @Test
        void findAll() {
            List<District> list = mapper.findAll();
            for (District district : list) {
                System.out.println(district);
            }
        }
    
        @Test
        void findByOneById() {
            District district = mapper.findByOneById(5);
            System.out.println(district);
        }
    }
    

    5.3SpringBoot 整合分页助手

    1.导入依赖

    <!--PageHelper依赖-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.5</version>
    </dependency>
    

    class AirMapperTest extends FirstSpringbootApplicationTests {
    
        @Autowired
        private AirMapper airMapper;
        @Test
        void findAll() {
            List<Air> list = airMapper.findAll();
            for (Air air : list) {
                System.out.println(air);
            }
        }
        @Test
        void findByPage() {
            //1.执行分页
            PageHelper.startPage(1, 5);
            //2.执行查询
            List<Air> list = airMapper.findAll();
            //3.执行PageInfo封装
            PageInfo<Air> pageInfo = new PageInfo<>(list);
            //4.输出
            for (Air air : pageInfo.getList()) {
                System.out.println(air);
            }
        }
    }
    

    六.SpringBoot整合JSP

    1.导入依赖

    <!--JSP核心引擎依赖-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
    <!--JSTL-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    

    2.创建webapp以及WEB-INF去存放JSP页面

    3.指定view前缀和后缀

    public class JspController {
        @GetMapping("/index")
        public String index(Model model){
            model.addAttribute("name","张三");
            return "index";
        }
    }
    

    spring:
      mvc:
          view:
            prefix: /WEB-INF
            suffix: .jsp
    
  • 相关阅读:
    Problem : [NOIP2015普及组]扫雷游戏
    Problem : [NOIP2014普及组]珠心算测验
    Problem : [Noip2005]谁拿了最多奖学金
    Problem : [Noip2008]火柴棒等式
    Problem : [Noip2010普及组]导弹拦截
    Problem : [NOIP2013普及组]计数问题
    Problem : [Noip2003]麦森数
    Problem: [Noip2008] 笨小猴
    Problem : 暴力摩托
    Problem : [Usaco2017 Dec]Blocked Billboard
  • 原文地址:https://www.cnblogs.com/zzy8080/p/13912345.html
Copyright © 2011-2022 走看看