MyBatis 同时存在 xml 的 SQL 语句和注解式 SQL 语句会发生什么?

tech2025-03-27  1

答案


抛异常

 

 

原因


MyBatis 实现了自己的 Map 用于存储映射完毕的语句 MappedStatement。

该 Map 的全限定名为 org.apache.ibatis.session.Configuration.StrictMap。

 其中 Map 的 put 方法如下:

public V put(String key, V value) { if (containsKey(key)) { throw new IllegalArgumentException(name + " already contains value for " + key + (conflictMessageProducer == null ? "" : conflictMessageProducer.apply(super.get(key), value))); } if (key.contains(".")) { final String shortKey = getShortName(key); if (super.get(shortKey) == null) { super.put(shortKey, value); } else { super.put(shortKey, (V) new Ambiguity(shortKey)); } } return super.put(key, value); }

由此可见,首先就会检查是否存在相同的 key,存在就抛出异常。

最新回复(0)