Spring Boot

一、为什么使用SpringBoot

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

二、SpringBoot-HelloWorld初体验

我们创建一个jar类型的Maven项目

  • pom.xml
	<!-- 解决天生错误 -->
	<properties>
		<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
	</properties>

	<!-- springboot父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
	</parent>

	<!-- web模块starter -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
  • 主程序(启动项目时直接运行主程序)
//这个注解代表该项目是一个SpringBoot项目
@SpringBootApplication
public class MainApplication {
	public static void main(String[] args) {
	//Spring应用跑起来...
	SpringApplication.run(MainApplication.class, args);
	}
}
  • 具体方法(必须和主程序在同一级或子目录下,不然访问不到
//@RestController相当于@Controller+@ResponseBody
@Controller
public class Hello {
	
	@ResponseBody
	@RequestMapping("/hello")
	public String handle(){
	return "hello";
	}
}

三、SpringBoot-原理-简化依赖和配置

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.9.RELEASE</version>
</parent>
  • spring-boot-starter-xxx:场景启动器;
    • 1)、我们现在开发web程序就引用web场景spring-boot-starter-web;
      SpringBoot自定引入这个场景所需要的所有依赖;
    • 2)、所有支持的场合都在这里
      例如:
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
</dependencies>

四、SpringBoot-原理-简化部署

  • 引入springboot插件
<!-- 引入springboot插件;打包插件 -->
<build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
</build> 
  • 打包指令:package
    在这里插入图片描述
  • 运行项目指令:java -jar xxx.jar

五、STS中快速创建SpringBoot应用

在这里插入图片描述

  • next之后需要等待一小会(这里必须要有网络,会自动下载一些东西)
    在这里插入图片描述
  • 选择所需要的依赖
    在这里插入图片描述
  • 他会自动去下面的地址中下载所需要的依赖jar包
    在这里插入图片描述
  • 项目会存在天生错误,在pom.xml加入下面语句
<properties>
	<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
  • 自动生成的springboot项目中多了这三个东西

    • static:存放静态资源(例如图片)
    • template:存放HTML文件
    • application.properties:配置文件
      在这里插入图片描述
  • 如果想访问template下的HTML文件,还需要在pom.xml中加入thymeleaf

    • 视图解析器的默认perfix=“template/”,suffix=".html"(可在配置文件中修改)
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  • SpringBoot使用一个全局的配置文件,配置文件名是固定的;

    • application.properties:默认生成的配置文件
      • 内容格式:key=value
      •   server.port=8090
        
    • application.yml
  • YAML语法

    • YAML基本语法
      • 使用缩进表示层级关系
      • 缩进时不允许使用Tab键,只允许使用空格(我在开发工具使用tab并没有问题)。
      • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
        大小写敏感
      • 在冒号与值之间必须有一个空格
      server:
      	port: 8080
      
    • YAML 支持的三种数据结构
      • 对象:键值对的集合
      • 数组:一组按次序排列的值
      • 字面量:单个的、不可再分的值
    • 值的写法
      • 字面量:普通的值(数字,字符串,布尔)
        • k: v:字面直接来写;
        • 字符串默认不用加上单引号或者双引号;
        • “”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
        • name: “zhangsan \n lisi”:输出;zhangsan 换行 lisi
        • ‘’:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
        • name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
      • 对象、Map(属性和值)(键值对):
        • : v:在下一行来写对象的属性和值的关系;注意缩进
        • 对象还是k: v的方式
          friends:
          	lastName: zhangsan
          	age: 20
          
        • 行内写法:
          friends: {lastName: zhangsan,age: 18}
          
      • 数组(List、Set)
        • 用- 值表示数组中的一个元素
          pets:
            ‐ cat
            ‐ dog
            ‐ pig
          
        • 行内写法
          pets: [cat,dog,pig]
          

