前言
新手小白在学习新技术的时候,总会遇到很多问题,导致心态崩溃,这也导致了部分初学者走了从入门到放弃这条路。
其实,接触任何新事物,学习任何新技能,都会遇到很多新奇的问题。只要针对遇到的问题,逐个解决,你也就入了门,就开启了正式的学习之旅!
只要思想不滑坡,办法总比困难多!
入门Spring Boot,搞懂基本的环境配置,可以让初学者少走很多弯路!
正文
Spring Boot框架,是目前行业中用的最多的框架。
传统行业的技术人员(技术栈相对落后),如果需要升职加薪或跳槽,没有点硬实力是不行的。因此,不努力提升自己的实力,更新自己的技术栈,就会碰触到职业生涯的天花板,很难有所晋升!
创建 Spring Boot 项目
新手小白可以这样操作:打开 idea 开发工具,依次执行 File -> New -> Project,在新建项目对话框中选择Spring Initializr,然后根据引导程序创建Maven项目,并选择自己需要的项目依赖包,操作流程如下所示。
配置环境依赖包
创建项目的时候,可以直接创建Maven项目,这样操作技术要求高,需要自己在pom.xml中手动编写环境依赖配置。
对于新手小白,进修者不推荐这样操作。当然也可以从已有项目中拷贝依赖包配置,可以参考下面的基本依赖包配置。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.15.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.erbin</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</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.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>8.0.27</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意:如果更换了 pom 包依赖,需要重新下载相关依赖;然后启动服务,项目正常运行,说明包依赖可以使用。如果运行异常,需要重新更换 jar 包的版本!
数据库连接及连接池
配置数据库连接及相关连接池。
这里以用的非常多的MySQL数据库为例,在application.properites(或者application.yml)文件中配置相关参数,具体配置如下:
# MySQL datasource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydbname?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&useTimezone=true
spring.datasource.username=root
spring.datasource.password=123456
# alibaba druid 连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
注意:如果MySQL驱动版本较高,需要在数据源url中添加这几个参数,否则项目启动会报异常。相关参数如下:
useSSL=false&serverTimezone=Asia/Shanghai&useTimezone=true
配置数据持久层框架
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJO(Plain Ordinary Java Object,普通的 Java 对象)映射成数据库中的记录。使用非常简单快捷,在 application.properties 文件中配置,具体参数如下:
# mybatis configuration
# 开启驼峰方式
mybatis.configuration.map-underscore-to-camel-case=true
# 配置 mapper 扫描路径
mybatis.mapper-locations=classpath:mapper/*.xml
# 配置类型别名
mybatis.type-aliases-package=com.erbin.springbootdemo.bean
# 开启 SQL 日志打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
配置日志打印
默认使用 slf4j 日志记录方式。在 application.properties 文件中配置相关参数如下:
# 开启 SQL 日志打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
# slf4 log level
logging.level.com.erbin.springbootdemo=info
配置 Spring Boot 热部署
Spring Boot 热部署:在开发过程中,修改代码或配置后不需要重启服务就会自动编译运行项目,非常方便。配置也很简单,可以参考下面的操作步骤。
在 pom.xml 中,添加 spring-boot-devtools 工具依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
设置 idea 工具,使其支持程序运行时自动编译项目。
按快捷方式:ctrl+shift+alt+/ 。在弹出的菜单中,选择第1项 Registry,找到下图中的选项并勾选。
配置完成后,重启 idea 开发工具(必须重启哦),使所有配置生效。
重新打开 idea 后,就可以编写自己的运行 demo 示例!当然你也可以参考这篇文章学习哦!Spring Boot整合MyBatis Plus,实现增删改查(CRUD)
小结
通过上面内容的学习,我相信新手小白也可以搭建起自己的入门工程了!入门Spring Boot其实很简单,只需要学会配置环境依赖包、配置数据库连接及连接池、配置日志打印、配置热部署等这些基础配置,也就真的入了门。初学者想从入门到精通,我只能回复:剩下的交给时间!
温情提示:生活不止眼前的苟且,还有诗和远方!
感谢您读完了进修者的内容分享,欢迎留言区一起聊聊天,聊聊关于您对“Spring Boot环境搭建”有什么更好的想法,都可以随时叨扰!
我是进修者,期待与您肩并肩,一起进化成长!