简体   繁体   English

如何在MyBatis插入中使用Mysql触发器生成的值填充对象属性

[英]How to populate object property with value generated by Mysql Trigger in MyBatis Insert

I can't get the value back from function. 我无法从函数取回值。 It does an insert on a table and must return a number. 它在表上插入,并且必须返回数字。 The data is insert correctly, but the number returned is always null. 数据已正确插入,但返回的数字始终为null。

Mysql Mysql的

create table driver_order (
  id int(11) unsigned NOT NULL AUTO_INCREMENT,
  area_start varchar(200),
  area_end varchar(200),
  order_number varchar(200),
  create_user varchar(200),
  primary key (id)
);

DELIMITER $$
CREATE TRIGGER seq_driver_order_number BEFORE INSERT ON driver_order
FOR each ROW
BEGIN
    DECLARE seq_type INT(10);
    SET seq_type = getUserNo(NEW.create_user);
    SET NEW.order_number = getNextCommSequence("motor", seq_type);
END$$
DELIMITER ;

Mybatis MyBatis的

<insert id="insertOrder" useGeneratedKeys="true" keyProperty="id" parameterType="DriverOrder">
    INSERT INTO 
    DRIVER_ORDER(ID,ORDER_NUMBER,AREA_START,AREA_END,CREATE_USER,CREATE_TIME) 
    VALUES
      (#{id},
       #{orderNumber,jdbcType=VARCHAR},
       #{areaStart,jdbcType=VARCHAR},
       #{areaEnd,jdbcType=VARCHAR},
       #{createUser,jdbcType=VARCHAR},
       now())
</insert>

The return Object all attributes have correctly value include id, except order_number which TRIGGER set value is return null. 返回对象的所有属性都具有正确的值,包括id,但order_number除外,其中TRIGGER设置的值返回null。 Is there something wrong? 有什么不对?

The problem is not with a trigger but how to make mybatis get value generated on mysql side during record insertion. 问题不在于触发器,而是如何使mybatis获取在记录插入期间在mysql端生成的值。 Mybatis is rather simple tool in sense that you can't specify properties to columns mapping and everything else happens automagically. 从某种意义上讲,Mybatis是一种非常简单的工具,您无法为列映射指定属性,其他所有事情都会自动发生。

Mybatis core is sql queries not properties-to-columns mappings like in hibernate for example. Mybatis的核心是sql查询,而不是像hibernate这样的属性到列的映射。 So mybatis only executes queries and simplifies setting parameters and construction objects from query result. 因此,mybatis仅执行查询,并简化了查询结果中的设置参数和构造对象。

Nevertheless starting from version 3.2.6 you can use selectKey to get several values and set properties in the object being inserted. 但是,从版本3.2.6开始,您可以使用selectKey获取多个值并在要插入的对象中设置属性。 If you combine this with last_insert_id() you can get what you need. 如果将此与last_insert_id()结合使用, 可以得到所需的内容。 For your case it is done like this: 对于您的情况,它是这样完成的:

<insert id="insertOrder" parameterType="DriverOrder">
  <selectKey keyProperty="id,orderNumber" keyColumn="ID,ORDER_NUMBER" order="AFTER" resultType="java.util.Map">
    SELECT ID,ORDER_NUMBER FROM DRIVER_ORDER where ID = last_insert_id()
  </selectKey>
  INSERT INTO 
   DRIVER_ORDER(ID,ORDER_NUMBER,AREA_START,AREA_END,CREATE_USER,CREATE_TIME) 
  VALUES
   (#{id},
    #{orderNumber,jdbcType=VARCHAR},
    #{areaStart,jdbcType=VARCHAR},
    #{areaEnd,jdbcType=VARCHAR},
    #{createUser,jdbcType=VARCHAR},
    now())
</insert> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM