MyBatis:ClassCastException:java.lang.Integer无法强制转换为java.lang.Boolean

时间:2012-11-07 05:01:24

标签: java boolean mybatis classcastexception

我在尝试执行更新语句(UpdateUser)时在MyBatis中收到此异常:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean
at $Proxy6.updateUser(Unknown Source)
at com.nortal.pirs.persistence.dbmybatis.UserDaoMyBatisImpl.updateUser(UserDaoMyBatisImpl.java:60)
at com.nortal.pirs.test.persistence.UserDaoAbstractTest.testUpdateUser(UserDaoAbstractTest.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的数据类型我正在使用的用户看起来像这样:

public class User {
private String firstName = "";
private String lastName = "";
private String personCode = "";
private Date birthDate = new Date();
private Gender gender = Gender.MALE;
private String email = "";
private String password = "";
private UserState userState = UserState.UNAPPROVED;

public User() {
}

当然是适当的傻瓜和制定者。

mybatis的My UserMapper.java界面如下所示:

public interface UserMapper {

public User getUserByEmail(String email);   

public void addUser(User user);

public void removeUser(String email);

public int getNumberOfUsers();

public int userExists(String email);

public boolean updateUser(@Param ("oldUser") User oldUser, @Param("newUser") User newUser);

public Map<String, User> getAllUsers();

}

我的UserMapper.xml看起来像这样:

 <?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.nortal.pirs.persistence.dbmybatis.UserMapper">

    <!-- QUESTION: Is the <cache /> needed as described in one of your examples? And what is it meant to do? -->

    <resultMap id="UserMap" type="com.nortal.pirs.datamodel.User">
        <result property="firstName" column="firstName"/>
        <result property="lastName" column="lastName"/>
        <result property="personCode" column="personCode"/>
        <result property="birthDate" column="birthDate" javaType="Date"/>
        <result property="gender" column="gender" javaType="com.nortal.pirs.datamodel.Gender"/>
        <result property="email" column="email"/>
        <result property="password" column="password"/>
        <result property="userState" column="userState" javaType="com.nortal.pirs.datamodel.UserState"/>        
    </resultMap>

    <select id="getUserByEmail" parameterType="String" resultMap="UserMap">
        SELECT 
        firstName,
        lastName,
        personCode,
        birthDate,
        gender,
        email,
        password,
        userState       
        FROM   USER 
        WHERE  email = #{email}
    </select>

    <insert id="addUser" parameterType="User" >
        INSERT INTO user

        (firstName,
        lastName,
        personCode,
        birthDate,
        gender,
        email,
        password,
        userState)

        VALUES
        (#{firstName},
        #{lastName},
        #{personCode},
        #{birthDate},
        #{gender},
        #{email},
        #{password},
        #{userState})        
    </insert>

    <delete id="removeUser" parameterType="String">
        DELETE FROM user
        WHERE email = #{email}
    </delete>


    <select id="getNumberOfUsers" resultType="int">
        SELECT
        COUNT(email)
        FROM USER
    </select>

    <select id="userExists" parameterType="String" resultType="int">
        SELECT CASE WHEN EXISTS (
            SELECT
            COUNT(email)
            FROM USER)
    </select>

    <update id="updateUser" parameterType="map">
        UPDATE user
        SET 
        firstName = #{newUser.firstName},
        lastName = #{newUser.lastName},
        personCode = #{newUser.personCode},
        birthDate = #{newUser.birthDate},
        gender = #{newUser.gender},
        email = #{newUser.email},
        password = #{newUser.password},
        userState = #{newUser.userState}
        WHERE email = #{oldUser.email}
    </update>

</mapper>

正如我所提到的,在调用updateUser时会抛出异常。奇怪的是我甚至看不到任何与此相关的Integer。我的意思是我甚至尝试只更新一个字段进行测试。这都一样。只有在完全删除查询时我才会得到异常,我会得到查询错误或错误...

有什么建议吗?提前谢谢。

编辑:如果人们在没有说什么的情况下投票我的问题,他们至少应该说出这个问题有什么问题,这样会更有帮助......我没有发布这个问题。事实上,我现在已经坚持了一段时间......

0 个答案:

没有答案