1

我想将 Post 域插入 MySQL 数据库。

后域是这样的:

public class Post {
    private long postId;
    private String title;
    String content;
    private String pictureId;
    private Picture picture;
    private int authorId;   
    private User author;
    private Date postTime;
    private int status;
    private int commentCount;

}

db 表是这样创建的:

CREATE TABLE `tb_posts` (
`post_id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`content` varchar(2000) COLLATE utf8_unicode_ci DEFAULT NULL,
`picture_id` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`author_id` int(11) DEFAULT NULL,
`post_time` datetime DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`comment_count` int(11) DEFAULT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

mybatis的mapper是这样的:

<insert id="insert" parameterType="post">

    INSERT INTO tb_posts
    (
        title, content, picture_id, author_id, post_time, status, comment_count
    ) VALUES
    (
        #{title}, #{content}, #{pictureId}, #{authorId}, #{postTime}, #{status}, #{commentCount}
    )
    <selectKey keyProperty="postId" resultType="long" order="BEFORE">
    SELECT LAST_INSERT_ID() AS postId;
    </selectKey>
</insert>

我想知道为什么生成的名为“postId”的密钥总是有异常“java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long”

整个异常是这样的:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at $Proxy14.insert(Unknown Source)
at me.joyo.mate.core.service.impl.PostServiceImpl.insert(PostServiceImpl.java:44)
at me.joyo.mate.core.test.PostTester.insert(PostTester.java:50)
at me.joyo.mate.core.test.PostTester.test(PostTester.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)

请帮忙 !!

4

1 回答 1

0

制造postId类型Long。我最近遇到了同样的错误,请参见这里http://code.google.com/p/mybatis/issues/detail?id=378

于 2012-08-17T17:34:28.067 回答