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 columnDefinition based on Column annotations

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use columnDefinition based on Column annotations". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

ColumnDefinition use of Column annotations

Interpretation of each field of @ Column annotation

ColumnDefinition use of Column annotations

The columnDefinition attribute represents the SQL statement created by this field when creating a table, which is generally used when generating table definitions through Entity. If the table in the database has been built, it is not necessary to use this attribute.

1. Specify field type, length, whether null is allowed, whether it is unique, and default value

/ * * Warehouse No. * / @ Column (name = "code", columnDefinition = "Varchar (100) not null default'' unique") private String code

2. Situations where a special field type is required

@ Column (name= "remark", columnDefinition= "text") private String remark;@Column (name= "salary", columnDefinition= "decimal (5Power2)") private BigDecimal salary;@Column (name= "birthday", columnDefinition= "date") private Date birthday;@Column (name= "createTime", columnDefinition= "datetime") interpretation of various fields in private Date createTime;@Column annotations

View the source code:

Target ({ElementType.METHOD, ElementType.FIELD}) @ Retention (RetentionPolicy.RUNTIME) public @ interface Column {String name () default ""; boolean unique () default false; boolean nullable () default true; boolean insertable () default true; boolean updatable () default true; String columnDefinition () default ""; String table () default ""; int length () default 255; int precision () default 0; int scale () default 0;}

Explanation:

Name: defines the name of the labeled field in the database table

Unique: indicates whether the field is a unique identity. The default is false. If there is a field in the table that needs to be uniquely identified, you can use either that tag or the

Nullable: indicates whether the field can be null. Default is true.

Insertable: indicates whether the value of this field needs to be inserted when inserting data using the "INSERT" script.

Updatable: indicates whether the value of this field needs to be updated when inserting data using the "UPDATE" script. Insertable and updatable properties are generally used for read-only properties, such as primary keys and foreign keys. The values of these fields are usually generated automatically.

ColumnDefinition (rarely used in most cases): indicates the SQL statement created by this field when the table is created, which is generally used when generating table definitions through Entity. (that is, if the table has been built in DB, this property is not necessary.

Table: indicates that the fields in the table of the specified table are specified when multiple tables are mapped. The default value is the table name of the primary table.

Length: indicates the length of the field. This property is valid only when the field is of type varchar. The default is 255characters.

The precision and scale:precision attributes and scale attributes represent precision. When the field type is double, precision represents the total length of the numeric value, and scale represents the number of digits occupied by the decimal point.

That's all for "how to use columnDefinition based on Column annotations". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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