mybatis执行test07测试类却显示test05测试类调用的sql语句出错

Posted 凉心、惊梦。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis执行test07测试类却显示test05测试类调用的sql语句出错相关的知识,希望对你有一定的参考价值。

1.测试类
@Test
public void test07()
{
    IStudentDao studentDao = new IStudentDaoImpl();
    Student student = studentDao.selectStudentById(10);
    System.out.println("查找成功!");
    System.out.println(student.toString());
}
2.IStudentDao接口
@Override
public Student selectStudentById(int id) {
    SqlSession sqlSession = null;
    try {
        sqlSession = mysqlSession.getSqlSession();
        Student student = sqlSession.selectOne("selectId",id);
        return student;
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(sqlSession!=null){
            sqlSession.close();
        }
    }
    return null;
}
3.mapper.xml
...
<select id="selectAll" resultType="com.abc.beans.Student"> select * from student </select> <!--根据姓名查询全部学生信息Map --> <select id="selectAll" resultType="com.abc.beans.Student" parameterType="String"> select * from student where name = #{name} </select> <select id="selectId" resultType="com.abc.beans.Student"> select * from student where id = #{id} </select>
...

问题,调用test07测试,用的是

<select id="selectId"  resultType="com.abc.beans.Student">
    select * from student where id = #{id}
</select>

但是错误提示是selectAll边出问题了,最后才发现是自己大意写了两个Id名为selectAll

报错信息:

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/abc/dao/IStudentDao.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is ‘com/abc/dao/IStudentDao.xml‘. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for org.mybatis.example.BlogMapper.selectAll
 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
 at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)
 at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)
 at com.abc.utils.MySqlSession.getSqlSession(MySqlSession.java:16)
 at com.abc.dao.IStudentDaoImpl.selectStudentById(IStudentDaoImpl.java:132)
 at com.abc.AppTest.test07(AppTest.java:90)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
 at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
 at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
 at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is ‘com/abc/dao/IStudentDao.xml‘. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for org.mybatis.example.BlogMapper.selectAll
 at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
 at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
 at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)
 ... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is ‘com/abc/dao/IStudentDao.xml‘. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for org.mybatis.example.BlogMapper.selectAll
 at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
 at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
 at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:373)
 at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
 ... 28 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for org.mybatis.example.BlogMapper.selectAll
 at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:872)
 at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:844)
 at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:668)
 at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:302)
 at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:109)
 at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:135)
 at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:128)
 at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118)
 ... 31 more

 

以上是关于mybatis执行test07测试类却显示test05测试类调用的sql语句出错的主要内容,如果未能解决你的问题,请参考以下文章

xcode 8 执行 UI 测试用例有啥命令吗?

maven打包时有test单元测试的项目一直执行项目问题

在mybatis的xml文件中如何使用test标签

使用 react-testing-library 在 useEffect 中测试 api 调用

使用 py.test 打印测试执行时间并锁定慢速测试

MyBatis if test 判断字符串相等不生效