Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ TableField in Mybatis Plus

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use @TableField in Mybatis Plus. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Use of @TableField 1 Research Background and Process

Sister paper development suddenly found @TableField invalid, ask in the group. As soon as I understand it, I found that my sister paper originally intended to use @TabkeField to achieve functions similar to @JsonProperty. That is, when the entity class attribute name is inconsistent with the database column name, the alias effect is achieved through this annotation.

Strangely enough, this annotation was partly successful and partly ineffective. The specific performance is that sometimes you can get the value, sometimes you can't get it. I clicked on the source code and found that it set the original meaning of value: when hump conversion is not enabled, use this annotation to implement hump conversion. However, my actual tests show that it doesn't actually do this.

I swear to tell my sister that @TableField has no alias function. Who knows sister paper sent me a screenshot of automatic sql printing. It clearly showed that MP had indeed automatically generated an AS alias. A flash of inspiration, is it Mybatis Plus automatically generated can alias, but their own written sql can not?

Don't test don't know, a test scared. As expected, handwritten sql cannot automatically generate aliases, and automatically generated sql query statements are successfully converted. At this point, the @TableField annotation ends.

2 Summary 2.1 Alias effect value attribute

When using MP auto-generated methods, you can alias with @TableField

When writing sql in xml, you need to use as or resultMap to convert

2.2 Prevent fields from persisting the exist attribute

Use the exist attribute to control whether the attributes in the class correspond to each other in the table. If there are fields that do not exist in the data table, it is recommended to set them to fasle.

@TableField(exist = false)

Annotations load bean attributes, indicating that the current attribute is not a database field, but must be used in the project, so that when using beans, mybatis-plus will ignore this and will not report errors.

Thank you for reading! About "Mybatis Plus @TableField how to use" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report