前言
距离springboot的发布已经过去好几年,现在spring家族越发强大,而springboot让java程序员解放了诸多繁杂的配置,让当年配置需要一周乃至更久的时代成为了过去式。让我们一起一步步搭建一个springboot项目吧~
我的环境:
- 基础环境
环境不同总会有各种难以预料的bug,此处笔者采用的基础环境是 openjdk 1.8.0_224 + mysql 8.0.12 + maven 3.6.3
2.项目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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.yh</groupId>
<artifactId>java_ks_project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 配置全局属性 -->
<properties>
<druid.version>1.1.22</druid.version>
<mybatisPlus.version>3.3.0</mybatisPlus.version>
<mysql.version>8.0.20</mysql.version>
<netty.version>4.1.43.Final</netty.version>
<easyexcel.version>2.2.4</easyexcel.version>
<gson.version>2.8.6</gson.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<!-- springboot快速启动包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- druid最新包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mybatisPlus最新包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisPlus.version}</version>
</dependency>
<!-- mysql最新连接包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- netty通信包 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<!-- 阿里开源的excel处理包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
<!-- 谷歌json转化包 -->
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- 引入模板引擎,类似 jsp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 配置切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 开发时热更新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.springboot项目配置文件分为.yml和.properties两种,笔者更喜欢用.properties的方式配置,以下为.properties文件配置
#数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java_ks_project?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#MybatisPlus配置
mybatis-plus.type-aliases-package=com.brm.entity
mybatis-plus.mapper-locations=classpath:mapper/*.xml
#mybatis-plus.executor-type=batch
#表名前缀
mybatis-plus.global-config.db-config.table-prefix=jks_
#配置逻辑删除
mybatis-plus.global-config.db-config.logic-delete-field=isDelete
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
#日志配置
logging.level.org.springframework=INFO
logging.level.com.brm.mapper=DEBUG
logging.level.com.brm.controller=DEBUG
#端口配置
server.port = 2333
server.max-http-header-size=10240000
server.tomcat.max-http-header-size= 10240000
server.tomcat.max-http-form-post-size=2MB
#文件上传
#设置单文件上传大小1
spring.servlet.multipart.max-file-size = 1MB
#设置总文件上传大小
spring.servlet.multipart.max-request-size=3MB
#thymeleaf 配置
##去除thymeleaf的html严格校验
spring.thymeleaf.mode=HTML
# 是否开启模板缓存,默认true
# 建议在开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
# 模板编码
spring.thymeleaf.encoding=UTF-8
#热部署配置
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/
4.程序启动入口编写,这里 以jar包运行为例,项目启动文件如下:
package org.yh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YHApplication {
public static void main(String[] args) {
SpringApplication.run(YHApplication.class);
}
}
项目目录
5.restful风格控制器编写,让我们一起hello world
package org.yh.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* restful风格的api
*/
@RestController
public class HelloController {
@GetMapping(path = "hello")
public String hello() {
return "hello world";
}
}
6.再整个佛祖保佑项目无bug,在resouce中加入banner.txt,复制以下内容即可
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
// ............................................. //
////////////////////////////////////////////////////////////////////
6.项目运行
启动成功
7.浏览器测试接口,输出hello world,项目基本架构搭建完毕
浏览器输出hello world
网友评论