Sharding-jdbc 提示:no table route info

异常描述:

### Error updating database.  Cause: java.lang.IllegalStateException: no table route info
### 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() {
		List<Order> orders = new ArrayList<Order>();
		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");
			orders.add(order);
		}
		if(orders != null && orders.size() > 0){
			orderService.saveBatch(orders);
		}

		return "批量新增成功";
	}

使用mybatis-plus 默认批量处理,导致t_order 表无法获取表路由信息导致数据无法入库。

mybatsi-plus 批量处理的语法规则是:多条insert 语句使用分号进行分隔,但是sharding-jdbc 路由引擎会分析每一条执行的sql 语句,并根据用户配置的路由规则达到指定的表,但是多条语句批量插入仅仅会只有一条语句插入成功,其他的都会插入失败,因为无法找到表路由信息。

异常解决办法

在使用sharding-jdbc 分库分表的项目中,禁止使用批量新增、批量修改功能。

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/112380132