A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

环境: idea  mybatis  mysql  jdk1.8

功能: xml实现一对多映射

异常:org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/itcast/demo/dao/IUserdao.xml
### The error occurred while processing mapper_resultMap[user]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalStateException: Cannot define both nestedQueryId and nestedResultMapId in property accounts

        at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
        at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
        at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
        at com.itcast.demo.App.befor(App.java:28)
        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:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        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. Cause: java.lang.IllegalStateException: Cannot define both nestedQueryId and nestedResultMapId in property accounts
        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:78)
        ... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalStateException: Cannot define both nestedQueryId and nestedResultMapId in property accounts
        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)
        ... 27 more


代码:

    @Test
    public void find() {

//        一对多
        User user = userdao.findUser(16);
        List<Account> accounts = user.getAccounts();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }



Dao层:    User findUser(int id);

Mapper文件:
    <select id="findAccountsById" resultMap="accountx">
        SELECT * from account WHERE uid=#{id}
    </select>

    <select id="findAccountById" resultMap="accountx">
        SELECT * from account WHERE uid=#{id} limit 1
    </select>

    <select id="findUser" resultMap="user">
        SELECT * from user where id=#{0}
    </select>

    <resultMap id="auser" type="user">
        <id property="id" column="id"/>
        <result property="address" column="address"/>
        <result property="birthday" column="birthday"/>

        <result property="sex" column="sex"/>
        <result property="username" column="username"/>
    </resultMap>

    <resultMap id="accountx" type="account">
        <id property="accountId" column="accountId"/>
        <result property="money" column="money"/>
    </resultMap>

    <!--一对多-->
    <resultMap id="user" type="user" extends="auser">
        <collection property="accounts" column="id" select="findAccountsById"  resultMap="accountx" ofType="account"/>
    </resultMap>





解决方案 :  异常显示 是mapper文件 的相关问题, 检查之后, 去掉<collection>resultMap就好了


希望对大家有用;

1 个回复

正序浏览
123123123123
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马