一、创建maven项目,项目结构如图
关于项目的创建这里就不在多做介绍,如有不懂的,可以留言
二、在pom文件中引入所需要的jar
如下是我pom文件引入的jar及详细说明:
<!-- mysql集成 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- 阿里巴巴数据驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- mysql集成 end -->
<!-- spring boot集成mybatis start -->
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.3</version>
</dependency>
<!--pagehelper 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 逆向工程生成 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- spring boot集成mybatis start -->
<!-- 日志记录start -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 日志记录end -->
<!-- 配制spring 热部署 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 配制spring 热部署 end -->
<!-- 添加thymeleaf的依赖 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 添加thymeleaf的依赖 end -->
三、配制application.yml文件
server:
port: 8080
# servlet:
# context-path: /store #项目名配
# 配制模板引擎
spring:
thymeleaf:
prefix: classpath:/templates/views/
suffix: .html
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
cache: false # 关闭缓存,即时刷新,上线生产环境前改为true
mvc:
static-path-pattern: /static/**
# mysql 属性配置
datasource:
# driver-class-name: com.mysql.jdbc.Driver #1.8前的版本使用
driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/mh_db_02?useSSL=flase&verifyServerCertificate=false; #本地
url: jdbc:mysql://你的mysql地址:3306/数据名?useSSL=false&useUnicode=true; #服务器
username: 用户名
password: 密码
#为mybatis设置,生产环境可删除
restart:
include:
mapper: /mapper-[\\w-\\.]+jar
pagehelper: /pagehelper-[\\w-\\.]+jar
companycommonlibs: tk/mybatis.*
# companycommonlibs: com.share.store.*
# mybatis 集成相关配置
mybatis:
type-aliases-package: com.share.store.**.domain
mapper-locations: classpath:mapper/**/*Mapper.xml
# mappers 多个接口时逗号隔开(通用mapper配制)
mapper:
mappers:
- com.share.store.utils.MyMapper
not-empty: false
identity: MYSQL
# pagehelper 分页插件配制
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
# log 日志配制
logging:
level:
com.share.store: debug #配制控制台打印sql
注:根据配制文件来配制所需要的文件及创建文件夹
四、Springboot启动类配制
五、完结
启动运行,输入地址访问,大功告成
最后,如果有需要源码的朋友,在评论区留言。