mybatis向oracle数据库中插入数据时,需要返回主键值, 20C

这是mapper文件里的插入语句


insert into TEMPLATE (ID, ALLOWIP, BACKGROUNDCOLOR,
BUSINESS1, BUSINESS2, BUSINESS3,
COPYRIGHT, DEFAULTTEMPLATE, HEADERUPLOAD,
IPLOCKTIME, LOGMANAGEMENT, LOGINMODEL,
MAXFAILURES, PWLENGTH, PWVALIDPERIOD,
STATE, UPLOADMAX, UPLOADTYPE
)
values (ID, #{allowip,jdbcType=VARCHAR}, #{backgroundcolor,jdbcType=VARCHAR},
#{business1,jdbcType=VARCHAR}, #{business2,jdbcType=VARCHAR}, #{business3,jdbcType=VARCHAR},
#{copyright,jdbcType=VARCHAR}, #{defaulttemplate,jdbcType=DECIMAL}, #{headerupload,jdbcType=VARCHAR},
#{iplocktime,jdbcType=DECIMAL}, #{logmanagement,jdbcType=VARCHAR}, #{loginmodel,jdbcType=VARCHAR},
#{maxfailures,jdbcType=DECIMAL}, #{pwlength,jdbcType=VARCHAR}, #{pwvalidperiod,jdbcType=DECIMAL},
#{state,jdbcType=DECIMAL}, #{uploadmax,jdbcType=VARCHAR}, #{uploadtype,jdbcType=VARCHAR}
)

select SEQ_TEMPLATE.NEXTVAL AS ID from dual


#这是javabean
public class Template {
private Integer id;

private String allowip;

private String backgroundcolor;

private String business1;

private String business2;

private String business3;

private String copyright;

private Long defaulttemplate;

private String headerupload;

private Long iplocktime;

private String logmanagement;

private String loginmodel;

private Long maxfailures;

private String pwlength;

private Long pwvalidperiod;

private Long state;

private String uploadmax;

private String uploadtype;

这个是数据库表字段

图片说明
#报错信息,我希望它能返回主键值,拜托了
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.giantplus.ssm.po.Template' with value '69' Cause: java.lang.IllegalArgumentException: argument type mismatch
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy13.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:51)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy14.insert(Unknown Source)
at com.giantplus.ssm.dao.impl.TemplateDaoImpl.saveTemplate(TemplateDaoImpl.java:17)
at com.giantplus.ssm.service.impl.TemplateServiceImpl.saveTemplate(TemplateServiceImpl.java:20)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy18.saveTemplate(Unknown Source)
at com.giantplus.ssm.service.impl.TemplateServiceImplTest.testInsert(TemplateServiceImplTest.java:35)
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$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
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.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
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:292)
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)
Caused by: org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.giantplus.ssm.po.Template' with value '69' Cause: java.lang.IllegalArgumentException: argument type mismatch
at org.apache.ibatis.executor.keygen.SelectKeyGenerator.processGeneratedKeys(SelectKeyGenerator.java:90)
at org.apache.ibatis.executor.keygen.SelectKeyGenerator.processBefore(SelectKeyGenerator.java:46)

9个回答

![图片说明](https://img-ask.csdn.net/upload/201507/12/1436686626_140995.png)图片说明这是我的jar包 不知道是不是jar的问题啊

图片说明

现在项目用的ibatis 在插入是返回主键的方法时

<selectkey>select sequence.currval from dual</selectkey>   这是ibatis的配置 不知道mybatis区别如何

dingpf1209
离恨天蓑衣客 没用啊 你在mybatis里遇到过这种情况不
大约 5 年之前 回复

现在项目用的ibatis 在插入是返回主键的方法时

<selectkey>select sequence.currval from dual</selectkey>   这是ibatis的配置 不知道mybatis区别如何

插入语句只返回插入数的值和失败的值0,你可以写自己写个获取表序列的方法得到序列值,在当成参数插入,这时插入就不要在使用select sequence.currval from dual 要不你的主键值又会表了,比如:
String sql = " select " + seqName + ".nextval from dual";//seqName表的序列
return getSqlSession().selectOne(
"com.dao.mappe.Mapper.getSerialNumber", sql);

给你个例子,配置成这样子就好了
_

insert into user(userName,password,comment)

values(#{userName},#{password},#{comment})

_



insert into user(userName,password,comment)

values(#{userName},#{password},#{comment})

外边的insert标签被解析了

insert into user(userName,password,comment) values(#{userName},#{password},#{comment})

把圆括号当尖括号看(insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User")
insert into user(userName,password,comment)

values(#{userName},#{password},#{comment})

(/insert)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问