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
@SpringBootTestannotation, which can be used as an alternative to the standard spring-test@ContextConfigurationannotation when you need Spring Boot features. The annotation works by creating theApplicationContextused in your tests throughSpringApplication.
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.entit

最低0.47元/天 解锁文章
429

被折叠的 条评论
为什么被折叠?



