? 專欄地址 系列教程更新中
?? 文章介紹: SpringBoot+MybatisPlus組合可以大大加快開發(fā)效率,緊接上一篇文章的內(nèi)容,這篇文章進行數(shù)據(jù)庫的連接與查詢測試,并配置日志輸出調(diào)試
?? 源碼獲取: 項目中的資料可以通過文章底部公眾號戳聯(lián)系我獲取
maven聚合工程依賴知識
dependencyManagement
元素用于集中管理項目中所有模塊的依賴關(guān)系,包括依賴的版本、范圍、排除等信息。通過在父POM(Project Object Model)文件中定義dependencyManagement
,可以確保子模塊使用一致的依賴版本,避免因不同版本導(dǎo)致的潛在問題。這個標簽下的依賴是不會被引入的
使用方法:
- 在父POM文件中定義
dependencyManagement
元素。例如:
<project>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.4</version>
</dependency>
<!-- 其他依賴 -->
</dependencies>
</dependencyManagement>
...
</project>
- 在子模塊的POM文件中引入需要使用的依賴。注意這里無需指定版本號,因為它會自動繼承父POM中定義的版本。
<project>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 其他依賴 -->
</dependencies>
...
</project>
這樣,在微服務(wù)架構(gòu)下,各個子模塊只需要在其自身的POM文件中引入所需依賴即可。Maven會自動處理好版本信息和傳遞性依賴,確保整個項目中的依賴關(guān)系一致。
- 父項目聲明版本號 common模塊添加具體版本 其他項目需要用到common依賴可直接引入
application.yml 配置文件編寫
server:
port: 9001
spring:
application:
name: xxx-xxx-xxx
#數(shù)據(jù)庫配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/user?useSSL=false&serverTimezone=UTC
username: root
password: password
#配置plus打印sql日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#設(shè)置日志級別,ERROR/WARN/INFO/DEBUG,默認是INFO以上才顯示
logging:
level:
root: INFO
啟動類配置注解
- 由于在運行,Spring需要對整個項目進行掃描,模板生成的mapper文件可能會出現(xiàn)沒有掃描到去情況,這里可以在啟動類配置下注解
@SpringBootApplication
@MapperScan("net.xiaochan.mapper")
查詢接口代碼編寫
@RestController
@RequestMapping("/api/address/v1/")
public class UserController {
@Autowired
private AddressServiceImpl addressService;
@GetMapping("/find/{address_id}")
public Object detail(@PathVariable("address_id") Long addressId){
return addressService.detail(addressId);
}
}
public interface AddressMapper extends BaseMapper<AddressDO> {
}
public interface AddressService {
AddressDO detail(Long id);
}
@Service
public class AddressServiceImpl implements AddressService {
@Autowired
private AddressMapper addressMapper;
@Override
public AddressDO detail(Long id) {
AddressDO addressdo = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id));
return addressdo;
}
}
結(jié)果演示
集成SpringBoot Test單元測試與sql日志打印
- 需求分析->設(shè)計->開發(fā)->測試->上線
- 單元測試: 完成最小的軟件設(shè)計單元的驗證工作,目標是確保模塊被正確的編碼
-
Spring Boot Test 是在Spring Test之上的再次封裝, 使用@SpringBootTest后,Spring將加載所有被管理的bean,等同于啟動了整個服務(wù)
-
common項目添加依賴 用于單元測試
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
Mybatis plus配置控制臺打印日志文章來源:http://www.zghlxwxcb.cn/news/detail-462307.html
#配置plus打印sql日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
文章來源地址http://www.zghlxwxcb.cn/news/detail-462307.html
到了這里,關(guān)于【SpringBoot教程】SpringBoot+MybatisPlus數(shù)據(jù)庫連接測試 用戶收貨信息接口開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!