- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 Java 应用程序中使用 iBatis 和 MySQL 5。
我有一个持久实体类
public class Entity {
private int id;
private Stirng property;
// setters and getters are omitted
}
插入新实体的操作如下:
<insert id="insert" parameterClass="MyEntity">
<selectKey resultClass="int" type="post" keyProperty="id" >
select LAST_INSERT_ID() as value
</selectKey>
{CALL insert_entity(#property#)}
</insert>
事务在存储过程中管理如下:
CREATE DEFINER=`user`@`%` PROCEDURE `insert`(IN p_property VARCHAR(255))
BEGIN
START TRANSACTION;
INSERT INTO entities (property) VALUES (p_property);
-- Do more stuff that requires transaction: update more tables etc.
COMMIT;
END;
我想要实现的是将新插入的实体 ID 返回到我的 Java 代码中。在没有并发数据库更新的情况下,上面的设置将完全按照我的要求进行。不清楚的部分是并发数据库更新会发生什么 - 即 iBatis 执行 selectKey
语句的确切时间和上下文是什么 - 我猜它不会在存储过程中定义的同一事务中执行,所以返回的id可能不是我想要的实体的id。
我能想到的唯一可能的解决方案是避免使用 selectKey
:
<insert id="insert" parameterClass="MyEntity">
{CALL insert_entity(#property#, #id,mode=OUT#)}
</insert>
从存储过程返回最后插入的 id:
CREATE DEFINER=`user`@`%` PROCEDURE `insert`(
IN p_property VARCHAR(255),
OUT p_id INTEGER(11),
)
BEGIN
START TRANSACTION;
INSERT INTO entities (property) VALUES (p_property);
SELECT LAST_INSERT_ID() INTO p_id;
-- Do more stuff that requires transaction: update more tables etc.
COMMIT;
END;
这个问题有没有更好的解决方案?
已编辑 LAST_INSERT_ID
的 MySQL 文档指出:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
所以看起来带有 selectKey
的原始解决方案在所有情况下都适用。但是,对于具有多个 INSERT
语句的复杂存储过程,第二种方法更安全。
最佳答案
首先,我必须声明一个显而易见的事实:您应该认真尝试避免在存储过程中进行自己的事务管理。
假设这是您唯一的选择,我会说后一种解决方案是我的首选,因为任何开发人员都清楚 ID 是从事务中返回的。
关于mysql - iBatis selectKey 和事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5388052/
有谁知道当省略了flushInterval时iBATIS cacheModel的行为是什么,比如说缓存类型是MEMORY?我希望它只是将缓存结果无限期地保留在内存中。我有一组结果,如果不重新启动服务器
我需要一个解决方案来取消长时间运行的选择语句。我正在使用 Spring 3.0.2、iBatis 2.3.0 和 Oracle 10g。我设法让它与普通 JDBC 一起工作,但是因为选择是通过高级搜索
我有两个表的数据库 post: id post_name post_desc files: file_id file_name pos
Spring4.2.0不支持ibatis。我的项目正在从 Spring 3.2.4 升级到 Spring 4.2。当我用谷歌搜索时,我发现 spring4.2 与 mybatis 的集成示例,但没有与
根据用户指南,我可以使用文件路径而不是资源: // Using classpath relative resources // Using url fully qualified pat
我有一个 iBatis 查询,例如 但是像 lowerLimit、upperLimit、maxLowerLimit、maxUpperLimit 和 state 这样的字段可能为 null,
我写了一个查询:- select userid,endpointarns from t_user_actives where sid=#{sid} and (userid i
我在 weblogic 10.3.6 中使用 spring 3.2.0 和 ibatis 2.3.4在 weblogic 中部署时。 我收到此 NoSuchMethodError 如下: User d
假设我有一个查询 getUser有两个参数 - 用户名和密码。我想要一个像这样的映射器方法: public UserBean getUser(String userName, String passw
我在生产应用程序中使用的是iBatis-2.3.4.726。我想迁移生产应用程序以使用MyBatis。 在迁移过程中我需要考虑哪些要点? 是否有任何配置更改,或者MyBatis支持iBatis配置作为
我试图将startSequenceId,stopSequenceId和orderNumber传递到SQL映射中,但是,我不想使用类型化的对象,即parameterType="com.abc.Order
要删除的字符串ID为 复制代码代码如下: string SDSALES_IDString = 1,2,3,4,5,6,7 //转成Array Array a
我目前正在使用 ibatis 返回一些 pojo,一切都很好。 我的问题是: 我必须从表中返回 1 行,就像 3 个字段一样,我不想为它创建一个 pojo。我只想运行查询并获取 3 个值。有没有简单的
我在 iBatis 中有几个嵌套的 ResultMaps,它们具有完全相同的数据库列名。这会导致歧义,并导致为不同的数据库表检索到不正确的结果。 例如,`
现有代码库的 Java 升级和 Wildfly 升级已完成。在具有 Wildfly 服务器的 Windows 上,应用程序已正确部署。在具有 Wildfly 服务器的 Unix 上,应用程序部署失败,
我在 MyBatis v3 映射器 xml 中动态生成 where 子句。不过加括号确实很麻烦。有没有更简单的方法来处理这个问题而不使用 if 语句?
我现在的处境很复杂。我需要向 iBATIS select 传递三个参数,其中 2 个是 String ,1 个是 List 。 列表看起来像: List userList=new List(); pu
我有一个 select 语句,它返回相同数据类型 (VARCHAR) 的值列表。结果介于 1 到 6 行之间。我使用 queryForList() 并将响应存储在 List 对象中。执行时出现错误 -
谁能告诉我这是怎么回事?我有两个程序和两个映射。一个工作正常,另一个失败。这个工作正常:
在我的项目中,我们使用springmvc、spring和ibatis框架,问题是:在我的 dao 代码中是: @Override public Integer insertAdzoneEnvInfoB
我是一名优秀的程序员,十分优秀!