当前使用版本(必填,否则不予处理)
3.4.2
注:3.4.3因去除了PaginationInterceptor,项目老代码,无法升级
该问题是如何引起的?(确定最新版也有问题再提!!!)
有一个enabled字段, 数据库是pg,类库int16,jdbc类型为SMALLINT,代码中使用bool类型,因为写了一个typeHandler转换器,注册到实体和xml中。
@ApiModelProperty(value = "是否启用")
@TableField(value = "enabled", typeHandler = BooleanTypeHandler.class)
private boolean enabled;
````
xml:
直接使用以下语句是能触发typeHandler的:
LambdaQueryWrapper<SchedulerJobs> queryWrapper = Wrappers.lambdaQuery(request);
return this.dao.list(queryWrapper);
但是,自己构造查询条件,就无法触发了
LambdaQueryWrapper<SchedulerJobs> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(SchedulerJobs::getEnabled, true);
if (StringUtils.isNotBlank(request.getSchedulerId())) {
queryWrapper.eq(SchedulerJobs::getSchedulerId, request.getSchedulerId());
}
return this.dao.list(queryWrapper);
附typeHandler:
@MappedTypes(Boolean.class)
@MappedJdbcTypes(JdbcType.SMALLINT)
public class BooleanTypeHandler extends BaseTypeHandler
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
short value = 0;
if (parameter) {
value = 1;
}
ps.setShort(i, value);
}
@Override
public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
boolean result = rs.getBoolean(columnName);
return !result && rs.wasNull() ? null : result;
}
@Override
public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
boolean result = rs.getBoolean(columnIndex);
return !result && rs.wasNull() ? null : result;
}
@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
boolean result = cs.getBoolean(columnIndex);
return !result && cs.wasNull() ? null : result;
}
}
### 重现步骤(如果有就写完整)
### 报错信息
2021-09-14 18:32:41,510 - ERROR [http-nio-8080-exec-2] [com.wushi.lade.web.filter.request.RequestFilter] - org.springframework.jdbc.BadSqlGrammarException:
Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: smallint = boolean
Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 309
The error may exist in com/wushi/bus/scheduler/mapper/SchedulerJobsMapper.java (best guess)
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: SELECT job_id,job_name,job_group,job_class,job_desc,enabled,trigger_config,exception_config,is_immediate,job_data,start_time,pre_fire_time,next_fire_time,status,file_location,tags,next_job_id,create_time,create_by,update_time,update_by,remark,scheduler_id,version FROM jobs WHERE (enabled = ?)
Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: smallint = boolean
Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 309 ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: smallint = boolean Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
```
Comment From: miemieYaho
目前不支持
Comment From: gooym
目前框架层面貌似没解决,我这里的解决方案是继承LambdaQueryWrapper然后重写自己需要的查询比如eq,in,like,然后针对要处理的字段进行处理,处理之后作为入参执行sql
Comment From: qmdx
https://github.com/baomidou/mybatis-plus/issues/3346