Mybatis 主键返回

1.1 xml 配置主键返回


<insert id="save" parameterType="orders" >
    <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER" >
        select last_insert_id();
    selectKey>
    
    insert into orders value(null, #{ordertime}, #{total}, #{uid})
insert>

1.2 注解配置主键返回

@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("insert into orders values(null, #{ordertime}, #{total}, #{uid})")
Integer insert(Orders orders);

1.3 获取主键

// 实体类对象,属性值省略
Orders orders;
// 插入,返回值为影响的行数
Integer rows = sqlSession.getMapper(OrdersDao.class).insert(orders);
// id 需要通过实体类对象获取
Long id = orders.getId()

你可能感兴趣的:(Mybatis,mybatis,sql,后端)