baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.35k stars 4.31k forks source link

IDEA本地运行可以,maven打包为单个jar出错 #5597

Closed ChiMuYuan closed 1 year ago

ChiMuYuan commented 1 year ago

当前使用版本(必填,否则不予处理)

spring-boot-starter-parent == 3.1.2 mybatis-plus-boot-starter == 3.5.3.2

该问题是如何引起的?(确定最新版也有问题再提!!!)

自定义 SQL中,自定义 ResultMap一对多:

    <resultMap id="UserDTO" type="com.example.springboot3.entity.dto.UserDTO">
        <id column="user_id" property="userId" />
        <collection javaType="java.util.List" ofType="com.example.springboot3.entity.SysRole" property="roleList">
            <id column="r_role_id" property="roleId" />
        </collection>
    </resultMap>

在IDEA中本地运行没有问题,Maven打包为一个jar运行报错:Result Maps collection already contains value for ......... 因为使用MP生成器生成的XML中有BaseResultMap也爆这个错,但是没用到删除了。 现在是自写的也爆这个错

重现步骤(如果有就写完整)

最小复现项目地址 git@github.com:ChiMuYuan/demo.git https://github.com/ChiMuYuan/demo/tree/main/springboot3

报错信息

Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.example.springboot3.mapper.UserMapper.mapper_resultMap[UserDTO]_collection[roleList]
        at com.baomidou.mybatisplus.core.MybatisConfiguration$StrictMap.put(MybatisConfiguration.java:435) ~[mybatis-plus-core-3.5.3.2.jar!/:3.5.3.2]
        at com.baomidou.mybatisplus.core.MybatisConfiguration$StrictMap.put(MybatisConfiguration.java:405) ~[mybatis-plus-core-3.5.3.2.jar!/:3.5.3.2]
        at com.baomidou.mybatisplus.core.MybatisConfiguration.addResultMap(MybatisConfiguration.java:276) ~[mybatis-plus-core-3.5.3.2.jar!/:3.5.3.2]
        at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:181) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:48) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:296) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.processNestedResultMappings(XMLMapperBuilder.java:413) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.lambda$buildResultMappingFromContext$0(XMLMapperBuilder.java:393) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.parsing.XNode.getStringAttribute(XNode.java:198) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:392) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:287) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:262) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:254) ~[mybatis-3.5.13.jar!/:3.5.13]
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:124) ~[mybatis-3.5.13.jar!/:3.5.13]
        ... 77 common frames omitted

感谢👍

miemieYaho commented 1 year ago

spring打包默认不打java目录下的xml文件的,挪到resource目录下去

ChiMuYuan commented 1 year ago

@miemieYaho 这里的问题貌似不是没有打包啊...他报的应该是重复。比如我之前的BaseResultMap也是Result Maps collection already contains,然后我删掉就不报这个错了。 我参考的是https://baomidou.com/pages/f84a74/#%E8%87%AA%E5%AE%9A%E4%B9%89-sql-%E6%97%A0%E6%B3%95%E6%89%A7%E8%A1%8C 这里面的,在application.yml配置mapper-locations,在pom里面配置resource

nieqiurong commented 1 year ago

这是由于mapper-locations配置的问题导致扫描出来了两份资源文件 修改一下为 mapper-locations: classpath:/mapper//xml/*.xml 就不会出问题,这个问题后面考虑下要不要处理一下.

ChiMuYuan commented 1 year ago

好的谢谢

nieqiurong commented 1 year ago

看着是spring加载资源问题,自行规避把,前面加上你的包名就没太多问题了. classpath*:com/example/**/mapper/**/xml/*.xml