序章
download:
クラウドネイティブアプリケーションアーキテクチャの設計と開発
点击下哉ZY
:https://www.97yrbl.com/t-1389.html
Spring boot Mybatis
この記事では、依存関係の構成とサンプルコード、およびテスト関連のコードを含む、環境の構築方法について詳しく説明します。
Spring Boot 2.3.4、Junit5に基づく
ステップの説明
プロジェクト全体の最終的なディレクトリ構造は次のとおりです。ファイルまたは新しく作成されたディレクトリへの参照を追加します。
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─mall
│ │ └─MallWeb
│ │ ├─controllers
│ │ ├─mapper
│ │ ├─model
│ │ └─services
│ └─resources
│ └─mybatis
│ └─mapper
└─test
└─java
└─com
└─mall
└─MallWeb
└─mapper
复制代码
复制代码
データベース初期化ステートメント
データベーステーブルの初期化ステートメントはすべて以下のとおりです。また、状況に応じて変更することもできます。
CREATE DATABASE IF NOT EXISTS `mall`;
USE `mall`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
`phoneNumber` varchar(15) NOT NULL,
`money` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
复制代码
复制代码
build.gradle:依存関係が追加されました
次の依存関係を追加する必要があります
- 'mysql:mysql-connector-java:8.0.14'
- 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
- 'org.projectlombok:lombok:1.16.16'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// MySQL数据库需要
implementation 'mysql:mysql-connector-java:8.0.14'
// spring mybatis
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
// lombok,用于Entity的自动get和set方法生成,不用自己写一大推的get和set方法
implementation 'org.projectlombok:lombok:1.16.16'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
复制代码
复制代码
application.properties:構成が追加されました
設定ファイルを作成するときは、パラメータの設定に注意する必要があります。たとえば、SSLの設定は通常falseに設定し、driver-class-nameにも注意を払う必要があります。間違い。ファイルの一般的な内容は次のとおりです。
# gradle
# mybatis的config文件位置配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# 各个表的xml文件位置配置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.neo.model
# 数据库连接信息配置,自行更换数据库,用户名和密码
spring.datasource.url=jdbc:mysql://localhost:3306/mall?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8\
&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#springboot + mybatis设置将SQL语句打印到控制台
logging.level.com.mall.MallWeb.mapper=debug
复制代码