Convert a MySQL CREATE TABLE statement to a Java entity class:
@TableName, @TableId, and JPA annotationsBefore generating the entity class, determine the following:
com.example.entity)If the user doesn't specify, use sensible defaults based on the project context.
For complete MySQL to Java type mappings, see type_mappings.md.
MySQLJavaNotesINT``Integer``BIGINT``Long``VARCHAR``String``TEXT``String``DATETIME``LocalDateTimejava.time.LocalDateTimeTIMESTAMP``LocalDateTimejava.time.LocalDateTimeTINYINT(1)``Integer``DECIMAL``BigDecimaljava.math.BigDecimal
Convert snake_case to camelCase:
text
user_name → userName
created_at → createdAt
user_id → userId
is_active → isActive
Class name conversion (PascalCase):
text
user_info → UserInfo
order_detail → OrderDetail
ConstraintAnnotationTable name@TableName("table_name")Primary key@TableId(type = IdType.AUTO)Column name@TableField("column_name") (only if different from field name)Not mapped@TableField(exist = false)
ConstraintAnnotationPrimary key@IdAuto increment@GeneratedValue(strategy = GenerationType.IDENTITY)Column name@Column(name = "column_name")Not null@Column(nullable = false)Unique@Column(unique = true)Length@Column(length = 50)
sql
CREATE TABLE `user_info` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int DEFAULT NULL,
`email` varchar(100) NOT NULL,
`salary` decimal(10,2) DEFAULT NULL,
`description` text,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
`is_active` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='User Table';
java
package com.example.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@TableName("user_info")
public class UserInfo {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
private BigDecimal salary;
private String description;
private LocalDateTime createdAt;
private Integer isActive;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public Integer getIsActive() {
return isActive;
}
public void setIsActive(Integer isActive) {
this.isActive = isActive;
}
}
Entity classes should be placed in a package specified by the user. Common conventions:
com.{company}.{module}.entity or com.{company}.{module}.modelUse table comment as class comment:
java
/**
* User Table
*/
@TableName("user_info")
public class UserInfo extends EntityBean {
}
The entity may extend a framework base class if required:
BaseEntity, AbstractEntity, or no base classMySQL Column DefinitionIdTypeAUTO_INCREMENTIdType.AUTOManual assignmentIdType.INPUTUUIDIdType.ASSIGN_UUIDSnowflake algorithmIdType.ASSIGN_ID
For nullable columns (allows NULL), use wrapper types:
Integer instead of intLong instead of longBoolean instead of booleanInteger, Long, Boolean) for nullable columnsBigDecimal for DECIMAL/NUMERIC types to maintain precisionLocalDateTime for DATETIME/TIMESTAMP types@TableName should match the actual database table name exactlyid field should use IdType.AUTO for auto-increment columns