The solution that the mybatis field cannot be automatically mapped in springboot

Article directory

question

Today, when using mybatis in springboot, the fields cannot be automatically mapped. The version of mybatis is 3.5.11. The database is named according to the underscore, and the java class follows the hump naming method, as follows

insert image description here
insert image description here

Write the mapper.xml file as follows to query all data

    <select id="queryAllGoods" resultType="com.ttpfx.seckill.entity.Goods">
        select *
        from t_goods
    </select>

The result of the final query does not perform field mapping, only the id is obtained, and the output is as follows

insert image description here


solve

After reading the official document mybatis3 automatic mapping , I found the reason

insert image description here

By default, mybatis only ignores case when performing automatic mapping, and does not handle underscores. You need to set mapUnderscoreToCamelCase to true.

The final solution is to configure in the yaml configuration file, as follows

mybatis:
  configuration:
    map-underscore-to-camel-case: true

Run the test program again and the problem is successfully resolved

insert image description here

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/130549864