Spring boot: mybatis returns null when querying underlined fields

Time:2021-5-12

Problem description

MyBatisUnable to query data with different property name and database field name.

That is: the attribute name and database field name areHump namingandUnderline namingThe data found out in the survey areNULL

problem analysis

MyBatisThe default is one-to-one correspondence between the attribute name and the database field name  

Database table column: user_ name  

Entity class attribute: user_ name

However, hump is usually named in Java

Database table column: user_ name  

Entity class property: username

Solution

staySpring BootBy setting themap-underscore-to-camel-caseProperty istrueTo turn on the hump.   

MyBatisto configure:  

application.propertiesMedium:

1. Turn on hump naming conversion
2.  mybatis.configuration.map-underscore-to-camel-case=true

application.ymlMedium:  

1.  mybatis:
2.    configuration:
3.      map-underscore-to-camel-case: true

  This configuration allows you toMyBatisAutomaticallySQLThe underlined named fields found in,Automatic conversionName the hump and match the attributes in the class.