Sharding-jdbc報錯:Missing the data source name:‘m0‘解決方案
異常描述
### Error updating database. Cause: java.lang.IllegalStateException: Missing the data source name: 'm0'
### The error may exist in com/zzg/mapper/OrderMapper.java (best guess)
### The error may involve com.zzg.mapper.OrderMapper.insert-Inline
### The error occurred while setting parameters
### Cause: java.lang.IllegalStateException: no table route info] with root cause
java.lang.IllegalStateException: no table route info
異常造成原因
@RequestMapping(value = "/batchInsert", method = { RequestMethod.GET })
public Object batchInsert() {
for (int i = 0; i < 10; i++) {
Order order = new Order();
order.setPrice(new BigDecimal(Math.random()));
order.setUserId(new Random().nextLong());
order.setStatus("0");
orderService.save(order);
}
return "批量新增成功";
}使用MyBatis-plus 新增Order 實體屬性時,提示Missing the data source name: 'm0'
造成原因是由于userId的屬性值,我使用的是隨機(jī)函數(shù)生成的Long 值進(jìn)行填充,導(dǎo)致sharding-jdbc 路由引擎執(zhí)行分析時與自己定義的數(shù)據(jù)庫規(guī)則計算值異常導(dǎo)致輸出了一個不存在的數(shù)據(jù)源。
調(diào)整后的插入功能代碼:
@RequestMapping(value = "/batchInsert", method = { RequestMethod.GET })
public Object batchInsert() {
for (int i = 0; i < 10; i++) {
Order order = new Order();
order.setPrice(new BigDecimal(Math.random()));
order.setUserId(Long.valueOf("" + i));
order.setStatus("0");
orderService.save(order);
}
return "批量新增成功";
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Sentinel啟動失敗及運(yùn)行時常見錯誤總結(jié)
本文總結(jié)了Spring Cloud Alibaba Sentinel實際使用中的常見問題及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
SpringBoot 創(chuàng)建web項目并部署到外部Tomcat
本篇文章主要介紹了SpringBoot 創(chuàng)建web項目并部署到外部Tomcat,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
關(guān)于MyBatis中映射對象關(guān)系的舉例
這篇文章主要介紹了關(guān)于MyBatis中映射對象關(guān)系的舉例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Java一維二維數(shù)組的使用方法詳解(Arrays類方法)
這篇文章主要給大家介紹了關(guān)于Java一維二維數(shù)組使用方法的相關(guān)資料,數(shù)組是相同類型元素的集合,可以通過索引訪問元素,文章還討論了數(shù)組的遍歷、排序、查找等操作,并提供了相關(guān)示例代碼,需要的朋友可以參考下2025-05-05
Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務(wù)@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
k8s+springboot+CronJob定時任務(wù)部署實現(xiàn)
本文主要介紹了k8s+springboot+CronJob定時任務(wù)部署實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

