ibatis 获取新增id mysql_ibatis插入数据返回ID的方法

news/2024/7/24 9:27:58 标签: ibatis 获取新增id mysql

很基础的知识了,贴过来备忘一下

主要就是利用seelctkey来获取这个ID值,但是oracle和mysql的区别还是很大的

oracle的用法

1 <insert id="insertOperation">

2 <selectKey resultClass="long" keyProperty="Id" >

3 select operation_seq.nextval as id from desc

4 </selectKey>

5 insert into test(id,name,desc) values (#id#,#name#,#desc#)

6 </insert>

oracle主要通过序列来返回insert的ID号,所以selectkey主要做的操作是从序列中拿到下一个值

mysql的用法

1 <insert id="insertTopic" parameterClass="topic">

2 insert into test(ID, NAME, DESC) values (#ID#, #NAME#, #DES#)

3 <selectKey resultClass="string" keyProperty="id">

4 select last_insert_id() as ID from test limit 1

5 </selectKey>

6 </insert>

msyql主要利用了last_insert_id这个函数来获取最大的id值

/**

* 2012.09.29

*/

今天在做selectkey时各种报错,这里列几个遇到的低级错误,分先一下

ibatis报ora-01475,无效的主机,绑定的变量名:这个很低级,字段中间忘记写逗号了。

主机无法启动,没有成功的生成bean,恐怕是ibatis的变量不是bean的字段

ibatis报ora-000904,无效的标示符,以为name是关键字,加上""也不行,尼玛最后发现生成表的时候name前面有个空格。。。

ibatis报ora-00984,列在此处不允许,一看其中的字符串用的是双引号,改成单引号即可。

启动时候报NullPointerException,selectkey增加属性type="pre"(针对oracle)

ibatis报无效的列索引,一般是查询变量的名字写错,或者多写少些了,我的问题是我用--注释,结果不认识了。。。

ibatis报无效的列类型,一般是插入了null而非空字符串。

/**

*2012.10.18

*/

今天又遇到一种错误类型There is no READABLE property named

我得解决方法是把报错的那个字符在ibatis里去掉了。

解决方案在这里 http://bakcom.iteye.com/blog/226036

补充一篇文章 http://hi.baidu.com/daniel_tu/item/6f5ce2da86de8452d73aae00

/**

* 2012.11.6

*/

今天遇到了一个执行sql时候ORA-00911: 无效字符的错误

解决方案很简单。sql的最后有个分号,去掉分号就好了

总结一下,之前没用到ibatis的时候,对于mysql还得写一个事务来封装,先插入,然后select max()下,太麻烦了。还是ibiats强大。


http://www.niftyadmin.cn/n/1469207.html

相关文章

SeleniumJava vs testcafe javascript第一部分

A comparison between Selenium and TestCafeSelenium和TestCafe的比较 Many articles compare Selenium and TestCafe test frameworks, but few of them compare these frameworks’ specific features through task execution. In this article, I will show you how to ex…

java遍历list去重_Java中List集合去除重复数据的方法汇总

List集合概述List集合是一个元素有序(每个元素都有对应的顺序索引&#xff0c;第一个元素索引为0)、且可重复的集合。List集合常用方法List是Collection接口的子接口&#xff0c;拥有Collection所有方法外&#xff0c;还有一些对索引操作的方法。void add(int index, E element…

php 转换成string,php中object如何转换成string

php中object如何转换成string发布时间&#xff1a;2020-09-01 16:03:02来源&#xff1a;亿速云阅读&#xff1a;95作者&#xff1a;小新这篇文章主要介绍php中object如何转换成string&#xff0c;文中介绍的非常详细&#xff0c;具有一定的参考价值&#xff0c;感兴趣的小伙伴们…

javascript面试_我的模拟面试中的5个JavaScript技术问题

javascript面试As I prepare for the many technical interviews ahead of me, I decided to share some of the questions that arose during my recent mock technical interview. In the end they all turned out to be much easier than I had thought and gave me more co…

php 图片无法删除,php如何删除指定图片

php删除指定图片的方法&#xff1a;首先读取数据库头像的URL地址&#xff1b;然后获取URL地址的有效字段&#xff1b;接着设置file文件路径&#xff1b;最后通过unlink函数删除图片文件即可。推荐&#xff1a;《PHP视频教程》php 删除服务器指定目录图片使用场景&#xff1a;新…

为什么我不能用tiktok_tiktok如何给我完美的编码方面的项目

为什么我不能用tiktokAs a computer science student, I am always looking for another cool side project to keep me busy. These ideas for side projects can come from anywhere; sometimes from the places you least expect.作为计算机科学专业的学生&#xff0c;​​我…

php异步批量删除,php中ajax实现批量删除的方法

php中ajax实现批量删除的方法发布时间&#xff1a;2020-08-26 11:50:08来源&#xff1a;亿速云阅读&#xff1a;109作者&#xff1a;小新这篇文章主要介绍了php中ajax实现批量删除的方法&#xff0c;具有一定借鉴价值&#xff0c;需要的朋友可以参考下。希望大家阅读完这篇文章…

javascript测试_用javascript测试

javascript测试How Writing Tests for Your Code Can Help Improve Your Code为您的代码编写测试如何帮助您改善代码 An important part of the process of writing clean, effective code is writing and running tests for your code. As a relative newcomer to the field …