Spring Boot + MySQL + MyBatis + Maven + Logging + Thymeleaf 项目搭建、配置、运行、部署完整教程

1. 创建一个springboot应用

1.1 创建

  springboot应用没有相应的archetype,不能通过mvn generate:archetype创建,一般有两种方式创建一个springboot应用:

  • 访问 https://start.spring.io/,选择Web依赖,点击Generate Project 创建一个springboot应用。
  • 通过IDEA:File -> New Project -> Spring Initializr -> Choose Initializr Service URL(https://start.spring.io) -> next(填写 Project Metadata) -> next -> 选择Web -> Finish

1.2 启动

mvn spring-boot:run
# 或
mvn clean spring-boot:run

1.3 @SpringBootApplication

  @SpringBootApplicatoin是用的@ComponentScan扫描,扫描的是Component,包括@Component, @Controller, @Service, @Repository等。

  SpringBootApplication默认扫描运行类所在包及其子包中的Component

2. 单元测试

2.1 spring-boot-starter-test Starter

  通常通过添加 spring-boot-starter-test "Starter"依赖来导入 Spring Boot test 模块以及 JUnit, AssertJ, Hamcrest, Spring Test, Mockito, JSONassert and JSONPath 库,如果这些库满足不了你的需要,你可以再添加其他的测试依赖。

        <!-- spring-boot-starter-test imports both Spring Boot test modules as well as JUnit, AssertJ, Hamcrest, Spring Test, Mockito, JSONassert and JSONPath -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.2 @SpringBootTest

A Spring Boot application is a Spring ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.

Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication.

3. 集成MyBatis

3.1 pom.xml中添加MyBatis依赖

        <!-- mybatis ORM 框架 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

3.2 application.properties中添加相关配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/erp?autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# \u521D\u59CB\u5316\u5927\u5C0F\uFF0C\u6700\u5C0F\uFF0C\u6700\u5927
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# \u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4
spring.datasource.maxWait=60000

# mybatis
mybatis.mapper-locations=classpath*:mapping/*.xml
mybatis.type-aliases-package=net.mrliuli.erp.domain.entity

3.3 添加mybatis-generator插件

3.3.1 pom.xml中添加插件
            <!-- Mybatis Generator 插件 ======&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值