-
Notifications
You must be signed in to change notification settings - Fork 10
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拦截器实现了字段值的填充功能,想问下大佬有没有更好的建议? #11
Comments
这个图没找到哪里可以获取入参 |
先提供一个具体方法,基于现有接口可以实现的。 package io.mybatis.provider.parameter;
import io.mybatis.provider.EntityTable;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public interface ParameterProcessor {
/**
* 对 parameterObject 参数进行修改
*
* @param sqlSource sqlSource
* @param entity 实体类
* @param ms MappedStatement
* @param context 当前接口和方法信息
* @param parameterObject 参数
*/
void process(SqlSource sqlSource, EntityTable entity, MappedStatement ms, ProviderContext context, Object parameterObject);
} 基于现有 package io.mybatis.provider.parameter;
import io.mybatis.provider.EntityTable;
import io.mybatis.provider.SqlSourceCustomize;
import io.mybatis.provider.util.ServiceLoaderUtil;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import java.util.List;
public class ParameterSqlSourceCustomize implements SqlSourceCustomize {
private List<ParameterProcessor> processors = ServiceLoaderUtil.getInstances(ParameterProcessor.class);
public void process(SqlSource sqlSource, EntityTable entity, MappedStatement ms, ProviderContext context, Object parameterObject) {
for (ParameterProcessor processor : processors) {
processor.process(sqlSource, entity, ms, context, parameterObject);
}
}
@Override
public SqlSource customize(SqlSource sqlSource, EntityTable entity, MappedStatement ms, ProviderContext context) {
if (processors.isEmpty()) {
return sqlSource;
}
return parameterObject -> {
process(sqlSource, entity, ms, context, parameterObject);
return sqlSource.getBoundSql(parameterObject);
};
}
} |
注意SPI需要相应的service配置。 基于上述接口的实现: package io.mybatis.provider.parameter;
import io.mybatis.provider.EntityTable;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
public class BaseParameterProcessor implements ParameterProcessor {
@Override
public void process(SqlSource sqlSource, EntityTable entity, MappedStatement ms, ProviderContext context, Object parameterObject) {
if (ms.getSqlCommandType() == SqlCommandType.INSERT || ms.getSqlCommandType() == SqlCommandType.UPDATE) {
//比如新增插入创建人、创建时间
//更新时更新人、更新时间
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,我基于mybatis拦截器实现了字段值的填充功能,想问下大佬有没有更好的建议?我看新版本提供了MsCustomize定制化生成主键的接口,但是接口方法customize没有当前sql对应的入参,没法做字段值填充。
The text was updated successfully, but these errors were encountered: