MyBatis中的selectKey的用法(使用xml标签的样式)+ 一直返回1怎么解决

在insert语句中,在MySQL中使用函数来自动生成插入表的主键(设置id自动递增),而且需要方法能返回这个生成主键。使用myBatis的selectKey标签可以实现这个效果。

使用注解的方式还是xml的方式,根据具体情况即可,使用的方式基本一样,就是语句的编写有一些差距。本篇文章就是以xml的方式进行配置的使用的。
一、了解selectKey的基本属性

属性 描述
keyProperty selectKey 语句结果应该被设置的目标属性
resultType 结果的类型。MyBatis 通常可以算出来,但是写上也没有问题。MyBatis 允许任何简单类型用作主键的类型,包括字符串
order 这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素-这和如 Oracle 数据库相似,可以在插入语句中嵌入序列调用
statementType (使用注解放方式时,在这个属性中加入需要的SQL语句)和前面的相 同,MyBatis 支持 STATEMENT ,PREPARED 和CALLABLE 语句的映射类型,分别代表 PreparedStatement 和CallableStatement 类型

二、基本使用XML方式
(一)ID使用自动递增的方式,在执行插入后,获取插入的ID(重点)
我是用的一个场景是,两张表,一张表中设置的id是自动递增的,另一张表中需要第一张表的id,然后需要插入同时插入数据到两个表中。这时候在插入完第一个表的时候就需要将第一个生成的id取出,给第二个表使用。

 	<insert id="insert">
        <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO order_info
        (user_id,product_id,address_id,product_name,product_count,product_price,order_channel,status,create_date)
         VALUES
        (#{
    
    user_id},#{
    
    product_id},#{
    
    address_id},#{
    
    product_name},#{
    
    product_count},#{
    
    product_price},#{
    
    order_channel},#{
    
    status},#{
    
    create_date})
    </insert>

mapper接口中,方法如下:

long insert(Order order);

这样插入之后的就应该返回刚插入的id!

如果遇到返回的参数一直是1,怎么解决?

我在使用时一直返回1,后来查资料,原来是传递的参数是一个order类 ,是一个bean,所以这个返回的参数已经自动注入到bean中对应的变量里了,在这里就是注入到order的id变量中了,这时候就需要在service层通过order.getId()进行获取使用。

service层使用的代码如下

  @Transactional
    public Order createOrder(User user, ProductVo productVo) {
    
    
        Order order = new Order();
       /* 跳过中间设置参数代码*/
        long orderId = orderMapper.insert(order);
        MiaoshaOrder miaoshaOrder = new MiaoshaOrder();
        miaoshaOrder.setOrder_id(order.getId());//这里就是取出了上面刚插入的id值
      	/* 跳过中间设置参数代码*/
        orderMapper.insertMiaoshaOrder(miaoshaOrder);
        return order;
    }

(二)生成uuid然后进行插入

 <insert id="insert">
        <selectKey resultType="java.lang.Long" keyProperty="id" order="BEFORE" >
            SELECT uuid()
        </selectKey>
        INSERT INTO order_info
        (user_id,product_id,address_id,product_name,product_count,product_price,order_channel,status,create_date)
         VALUES
        (#{
    
    user_id},#{
    
    product_id},#{
    
    address_id},#{
    
    product_name},#{
    
    product_count},#{
    
    product_price},#{
    
    order_channel},#{
    
    status},#{
    
    create_date})
    </insert>

三、使用注解的方式

	@Insert("insert into order_info(user_id, goods_id, goods_name, goods_count, goods_price, order_channel, status, create_date)
	values(#{
    
    userId}, #{
    
    goodsId}, #{
    
    goodsName}, #{
    
    goodsCount}, #{
    
    goodsPrice}, #{
    
    orderChannel},#{
    
    status},#{
    
    createDate} )")
	@SelectKey(keyColumn="id", keyProperty="id", resultType=long.class, 
		before=false, statement="select last_insert_id()")
	public long insert(OrderInfo orderInfo);

猜你喜欢

转载自blog.csdn.net/Wangdiankun/article/details/106461414
今日推荐