当前使用版本(必填,否则不予处理)
3.4.0
该问题是如何引起的?(确定最新版也有问题再提!!!)
设置自动填充后,在xml中设置了if标签的字段依然没有值,直接写在sql上的可以设置值,版本3.4.2依然存在这个问题
期望
请问是否能够通过设置来实现这个需求,或者必须不能存在if标签
Comment From: lhtcactus
public MybatisParameterHandler(MappedStatement mappedStatement, Object parameter, BoundSql boundSql) {
this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
this.mappedStatement = mappedStatement;
this.configuration = mappedStatement.getConfiguration();
this.sqlCommandType = mappedStatement.getSqlCommandType();
this.parameterObject = processParameter(parameter);
//在上面填充完参数后再重新获取boundSql
this.boundSql = mappedStatement.getBoundSql(parameterObject);
//替换boundSql
replaceBoundSql(this.boundSql,boundSql);
}
private void replaceBoundSql(BoundSql newBoundSql,BoundSql oldBoundSql) {
try {
Field[] fields = BoundSql.class.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(oldBoundSql,field.get(newBoundSql));
}
}catch (Exception e){
throw new RuntimeException(e);
}
}
尝试修改MybatisParameterHandler部分代码后可以实现我的需求,请问有没有更好的方式
Comment From: qmdx
https://baomidou.com/guide/auto-fill-metainfo.html
注解填充字段 @TableField(.. fill = FieldFill.INSERT) 生成器策略部分也可以配置!
Comment From: lhtcactus
自己写的insert/update语句如果有if标签,自动填充也会失败
Comment From: lhtcactus
public MybatisParameterHandler(MappedStatement mappedStatement, Object parameter, BoundSql boundSql) {
this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
this.mappedStatement = mappedStatement;
this.configuration = mappedStatement.getConfiguration();
this.sqlCommandType = mappedStatement.getSqlCommandType();
this.parameterObject = processParameter(parameter);
//如果是insert||update 执行sql重新编译,因为可以需要进行参数填充
if(SqlCommandType.INSERT == this.sqlCommandType || SqlCommandType.UPDATE == this.sqlCommandType){
this.boundSql = mappedStatement.getBoundSql(parameterObject);
replaceBoundSql(this.boundSql,boundSql);
}else{
this.boundSql = boundSql;
}
}
不过需要换一个方式,不然在使用分页连接器时会报错