Java 后端开发的学习流程:
Java-> MySql -> Servlet -> Spring-> Spring MVC -> MyBatis -> Spring boot
如果有Java基础和MySql 的CRUD基本技能,可以跳过中间这部分,直接学习Spring boot 就可以,没有任何问题。
第一步:下载 Maven
Maven – Download Apache Maven 下载 apache-maven-3.6.3-bin.zip
解压后放到指定的目录 如 /Users/zhangfengzhou/Tools/Maven/apache-maven-3.6.3
第二步:配置Maven远程仓库镜像
修改 conf目录下的settings.xml 文件 在mirrors中添加 阿里云的远程仓库
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
第三步:创建SpringBoot项目
1. File->New->Project,选择Spring Initializr,选择合适 Project SDK, 此处选择JDK8.0
2. 点击Next 之后,进入项目信息配置页面,注意,这里的Java Version和前面选择的 Project SDK 版本一致,选择Java 8
3. 点击Next,进入项目依赖配置页面,进行如下配置
进行依赖项的配置
Web ->Spring Web
Template Engines -> Thymeleaf
SQL -> JDBC API MyBatis Framework MySQL Driver
配置依赖完成之后,点击Finish完成项目的创建
第四步:配置项目Maven仓库
在完成项目创建之后,在打开项目之后,选择Preferences->Build, Exectuion,Development->Maven,进行如下设置
第五步:认识项目结构
- src/main/java 项目源码部分
- src/main/resources 是项目资源部分 分为三部分:static templates application.properties
- static 用来存放静态文件 如html,js,css 等
- templates 用来存放模板文件
- application.properties 用来存放 项目配置的 如web访问地址,端口号,MySQL数据库连接地址以及其他的项目配置,通过这些配置可以简化很多操作
- src/test 用来存放项目的测试代码 用来测试代码的业务逻辑是否正确等
- pom.xml 是用来进行项目的依赖管理 类似 AndroidStudio 项目中的 gradle 文件,可以引入一些第三方的库来帮助我们进行项目的构建
第六步:配置pom.xml文件
在pom.xml中我们一般都会将项目配置成如下内容,配置完成之后,相关的jar会自动引入
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.luckyboy</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- thymeleaf模板引擎配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- MySQL数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!-- 单元测试配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 配置注解处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>*.keystore</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>*.keystore</include>
</includes>
</resource>
</resources>
</build>
</project>
第七步:配置首页页面
在templates文件夹下,创建index.html,内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Springboot</title>
</head>
<body>
这是一个Springboot简单启动页面
</body>
</html>
第八步:配置项目属性文件
修改application.properties文件名为application.yml,同时添加如下内容
spring:
datasource:
name: demo # 数据库名称
url: jdbc:mysql://192.168.0.20:3306/demo?useSSL=false # url
username: root # 用户名
password: 123456 #密码
driver-class-name: com.mysql.jdbc.Driver # 数据库连接驱动
thymeleaf:
mode: HTML5
encoding: utf-8
servlet:
content-type: text/html
cache: false # 为了便于测试 在开发时需要关闭缓存
mybatis:
mapper-locations: classpath:mapper/*.xml # 配置映射文件
type-aliases-package: com.luckyboy.demo.bean # 配置实体类
server:
port: 8081
第九步:运行Application
点击Run按钮,然后在浏览器中输入 http://localhost:8081/ 就可以看到首页
第十步:打包并运行
IDE 右侧的 lifecycle->package 点击,在项目的target目录下会生成相应的jar包,然后将项目拷贝到服务器上,执行 java -jar demo-xxx.jar 即可启动项目。
完成上面的内容,基本上可以说是搭建了后台开发架子,后面就是正常的后台接口开发流程,后面会有实践来学习,很是期待......
参考: