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

环境筹备

  • 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

执行单元测试查看后果。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理