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的屬性值,我使用的是隨機函數(shù)生成的Long 值進行填充,導致sharding-jdbc 路由引擎執(zhí)行分析時與自己定義的數(shù)據(jù)庫規(guī)則計算值異常導致輸出了一個不存在的數(shù)據(jù)源。
調整后的插入功能代碼:
@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 "批量新增成功"; }
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用SpringBoot實現(xiàn)微服務超時重試模式的示例
這篇文章主要介紹了使用SpringBoot實現(xiàn)微服務超時重試模式的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-11-11Java多線程編程中使用Condition類操作鎖的方法詳解
Condition是java.util.concurrent.locks包下的類,提供了對線程鎖的更精細的控制方法,下面我們就來看一下Java多線程編程中使用Condition類操作鎖的方法詳解2016-07-07mybatis實現(xiàn)對數(shù)據(jù)的增刪查改實例詳解
這篇文章主要介紹了mybatis實現(xiàn)對數(shù)據(jù)的增刪查改實例詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Springboot如何獲取配置文件application.yml中自定義的變量并使用
這篇文章主要介紹了Springboot中獲取配置文件(application.yml)中自定義的變量并使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Spring實戰(zhàn)之使用注解實現(xiàn)聲明式事務操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用注解實現(xiàn)聲明式事務操作,結合實例形式詳細分析了spring使用注解實現(xiàn)聲明式事務相關配置、接口實現(xiàn)與使用技巧,需要的朋友可以參考下2020-01-01SpringBoot集成H2內存數(shù)據(jù)庫的方法
H2是Thomas Mueller提供的一個開源的、純java實現(xiàn)的關系數(shù)據(jù)庫。本文主要介紹了SpringBoot集成H2內存數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下2021-09-09