步骤1:hibernate 注解分类
步骤2:类注解
步骤3:属性注解
步骤 1 : hibernate 注解分类
hibernate里常用注解包括,类注解,属性注解,关系注解,其他的注解
本知识点讲解类注解和属性注解
步骤 2 : 类注解
在注解示例-注解方式的Product中,Product类声明前面有两个注解:@Entity 和 @Table(name = "product_")
@Entity 表示这是一个实体类,用于映射表
@Table(name = "product_") 表示这是一个类,映射到的表名:product_
@Entity @Table (name = "product_" ) public class Product { int id; String name; float price; } |
步骤 3 : 属性注解
然后是属性注解,属性注解是配置在属性对应的getter方法上的
@Id @GeneratedValue (strategy = GenerationType.IDENTITY) @Column (name = "id" ) public int getId() { return id; } |
@Id 表示这是主键
@GeneratedValue(strategy = GenerationType.IDENTITY) 表示自增长方式使用mysql自带的
@Column(name = "id") 表示映射到字段id
注: 其他自增长方式请查看 注解手册
@Column (name = "name" ) public String getName() { return name; } |
表示name属性映射表的name字段
@Column (name = "price" ) public float getPrice() { return price; } |
表示price属性映射表的price字段
package com.how2java.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name = "product_" ) public class Product { int id; String name; float price; @Id @GeneratedValue (strategy = GenerationType.IDENTITY) @Column (name = "id" ) public int getId() { return id; } public void setId( int id) { this .id = id; } @Column (name = "name" ) public String getName() { return name; } public void setName(String name) { this .name = name; } @Column (name = "price" ) public float getPrice() { return price; } public void setPrice( float price) { this .price = price; } } |
关注我,每天准时更新Java技术知识
更多Java全栈技术内容,点击了解: https://how2j.cn/k/hibernate/hibernate-class-property-annotation/1048.html