Mybatis配置错误:BuilderException

时间:2017-08-07 02:34:53

标签: spring-boot mybatis

我正在学习spring boot + Mybatis + MySQL,我只是使用xml作为mybatis mapper编写了一个demo,但是出现了错误:

nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. 
Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value 
for mappers.UserMapper.UserResultMap

我的mapper xml是:

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mappers.UserMapper">
    <resultMap id="UserResultMap" type="hello.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="email" property="email" />
    </resultMap>
    <select id="findAllUsers" resultMap="UserResultMap">
        select id, name, email from users
    </select>
    <select id="findUserById" resultMap="UserResultMap">
        select id, name, email from users WHERE id=#{id}
    </select>
    <insert id="insertUser" parameterType="hello.User" useGeneratedKeys="false" keyProperty="id">
        insert into users(name,email) values(#{name},#{email})
    </insert>
</mapper>

我该如何解决?

2 个答案:

答案 0 :(得分:0)

错误是resultType id必须是唯一的。

 <resultMap id="UserResultMap1" type="hello.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="email" property="email" />
    </resultMap>
 <resultMap id="UserResultMap2" type="hello.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="email" property="email" />
    </resultMap>

答案 1 :(得分:0)

建议使用 resultType 而不是 resultMap 。像这样

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.oa.dao.Borrow_DeviceDao">
<insert id="addBorrow_Device"
    parameterType="com.example.oa.domain.Borrow_device">
    INSERT INTO
    borrow_device
    (borrow_device_deviceid,employeeid,borrow_device_date)
    VALUES
    (#{borrow_device_deviceid},#{employeeid},now())
</insert>
<update id="updateBorrow_Device"
    parameterType="com.example.oa.domain.Borrow_device">
    update
    borrow_device set
    borrow_device_deviceid=#{borrow_device_deviceid},employeeid=#{employeeid},borrow_device_date=now()
    where borrow_device_id=#{borrow_device_id}
</update>
<delete id="deleteBorrow_Device"
    parameterType="com.example.oa.domain.Borrow_device">
    delete from
    borrow_device where
    borrow_device_id=#{borrow_device_id}
</delete>
<select id="getallBorrowDeviceInfo"
    resultType="com.example.oa.domain.Borrow_device">
    select * from
    borrow_device
</select>