SoFunction
Updated on 2025-03-03

Mybatisplus underline camel conversion problem solved

Problem: The fields containing underscores in the object cannot be found

existmybatis-plus, enabled by defaultSliding Line-CamelConvert
The underscore in the database field will be automatically converted into camel form, and then match the fields of the entity class object.

online_num -> onlineNum

If there is an underlined field in an entity class, query itnull

 private Integer online_num;

Just used@TableField()The annotation specifies the mapping relationship, and the query result is alsonull

 @TableField("online_num")
 private Integer online_num;

The problem is that the camel conversion occurs. Because the mapping relationship has been specified, just turn off the camel conversion:
yml:

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false

properties:

-underscore-to-camel-case=false

But there is a problem here. If you turn off the automatic camel conversion, all fields must be passed@TableField()Annotation to specify the mapping relationship orSpecify mapping relationships in this way, which increases the workload. So don't use class fields unless necessary_Go to the name field to save trouble.

It really doesn't work. If you have to use an underlined field, see what the specific reason is:

①If it is required for serialization or deserialization, you can use @JsonProperty and @JsonAlias ​​to solve it.
②If it is required by toString, then rewrite the toString method of Object
③In other cases, you can add a field such as a_b, then add aB field, and then write the getter and setter corresponding to the a_b field to point to the aB field. If you don’t want to convert the field, turn the object, query an object, and then turn it into a business object after querying it. There are still many tricks and tricks~

This is the article about solving the problem of mybatisplus underline camel conversion. For more related contents of mybatisplus underline camel conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!