浏览 16132 次
锁定老帖子 主题:IBatis简单实现(附主键自动生成)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (17) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-03
我第一次接触ibatis这个框架,感觉好可以啊,我以后会常来的。
|
|
返回顶楼 | |
发表时间:2008-12-03
最后修改:2008-12-03
孤风浪客 写道 这些主键生成机制都是用数据本身机制生成的,有没有办法自定义主键生成机制呢。比如说用UUID来写主键?
PS:不是用Pojo中的setId(String str)这样子将生成的UUID赋值给主键字段。 我用oracle <insert id="insertUser" parameterClass="user"> INSERT INTO USER_INFO ( id, name, sex) VALUES ( sys_guid(), #name#, #sex# ) </insert> 预生成(如Oracle)和后生成两种类型(如MS-SQL Server)。下面是两个例子: < !—Oracle SEQUENCE Example --> <insert id="insertProduct-ORACLE" parameterClass="com.domain.Product"> <selectKey resultClass="int" keyProperty="id" > SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL </selectKey> insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#) </insert> <!— Microsoft SQL Server IDENTITY Column Example --> <insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product"> insert into PRODUCT (PRD_DESCRIPTION) values (#description#) <selectKey resultClass="int" keyProperty="id" > SELECT @@IDENTITY AS ID </selectKey> </insert> |
|
返回顶楼 | |
发表时间:2009-07-14
楼主你的例子有错误。
com.ibatis.common.beans.ProbeException: Could not set property 'id' to value 'null' for com.jack.ibatis.pojo.Person. Cause: java.lang.IllegalArgumentException at com.ibatis.common.beans.ComplexBeanProbe.setProperty(ComplexBeanProbe.java:368) at com.ibatis.common.beans.ComplexBeanProbe.setObject(ComplexBeanProbe.java:234) at com.ibatis.common.beans.GenericProbe.setObject(GenericProbe.java:90) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.executeSelectKey(SqlMapExecutorDelegate.java:427) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:387) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82) at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:58) at test.com.jack.ibatis.TestPerson.testInsert(TestPerson.java:26) 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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Caused by: java.lang.IllegalArgumentException 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 com.ibatis.common.beans.MethodInvoker.invoke(MethodInvoker.java:17) at com.ibatis.common.beans.ComplexBeanProbe.setProperty(ComplexBeanProbe.java:356) ... 27 more 还有,你在帖子上的person.xml的配置中。 <!-- 添加用户 --> <insert id="insertPerson" parameterClass="person"> <!-- 配置Mysql主键自动增长 --> <selectKey keyProperty="id" resultClass="int"> SELECT LAST_INSERT_ID() as value @1 --------------------------------------- 上面的value是不是应该改为ID?? ---------------------------------------- </selectKey> <!-- 配置Oracle主键自动增长 <selectKey resultClass="int" keyProperty="id" type="pre"> <![CDATA[SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL]]> </selectKey>--> <!-- 配置MSServer主键自动增长 <selectKey resultClass="int" keyProperty="id"> <![CDATA[SELECT SCOPE_IDENTITY() AS ID ]]> </selectKey>--> <![CDATA[ INSERT PERSON(U_NAME,U_PASSWORD) VALUES(#username#,#password#) ]]> </insert> ---------------------------- 另外。对于自增长主键来说,mysql和sql的自增长ID应该属于后发型,即 type="post"但你没有配置,并且把selectKey 写到前面去了,这样的话应该会报错吧?我猜报ID无法SET的原因就在于此吧?? -------------------------- 本人其实也写过类似的例子,但始终无法搞定的就是插入后获得自增长ID值 的问题。网上的方法基本都试过,但获得依旧为NULL,不知道 为什么。。。哭。。。。 |
|
返回顶楼 | |