我想将Post域插入MySQL db。
发布域名是这样的:
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映射器是这样的:
<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不能强制转换为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)
请帮忙!!
答案 0 :(得分:0)
制作postId
类型的Long
。我最近遇到了同样的错误,请看http://code.google.com/p/mybatis/issues/detail?id=378