嘿,我正在使用Mybatis和Spring Annotations。
并收到此错误:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.mappers.QuestionsMapper.Question
这里是域类(没有getter和setter):
public class Question {
String optionsAsString;
String typeAsString;
Integer fieldId;
String title;
String description;
public Question(){
}
}
这是我的Mapper.Java类
@MapperScan
public interface Mapper {
public List<Question> getQuestions(@Param("shifts") List<Integer> shifts, @Param("job_id") Integer job_id);
}
最后这里是Mapper.xml
<mapper namespace="com.mypackage.mappers.Mapper">
<resultMap type="com.mypackage.domain.Question" id="Question">
<id column="field_id" property="fieldId" />
<result column="data_type" property="typeAsString" />
<result column="title" property="title" />
<result column="description" property="description" />
<result column="options" property="optionsAsString" />
</resultMap>
<select id="com.mypackage.mappers.Mapper.getQuestions" resultMap="Question" timeout="10">
SELECT
f.field_id,
f.data_type,
f.title,
f.options,
f.description
FROM
(SELECT DISTINCT q.*
FROM
question_services qs INNER JOIN
questions q
ON qs.field_id=q.field_id AND q.job_id = qs.job_id INNER JOIN
services s
ON qs.service_id = s.service_id and qs.job_id = s.job_id
WHERE s.job_id = #{job_id} AND s.service_id in
<foreach item="shift" collection="shifts" open="(" separator="," close=")">
#{shift}
</foreach>
) f
</select>
我倾向于认为xml select语句有问题。可能与我如何使用foreach。我有另一个使用类似格式的映射器,它只是不用于每个,它没有任何问题。
答案 0 :(得分:1)
是的,我的select语句中某处出现了错误。我最后只是以不同的方式重写它。
<select id="getQuestions" resultMap="Question">
SELECT
q.field_id,
q.data_type,
q.title,
q.description,
q.options
FROM
questions q
WHERE
job_id = #{job_id}
AND
field_id
IN
(SELECT
fs.field_id
FROM
question_services qs
INNER JOIN
services s
ON
qs.service_id = s.service_id
AND
qs.job_id = s.job_id
WHERE
s.job_id=#{job_id}
AND
s.service_id
IN
<foreach item="item" index="index" collection="shifts" open="(" separator="," close=")">
#{item}
</foreach>
);
</select>
答案 1 :(得分:1)
添加这个答案,因为在我的情况下,问题是不同的,但异常是相同的。例外说,
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.model.mymapper.BaseResultMap
如果您在多个位置中存在相同的映射器xml并且拾取和解析了重复的xml,也会发生这种情况。很好的检查,即使你错误地已经备份了映射器xmls,它位于mybatis配置器扫描的位置或子文件夹中。
答案 2 :(得分:0)
我建议您使用映射器文件自动加载功能代替。 (推荐此解决方案)
删除mybatis.mapperLocations属性。
mybatis:
typeAliasesPackage: com.fiberhome.payroll.model
mapperLocations: classpath*:**/mapper/*.xml
更改映射器文件的存储目录和文件名,如下所示:
您可以使用@Mapper
代替@MapperScan
。
在映射器界面上添加@org.apache.ibatis.annotations.Mapper
界面,如下所示:
@Mapper
public interface DmClassInfoDao {
// ...
}
Delete the @MapperScan from java config
@SpringBootApplication
@ComponentScan(basePackages = {"com.fiberhome.payroll"})
@MapperScan("com.fiberhome.payroll.dao")
public class MybatisSpringBootApp {
// ...
}