springBoot入门
一、springBoot的介绍
已经学过的ssm+quartz+shiro+freemarker+log4j+datasource…
1. 什么是springboot
他没有实现具体的任何功能,但是他帮我们预先编写好配置类和对应的jar
我们只修改少量的配置,调用springboot的启动方法就可以开启项目。
注意:
springboot的两个热重要原则
- 约定大于配置:避免了大量的配置修改,很多默认配置就可以满足要求
- 开箱即用:导入对应的启动器,直接可以使用
spring boot is make it easy to stand-alone,producttion-grand spring based on
aplications that youcan only just run web take it opinionated view of the spring
platform and thrd-part libraries so you started with minmum fus. most spring boot aplications need very little spring configuration
2. springboot的作用
快速搭建和启动项目
3. springboot的组成
- spring boot 的父项目(pom文件 制定好了版本和一些基础的配置信息)
- springboot 的启动器配置(pom文件)(web-starter、jdbc-starter\mybatis-starter\freemarker-starter…)
包含jar,配置类、以及配置类清单(meta-inf/spring.factoryies)
4. springboot的原理
继承springboot的父pom得到基本配置以及版本控制,通过以来启动器得到对应的启动器的配置文件
以及文件清单。spring boot只需要启动类就可以启动启动器的配置文件和依赖,最后把项目需要的
jar和配置文件以及bean进行初始化
5. springboot、spring、springmvc的关系
spring boot包含,就是依赖spring、springmvc的依赖以及配置
二、springBoot的快速使用
方案一:
- 打开idea
- 创建spring init 项目
- 选择maven对应的信息jav
- 选中spring-web 启动器
- 修改项目结构
src
main---------------->项目的主题代码
java--------->自定义Java代码和springboot的启动类
resources—>配置的位置
static—>静态资源
templates---->模板 ftl
application.properties|yaml-------------springboot 的唯一配置文件
test----------测试代码
java
……
pom
父工程pom------------------------夫工程(配置和打包相关)----------dependencies夫工程(依赖版本)
自己的属性和自己的配置
spring boot的启动和启动器依赖
三、springBoot 的核心内容
1. 核心注解
@SpringbootApplication----------->启动类
- ComponentScan—扫描当前项目添加注解的bean
注意:要求bean的位置一定要在配置类的同包或者子包下
- @EnableAutoConfiguration—主动加载其他起动器的配置类信息
扫描所有classpath:/META-INF/spring.factoryies配置文件
- @SpringbootCongiguration====@Configuration配置类
2. 核心配置文件
- 配置的命名和位置
命名:application.properties|taml
properties: key=value yaml🔑value
- 配置的作用
- 修改一些默认的属性例如端口号,项目根路径、数据库连接池…… 固定的key=值
- 申明一些自定义的属性 key可以随意命名,但不能是固有的key
- properties配置文件
例如修改端口号
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#web-properties
- yaml配置文件
- 利用缩进配控制层级关系
利用缩进控制层级关系,能简化代码
例如:
server:
|空格||空格| port:|空格|9090
servlet:
context-path: /root
key:
key:|空格|值
引用其他属性值
key:||${其他的key}
随机生成
${random.uuid}random内置关键字,可以随机生成字符串和数字
3. 核心启动器
springboot就是启动器,每个启动器负责一个方向的jar报的依赖和配置文件
如果项目需要多个启动器,则需要引多个启动器即可
四、springboot 整合mybatis
1. 使用mapper.xml文件的形式(推荐使用因为配置类无法做到resultmap)
步骤一:导入相关启动器
mybatis-starter
durid-starter
springboot-starter
mysql
junit
步骤二:创建xxxMapper接口和xxxMapper.xml 注意:不推荐写在一起
mapper.xml-----------------resources/static/xx/xxxMapper.xml
步骤三:配置文件
数据库相关配置
spring:
datasource
username:
password:
url:
driver-class-name
mybatis相关配置
扫描mapper.xml\别名、缓存、驼峰命名、二级缓存、延迟加载……
mybatis
mapper-locations: static/*.xml
type-alises-package 别名包
……
步骤四:启动类扫描mapper接口
@MapperScan(basepackge=“con.it.mapper”)
步骤五:搭建springboot的测试
@RunWith(spring4junitrunner.class)
@springboottest(classes=启动类.class)
class 测试类{ @Autowired
private Mapper mapper;@test public void test1(){ mapper.xxx(); } }
使用mybatisannotation的形式
不需要编写mapper.xml文件 去掉 mapper-locations: static/*.xml配置
mybatis分页插件启动器
com.github.pagehelper pagehelper-spring-boot-starter 1.2.3和ssm里面的使用一样
逆向工程和以前一样