MyBatis:ClassCastException:java.lang.Integer 不能转换为 java.lang.Boolean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13263379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:03:58  来源:igfitidea点击:

MyBatis: ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean

javabooleanmybatisclasscastexception

提问by Arturas M

I'm getting this exception in MyBatis while trying to execute the update statement(UpdateUser):

我在尝试执行更新语句 (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.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.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access
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() {
}
0(ParentRunner.java:50) at org.junit.runners.ParentRunner.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)

My Data type User with which I'm working looks like this:

我正在使用的数据类型用户如下所示:

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();

with the apropriate gettters and setters of course.

当然还有适当的 getter 和 setter。

My UserMapper.java interface for mybatis looks as follows:

Mybatis 的 UserMapper.java 界面如下所示:

 <?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>

}

}

And my UserMapper.xml looks like this:

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

##代码##

as I've mentioned the exception is thrown when updateUser is called. The strange thing is I can't even see anything Integer related here. I mean I even tried testing with just updating one field. It's all the same. I don't get the exception only if the query is totally removed, I would get the query error or something error...

正如我所提到的,在调用 updateUser 时会抛出异常。奇怪的是我什至看不到任何与 Integer 相关的东西。我的意思是我什至尝试只更新一个字段进行测试。全部都是一样。仅当查询被完全删除时,我才不会收到异常,我会收到查询错误或某些错误...

Any suggestions? Thanks in advance.

有什么建议?提前致谢。

Edit: if people are voting my question down without saying anything, they should at least tell what was wrong with the question, that would be more helpful... I haven't posted this question out of fun. Indeed I'm kinda stuck on this for some time now...

编辑:如果人们在没有说任何话的情况下投票否决我的问题,他们至少应该告诉我问题出了什么问题,这会更有帮助......我发布这个问题并不是为了好玩。事实上,我现在有点坚持这个......

回答by Rohit Jain

UPDATEquery does not return a booleantype. It returns an integerdepicting the number of rowssuccessfully updated. And you have given a return type of booleanto your updateUserwhich is using UPDATEquery.

UPDATE查询不返回boolean类型。它返回一个integer描述rows成功更新的数量。而你给的返回类型booleanupdateUser这是使用UPDATE查询。

Change the return type of updateUserto int. I hope that would work then.

将 的返回类型更改updateUserint。我希望那会奏效。