项目搭建方式1
springboot内置了tomcat
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.5</version> </dependency> </dependencies>
项目搭建方式2
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.5</version> </dependency> </dependencies>
SpringBoot文件配置
application.properties
server.servlet.context-path=/springboot03 server.port=8090
application.yml
server: port: 8080 servlet: context-path: /springboot03
上下两行隔开两个空格是一个层级。
两者都配置的话.properties的优先级更高。
SpringBoot整合Mybatis
导入依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis的启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies>
application.yml:
spring: datasource: url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: driver-class-name: com.mysql.cj.jdbc.Driver server: servlet: context-path: /springboot03 mybatis: type-aliases-package: com.msb.pojo mapper-locations: classpath:mybatis/*.xml
其他的地方就和springmvc的一样了。
SpringBoot整合logback
logback作用: 打印日志
在resourse目录下增加logback.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--> <property name="LOG_HOME" value="${catalina.base}/logs/"/> <!-- 控制台输出 --> <appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender"> <!-- 日志输出格式 --> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n </pattern> </layout> </appender> <!-- 按照每天生成日志文件 --> <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名--> <FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n </pattern> </layout> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 日志输出级别 --> <root level="info"> <appender-ref ref="Stdout"/> <appender-ref ref="RollingFile"/> </root> <logger name="com.msb.mapper" level="DEBUG"></logger> <!-- <!–日志异步到数据库 又开启一个线程来打印日志–>--> <!-- <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">--> <!--<!– 日志异步到数据库–>--> <!-- <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">--> <!--<!– 连接池–>--> <!-- <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">--> <!-- <driverClass>com.mysql.cj.jdbc.Driver</driverClass>--> <!-- <url>jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true</url>--> <!-- <user>root</user>--> <!-- <password>cl091900</password>--> <!-- </dataSource>--> <!-- </connectionSource>--> <!-- </appender>--> </configuration>
SpringBoot整合pageHelper
导入依赖:
<!-- pageHelper的启动器--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
Controller层:
@RequestMapping("findByPage/{pageNum}/{pageSize}") @ResponseBody public List<Emp> findByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize ){ return empService.findByPage(pageNum,pageSize); }
Service层:
@Override public List<Emp> findByPage(Integer pageNum, Integer pageSize) { Page<Object> objects = PageHelper.startPage(pageNum, pageSize); List<Emp> list=empMapper.findAll(); System.out.println(objects.toString()); // 页码 页大小 当前页数据 总页数 总记录数 //这些对象都在startPage的返回值里 //数据封装方式2pageInfo ->pageBean PageInfo<Emp> pi=new PageInfo<>(list); return list; }
SpringBoot整合Druid
很方便的帮助我们实现对sql执行的监控
导入依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
application.yml配置文件:
spring: datasource: #使用阿里的Druid连接池 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver # 填写你数据库的url、登录名、密码和数据库名 url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: druid: #连接池配置信息 #初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 20 #配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true testOnBorrow: false testOnReturn: false # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,slf4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 # 配置DruidStatFilter web-stat-filter: enabled: true url-pattern: "/*" exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" # 配置DruidStatViewServlet stat-view-servlet: url-pattern: "/druid/*" # IP白名单(没有配置或者为空,则允许所有访问) allow: 127.0.0.1,192.168.8.109 # IP黑名单 (存在共同时,deny优先于allow) deny: 192.168.1.188 # 禁用HTML页面上的“Reset All”功能 reset-enable: false # 登录名 login-username: admin # 登录密码 login-password: 123456 main: allow-circular-references: true
SpringBoot整合FreeMarker
导入依赖:
<!-- freeMaker启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
Controller:
@Controller public class FreemarkerController { @RequestMapping("/show") public String freemarger(Map<String,Object> map){ map.put("name","漩涡刘能"); return "show"; } }
show.ftlh:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <br/> ${name} </body> </html>