※ 快速入门Spring boot必看
一.引导概述
(一)传统搭建项目的方式–SSM或者SSH
在以前的项目开发中,通常会选用Spring+Struts2+Hibernate或者Spring+Spring MVC+Mybatis作为项目的框架,但是会发现,在搭建项目的时候,jar包问题,配置文件编写的问题往往成为我们开发中很大的绊脚石,开发不顺畅。
(二)流行搭建项目的方式–Spring boot全家桶
有没有一种框架可以让我们写最少的配置文件,写最少的jar包依赖,一分钟搭好项目,马上拿来就可以开发项目呢。答案是有的,Spring boot就是用于简化Spring应用配置过程(包括配置文件,jar包依赖等),让我们快速上手项目。
(三)学习前提知识
- 最好之前已经接触过SSM开发,不熟练也没关系
- 基本的Web开发能力
(五)个人电脑环境
- 安装好jdk:1.7以上
- 开发工具:IDE(推荐)或者Eclipse
- Maven环境
二.必备知识概述
(一)什么是Spring boot
简单来说,Spring boot就是帮我们简化Spring应用配置过程的一站式解决框架。特点是尽可能去配置化,采用注解化;通过starter自动导入依赖。
(二)Spring boot可以帮我们做什么
- 简化配置
- 自动通过starter导入依赖
- 整合第三方框架(接下来会围绕这点讲)
(三)spring boot常用的配置项(可先不懂)
server.port=80
debug=false
#/ a , b ,c 三个项目 /地址将产生冲突
#一般情况下,小项目通常都是在Tomcat下部署多个webapp,通过上下文来区分
#在集群或者中大型项目中,通常我们一个Tomcat对应一个webapp,然后通过不同的端口来进行区分(8080/8081/8082)
server.servlet.context-path=/myspringboot
#UTF-8只包含了20000+个中文字符,对于生僻字显示不了
spring.http.encoding.charset=UTF-8
#开启thymeleaf缓存,Thymeleaf热部署
#注意这里有大坑,除了设置cache=false外,还要设置
#file->setting->build->complier-> Build Project Auto... 勾选
spring.thymeleaf.cache=false
#Spring MVC中对输入参数的格式化
spring.mvc.date-format=yyyy-MM-dd
#设置JSON 日期序列化输出格式,北京时间相对伦敦有8个小时时差所以使用GMT+8
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss SSS
#日志输出的地址,SpringBoot默认并没有进行文件输出,只在控制台中进行了打印
logging.file=d:/logs/msb.log
#日志级别 debug->info->warn->error
#默认情况下SpringBoot日志级别为info
#如果设置了debug=true的时候,会自动日志会自动降到debug
#ROOT代表默认全局设置
#logging.level.ROOT=INFO
#设置指定包的输出级别
#logging.level.org.springframework=ERROR
#logging.level.org.apache=ERROR
(四)spring boot配置的环境切换(可先不懂)
applicationContext.xml
spring:
profiles:
active: dev
http:
encoding:
charset: utf-8
applicationContext-dev.xml
#debug=true
debug: false
#server.port=80
#前缀相同的配置项,idea会自动进行归纳
server:
port: 8000
servlet:
context-path: /
thymeleaf:
cache: false
spring:
mvc:
date-format: yyyy-MM-dd
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
(五)自定义配置文件(可先不懂)
1.方法一
在resource下自定义config.properties;注意对中文的支持不是很好,所以要先设置:Editor-》FileEncodding-》Transiparent
app.name=\u6D0B\u5E97\u8857
app.version=1.0.0
app.description=\u8FD9\u662F\u4E00\u5BB6\u6BCD\u5A74\u7535\u5546\u7F51\u7AD9
app.page-size=20
app.show-advert=true
app.website=http://www.yangdianjie.com
需要使用的时候再属性上方加@value("${app.name}")
在入口类加入
@PropertySource("classpath:config.properties")
2.方法二
首先同上创建config.properties
然后创建一个实体类来承载这些值
package com.itlaoqi.myspringboot.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component//这是一个组件类,写上这个注解,SPringBoot会在启动时加载它
//将所有app前缀的属性自动赋值给对应Bean 属性
@ConfigurationProperties(prefix="app")
public class AppConfig {
private String name;
private String description;
private String version;
private Integer pageSize;
private Boolean showAdvert;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Boolean getShowAdvert() {
return showAdvert;
}
public void setShowAdvert(Boolean showAdvert) {
this.showAdvert = showAdvert;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
最后使用的时候,只需要@Resource
三.一分钟搭建好项目
打开idea,创建工程,选择spring initize,填好相应的maven信息,选择web模块,thymeleaf(模板引擎),完成。
四.整合日志框架
由于spring boot中关于日志的配置是有限的,因此尝尝根据logbak-spring.xml进行配置
五.整合模板引擎
有freeMarker,Thymeleaf,Beetl
六.整合SpringMVC
(一)上传文件
(二)自定义404,500页面
只需要在resource 目录下template新建目录 error 添加500.html,400.html
(三)自定义Filter
1.自定义Filter类
2.注册到启动类
七.整合Mybatis
(一)导入jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
(二)修改配置文件
#配置Datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://120.79.137.164:3306/zqdy_userdb?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
#mybatis
#Core Config File
#start width /
mybatis.config-location=classpath:/mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
#show sql
logging.level.com.itlaoqi.springbootmybatis.mapper = debug
#format json
spring.jackson.serialization.indent-output=true
(三)主类扫描DAO
@MapperScan("com.zqdy.userservice.dao")
(四)逆向工程的事情
(五)使用阿里云数据连接池
七.替换内置Tomcat
八.热部署
九.打成jar包
- 1.利用 mvn package 打成jar
- 2.利用java -jar xxx.jar 启动应用
- 3.在同级目录下添加配置文件,应用生效
- 4.在linux环境下利用 nohup java -jar xxx.jar >> xxx.log & 命令后台运行 netstat -tulpn查看pid 想停止就kill -9 pid
- 5.使用tail -f xxx.log 监控日志文件