[Mybatis]xxx is ambiguous in Mapped Statements collection

Mybatis 常见异常:org.apache.ibatis.exceptions.PersistenceException: ```log

Error querying database. Cause: java.lang.IllegalArgumentException: selectAll is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)

Cause: java.lang.IllegalArgumentException: selectAll is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)

```

在使用 SqlSession 调用查询方法时,报出的这个错误。代码如下: ```java public class TestEmp {

private SqlSession session;

@Before
public void before() throws IOException {
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    InputStream is = Resources.getResourceAsStream("sqlMapperConfig.xml");
    SqlSessionFactory factory = builder.build(is);
    session = factory.openSession();
}

@After
public void after() {
    session.close();
}

@Test
public void testSelectAll() {
    List<Emp> list = session.selectList("selectAll");
    list.forEach(System.out::println);
}

} 这是 EmpMapper 映射文件内部代码: xml

  • select * from emp 最后经过排查得出是 XML 文件件中的 CRUD 方法的 id 重复,需要加上命名空间才行,不然 Mybatis 不能识别这个方法对应的是哪个 Mapper 文件内的查询标签。我在另一个 Mapper 文件 DeptMapper 文件中定义了如下查询标签: xml
  • select * from dept 这个标签的 id 属性的值和上面那个是一样的,所以调用时会产生冲突。解决方法就是调用时加上命名空间: java @Test public void testSelectAll() { List list = session.selectList("org.codeArt.mapper.EmpMapper.selectAll"); list.forEach(System.out::println); } `` 这样就能正常运行。注意:org.codeArt.mapper.EmpMapper.selectAll 是你自己定义的 Mapper 文件中的命名空间加上对应的查询标签的 id 值,上面的 DeptMapper 假如想调用内部的方法那就加上org.codeArt.mapper.DeptMapper.selectAll`。命名空间可以在 mapper 标签的 namespace 属性上定义。
    • 1
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值