Introduction
download:
Cloud native application architecture design and development
点击下哉ZY
:https://www.97yrbl.com/t-1389.html
This article will explain in detail how to build an Spring boot Mybatis
environment, involving dependency configuration and sample code and test related code
Based on Spring Boot 2.3.4, Junit5
Step Instructions
The final directory structure of the entire project is as follows, add a reference to a file or a newly created directory:
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─mall
│ │ └─MallWeb
│ │ ├─controllers
│ │ ├─mapper
│ │ ├─model
│ │ └─services
│ └─resources
│ └─mybatis
│ └─mapper
└─test
└─java
└─com
└─mall
└─MallWeb
└─mapper
复制代码
复制代码
Database initialization statement
The initialization statements of the database tables are all below, and you can also modify them according to your own situation.
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: dependency added
Need to add the following dependencies
- '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: Configuration added
When writing the configuration file, you need to pay attention to the configuration of the parameters. For example, the SSL one should generally be set to false, and the driver-class-name should also be paid attention to. Don’t make a mistake. The general content of the file is as follows:
# 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
复制代码