Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that mybatis cannot assign values to underlined attributes

Shulou Source: shulou.com Published: 2022-06-02 00:54:18 10月11日 Update

This article mainly shows you how to solve the problem that mybatis can not assign values to underlined attributes, the content is simple and clear, and I hope it can help you solve your doubts. Let me take you to study and learn "how to solve the problem that mybatis cannot assign values to underlined attributes".

Mybatis cannot assign a value to an underlined attribute. 1. Configuration question

Or

/ / enable hump mapping bean.getObject (). GetConfiguration (). SetMapUnderscoreToCamelCase (true)

After this is set in the mybatis configuration file, if the queried field is underlined, then the underscore will be removed and the java hump rule will be adopted. For example, if the database field Parent_id is queried, it will be changed to parentid, and then go to the entity class to match the corresponding field. Because there is an underscore in your entity class, it doesn't match. Either use resultMap or disable the hump rule (not recommended). If you do not want to change the entity class, it is recommended to use resultMap.

2. Increase the way the set method converts values.

Original entity class

Public class demo {private String user_name; private Striing pass_word; public String getPass_word () {return pass_word;} public void setPass_word (String pass_word) {this.pass_word=pass_word;}}

Original sql

Select user_name,pass_word from demo

This sentence sql can be found in the database, but not in entity class queries using mybatis.

Solution method

Add the following two set methods to the entity class and change sql to

Select user_name as username,pass_word as password from demo

Querying a database with an entity class is actually a set process that requires only one set method for each attribute.

Public void setUsernaem (String user_name) {this.user_name=user_name;} public void setPassword (String pass_word) {this.pass_word=pass_word;}

It is only applicable to situations where it is not convenient to change entity classes.

There is a problem that the sliding line field cannot be assigned in the entity class.

There is a drop line field in the entity class, and mybatis cannot assign a value.

Remove the underscore from the entity class directly

The return value uses resultMap for field mapping assignment

The above is about "how to solve the problem that mybatis cannot assign values to underlined attributes". If this article is helpful to you and you think it is well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.

Tags: Entities underscores fields attributes questions data methods queries content databases humps that is suggestions articles or rules learning help configuration good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Shulou Tech Info Microsoft Shulou Technology