本文主要是對於spring boot中非常淺顯的知識進行介紹,並不深入瞭解,只接觸表面,對一些較複雜的內容也不過多描述。如文中有錯誤之處,望不吝賜教,謝謝~
在上一篇文章Spring Boot基礎知識學習(一)中,單單隻使用了spring boot,而實際開發過程中,並不會只使用spring boot,還會整合mybatis、junit等框架,這裏就對此進行簡單介紹。
一、spring boot整合其他框架的簡單使用
(1)新建spring initializr項目
選擇依賴(也可以不選擇,創建好項目後,自己可以去pom.xml文件裏面去手動創建)
(2)查看pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
可以發現持久層框架mybatis以及測試框架junit已經導入。
(3)配置spring boot信息(這裏暫時只包括數據庫信息)。
在application.properties中添加數據量的連接信息
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
注意實現本地上已存在test數據庫,含有一張用戶表user.。
(4)創建用戶實體類以及對應的mapper
User類
package com.example.springboot_study_demo2.entity;
public class User {
// 主鍵
private Long id;
// 用戶名
private String username;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
User對應的mapper類
package com.example.springboot_study_demo2.mapper;
import com.example.springboot_study_demo2.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
/**
* 通過用戶名查詢用戶
* @return
*/
User findByUsername(String username);
}
注意這裏的@Mapper註解,一定不要忘記,不然會出錯。
(5)配置Mapper映射文件
在src\main\resources\目錄下新建mappers目錄,再新建UserMapper.xml配置文件(注意路徑)
(6)在application.properties中添加mybatis的信息
#別名掃描包
mybatis.type-aliases-package=com.example.springboot_study_demo2.entity
#加載Mybatis映射文件
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
(7)驗證spring boot中的mybatis和junit
在新建項目時就自動創建了一個test目錄,這個test目錄和和main目錄是對應的,專門用於測試用的。IDEA還自動生成了整個文件的測試類,我們就要在這個類裏面編寫相應的代碼
package com.example.springboot_study_demo2;
import com.example.springboot_study_demo2.entity.User;
import com.example.springboot_study_demo2.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootStudyDemo2ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
}
//測試 mybatis junit
@Test
public void test(){
User user=userMapper.findByUsername("xiaoming");
System.out.println(user);
}
}
需要注意項目的名稱以及各個類的路徑。代碼中出現的@RunWith(SpringRunner.class)、@SpringBootTest、@Autowired、@Test註解十分重要,切記;另外有關注解的知識不在此展開。
最終的結果應輸出username爲xiaoming的這個user對象
(8)整個項目的結構如下所示
2020.03.07