关于springboot:springboot30快速搭建入门测试资料整理

9次阅读

共计 3531 个字符,预计需要花费 9 分钟才能阅读完成。

环境筹备

  • jdk17
    参考《JDK17 装置教程及环境变量配置》
    jdk17 下载,或者先装置 Scoop,应用 scoop install openjdk19; scoop install openjdk8-redhat
  • IntelliJ
    工具下载:jetbrains-toolbox,装置 IntelliJ IDEA Ultimate
  • 官网文档
    中文官网入门文档
    [api 文档]https://springdoc.org/v2/

一、装置 Spring Boot[gradle]

以下为 win11 环境

1. 装置 Scoop

应用中文官网入门文档中 3.2.6. Windows Scoop 装置

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh -outfile 'install.ps1'
.\install.ps1 -RunAsAdmin
https://github.com/ScoopInstaller/Install#for-admin

官网教程 admin 用户。应用迷信上网装置,或增加 hosts: 解决 raw.githubusercontent.com 无法访问的问题。

# 或应用国内镜像下载:iwr https://gitee.com/glsnames/scoop-installer/raw/master/bin/install.ps1 | iex

2. 初始化装置

scoop install git
# 官网提供
scoop bucket add extras
scoop bucket add java

scoop install gradle
scoop install springboot
# scoop install springboot@2.7.6
spring --version

参考《Scoop windows 下的包管理器》
换源:进步下载速度
要改善 Scoop 的下载速度,具体能够参照 Scoop | Gitee 版 的阐明更换下载源。换源之后的 Scoop,速度晋升不是一星半点儿。

更换 Scoop 源
scoop config SCOOP_REPO 'https://gitee.com/glsnames/scoop-installer'
scoop config SCOOP_REPO https://gitee.com/squallliu/scoop
scoop update

git -C "${Env:USERPROFILE}\scoop\buckets\main" remote set-url origin https://hub.fastgit.org/ScoopInstaller/Main.git
git -C "${Env:USERPROFILE}\scoop\buckets\extras" remote set-url origin https://hub.fastgit.org/lukesampson/scoop-extras.git


gradle 与 Java 兼容性阐明

3. 建设空我的项目

新建我的项目

  • 在 gradle/wrapper/gradle-wrapper.properties 中批改 distributionUrlgradle版本与以后环境兼容

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
  • 在根目录 build.gradle 中批改 sourceCompatibility 版本与以后环境统一

    sourceCompatibility = '19'

    《gradle 中的 sourceCompatibility 与 targetCompatibility 的区别》

点击工具栏运行我的项目。拜访:http://localhost:8080/。

二、连贯 MySQL

1. 创立 MySQL 测试环境

docker network create mybridge --subnet=172.16.0.0/16
docker run --name mysql8 \
    --network mybridge --ip=172.16.11.1 -p 3306:3306 \
    -e MYSQL_ROOT_PASSWORD='dbpass' \
    --restart=always \
    -d mysql --character-set-server=utf8mb4
docker rm -f pma01 && docker run --name pma01 \
    --network mybridge --ip=172.16.33.1 -p 9033:80 \
    -e PMA_HOST=mysql8 \
    -e PMA_PORT=3306 \
    -e PMA_USER='root' \
    -e PMA_PASSWORD='dbpass' \
    --restart=always \
    -d phpmyadmin

路由器绑定 IP 并拜访 http://192.168.10.104:9033/。phpmyadmin 须要 root 用户。创立 springboot 数据库。

-- drop user 'dbuser'@'%';
create user dbuser@'%' identified by 'dbpass';
update mysql.user set authentication_string = ''where user ='dbuser';
FLUSH PRIVILEGES;
update mysql.user set host = '%', plugin = 'mysql_native_password' where user = 'dbuser';
ALTER USER 'dbuser'@'%' IDENTIFIED WITH mysql_native_password BY 'dbpass';
grant all privileges on *.* to dbuser@'%';
flush privileges;

2. 装置 MyBatis-Plus

Could not find method compile 解决方案

compile, runtime, testCompile, and testRuntime 等在 Gradle 7.0 当前移除了,咱们须要用 implementation, runtimeOnly, testImplementation, and testRuntimeOnly 一代替。例如:compile 改成 implementation.

build.gradle 中批改

dependencies {
    implementation 'mysql:mysql-connector-java:8.0.31'
    implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.3.1'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

resources/application.properties 中批改增加

# mysql
spring.datasource.url=jdbc:mysql://192.168.10.104/springboot
spring.datasource.username=dbuser
spring.datasource.password=dbpass

点击工具栏运行我的项目。

3. 简略测试 CURD

装置 mybatisx 插件。
增加 lombok
代码上传 https://github.com/cffycls/springboot

src/main/resources/application.properties 数据库配置,spring.sql.init.mode=alaways 同步 |never 不同步数据库。

# mysql
spring.datasource.url=jdbc:mysql://192.168.10.104/springboot?characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.username=dbuser
spring.datasource.password=dbpass
# mysql.init
spring.sql.init.mode=never
spring.sql.init.schema-locations=classpath:sql/schema.sql,classpath:sql/data.sql

执行单元测试查看后果。

正文完
 0