Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mybatis对于使用了 useGeneratedKeys="true" keyProperty="id"属性的insert sql无法mock。 #14

Open
ElesG opened this issue Aug 28, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@ElesG
Copy link
Contributor

ElesG commented Aug 28, 2019

问题描述

useGeneratedKeys 取值范围true、false 默认值是:false。 含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中
keyProperty 取id的key值

使用了这类两个属性的insert sql会对传入的对象进行修改,将插入完成后的id设回给传入的参数。
然后调用了这个方法后,大家可以直接拿执行插入操作的对象中的id进行后面的操作。
如:

public Integer addCat(){
     Cat cat = new cat();
     cat.setName("miao");
     catMapper.insert(cat);
     return cat.getId();
}

由于录制时只关注mybatis的execute方法的入参和返回,没有关注传入参数的改变,导致在上面代码的场景中会出现回放失败的情况。

@zhaoyb1990 zhaoyb1990 added the enhancement New feature or request label Aug 29, 2019
@zhaoyb1990
Copy link
Contributor

该问题是"及时序列化"引入的问题;为了保证能够录制到原始的入参和返回值,避免被后续流程篡改导致的回放问题;

对于这种问题;可以自己实现MockInterceptor来适配,把response或者特定的参数填充到请求中

@ElesG
Copy link
Contributor Author

ElesG commented Aug 29, 2019

该问题是"及时序列化"引入的问题;为了保证能够录制到原始的入参和返回值,避免被后续流程篡改导致的回放问题;

对于这种问题;可以自己实现MockInterceptor来适配,把response或者特定的参数填充到请求中

如果通过MockInterceptor来实现,怎么样能够获取到这个特定参数呢?
因为在mybatis中这类插入操作每次设置回原对象的id都不同,且返回值是表示操作成功或失败,而不是生成的id。
在录制的时候在处理RETURN类事件时,RETURN事件中并没有记录当前入参的状态,只有返回值的情况下,怎么获取到这个被插入到对象里的id呢?

<insert id="insertProject" parameterType="com.xx.xx" useGeneratedKeys="true" keyProperty="id">

@zhaoyb1990
Copy link
Contributor

该问题是"及时序列化"引入的问题;为了保证能够录制到原始的入参和返回值,避免被后续流程篡改导致的回放问题;
对于这种问题;可以自己实现MockInterceptor来适配,把response或者特定的参数填充到请求中

如果通过MockInterceptor来实现,怎么样能够获取到这个特定参数呢?
因为在mybatis中这类插入操作每次设置回原对象的id都不同,且返回值是表示操作成功或失败,而不是生成的id。
在录制的时候在处理RETURN类事件时,RETURN事件中并没有记录当前入参的状态,只有返回值的情况下,怎么获取到这个被插入到对象里的id呢?

<insert id="insertProject" parameterType="com.xx.xx" useGeneratedKeys="true" keyProperty="id">

对于mybatis这种情况,如果MockInterceptor不能解决; 可以给插件新增一个能力,选择序列化时机,比如:默认在before事件序列化,mybatis这种可以选择在return进行序列化,就能解掉这个问题

@ElesG
Copy link
Contributor Author

ElesG commented Aug 29, 2019

该问题是"及时序列化"引入的问题;为了保证能够录制到原始的入参和返回值,避免被后续流程篡改导致的回放问题;
对于这种问题;可以自己实现MockInterceptor来适配,把response或者特定的参数填充到请求中

如果通过MockInterceptor来实现,怎么样能够获取到这个特定参数呢?
因为在mybatis中这类插入操作每次设置回原对象的id都不同,且返回值是表示操作成功或失败,而不是生成的id。
在录制的时候在处理RETURN类事件时,RETURN事件中并没有记录当前入参的状态,只有返回值的情况下,怎么获取到这个被插入到对象里的id呢?

<insert id="insertProject" parameterType="com.xx.xx" useGeneratedKeys="true" keyProperty="id">

对于mybatis这种情况,如果MockInterceptor不能解决; 可以给插件新增一个能力,选择序列化时机,比如:默认在before事件序列化,mybatis这种可以选择在return进行序列化,就能解掉这个问题

但是采取这个 方式的话,在ParameterMatchMockStrategy中就会因为参数与入参不匹配而导致回放失败了呀。

@ElesG
Copy link
Contributor Author

ElesG commented Aug 30, 2019

回归版本:755806a
从本次回归版本的代码中没有看到MockInterceptor这块的实现,�使用本次回归版本进行回放的时候会报错no matching invocation found

@zhaoyb1990
Copy link
Contributor

yeap... 拦截器的代码没写,现在找到不到子调用的是预期内的; 因为录制的时候有ID,回放的时候没有

@ElesG ElesG mentioned this issue Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants