1
我想插入Post域到MySQL數據庫。mybatis的自動生成密鑰的java數據類型
後域是這樣的:
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>
我不知道爲什麼克enerated關鍵這名字是「帖子ID」總有例外,這是「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)
請幫忙!
mapper方法的返回類型不是long類型。所以...... – Seong 2012-08-21 03:58:58
是否可以發佈你的調用這個插入的Java代碼。這一定是非常簡單的問題。 – Nishant 2012-08-21 04:07:39