六、类注解(理解) 着重理解自动配置原理

  • 自动配置注解

    • @Target
      该注解只能放到指定的范围
    • @Retention(RetentionPolicy.RUNTIME)
      • SOURCE
        • 该注解只保留在源文件
      • CLASS
        • 注解被保留到class文件,但jvm加载class文件时候被遗弃
      • RUNTIME
        • 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
    • @Documented
      • 有该注解说明我们的注解在生成帮助文档的时候也会有
        -encoding UTF-8 -charset UTF-8
    • @Inherited
      • 如果子类继承父类 父类哪些注解 就会依次继承过去
    • @EnableAutoConfiguration
      • 开启SpringBoot的自动配置功能
  • 自动配置类

    • 与web相关的配置
      • WebMvcAutoConfiguration:视图解析器

      • MultipartAutoConfiguration:文件上传解析器

      • HttpEncodingAutoConfiguration:字符编码过滤器

      • DispatcherServletAutoConfiguration:springmvc核心控制器

  • 其他的注解

    • ConditionalOnClass
      • 某个class位于类路径上,才会实例化一个Bean

七、SpringBoot-整合mybatis-配置版

  • 1、需要的依赖
    • mysql
    • jdbc
    • mybatis
    • spring-web
  • pom.xml
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>2.1.1</version>
    </dependency>
    
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<scope>runtime</scope>
    </dependency>
    
  • 然后将相应的文件都创建好
    在这里插入图片描述
    在这里插入图片描述
  • application.yml
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://127.0.0.1:3306/student?useSSL=false&useUnicode=true&characterEncoding=UTF-8
        driver-class-name: com.mysql.jdbc.Driver
    mybatis:
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
  • 2、报错
    • 此时如果直接启动项目会报错说找不到Mapper,需要在SpringbootSsmApplication.java中加入注解@MapperScan,说明mapper的位置
      @MapperScan("com.baidu.springboot.mapper")
      @SpringBootApplication
      public class SpringbootSsmApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(SpringbootSsmApplication.class, args);
      	}
      
      }
      
    • 再次启动发现还会报错
      • Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
      • 此时我们修改driver,加cj的是升级后的新版本驱动
        application.yml
      spring:
        datasource:
          username: root
          password: 123456
          url: jdbc:mysql://127.0.0.1:3306/student?useSSL=false&useUnicode=true&characterEncoding=UTF-8
          driver-class-name: com.mysql.cj.jdbc.Driver
      mybatis:
        config-location: classpath:mybatis/mybatis-config.xml
        mapper-locations: classpath:mybatis/mapper/*.xml
      
    • 再次启动,此时不再报错,但当我们查询数据库的时候有开始报错
      • 错误信息:is unrecognized or represents more than one time zone
      • 时区问题:我们需要改为东八区
      • GMT+8,这里%2B是+的意思
      url: jdbc:mysql://127.0.0.1:3306/atcrowdfunding?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=UTF-8    
      
  • 3、开启事务
    • SpringbootSsmApplication中
      //开启事务
      @EnableTransactionManagement
      //扫描mapper
      @MapperScan("com.baidu.springboot.mapper")
      @SpringBootApplication
      public class SpringbootSsmApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(SpringbootSsmApplication.class, args);
      	}
      
      }
      
      //为方法加上事务,可选择性的设置事务中的属性
      @Transactional(isolation=Isolation.DEFAULT)
      @Override
      public TAdmin getTAdminById(Integer id) {
      	return adminMapper.selectByPrimaryKey(id);
      }
      
  • 4、使用注解代替mapper.xml中的SQL
    • @Slect
    • @Insert
    • @Update
    • @delete
    public interface TAdminMapper {
    	@Select("select * from t_admin where id=#{id}")
    	TAdmin selectByPrimaryKey(Integer id);
    }
    
  • 5、将数据源改为druid
    • 默认的数据源是:HikariDataSource( 性能高一些) ,但druid稳定性强
    • 加入依赖
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>druid</artifactId>
    	<version>1.1.18</version>
    </dependency>
    
    • 选择druid,修改application.yml,加入
      • type: com.alibaba.druid.pool.DruidDataSource
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://127.0.0.1:3306/atcrowdfunding?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=UTF-8
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
    mybatis:
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值