Hibernate开发时,有两种形式,一种是XML配置的方式,另外一种是注解形式的开发。
XML配置是,需要写一个实体类User,还要在它的同级目录下创建一个相对应的User.hbm.xml,
而注解方式比较简单,只需要在User类中添加相应的注解即可。
一. Hibernate的注解开发
一.一 创建简单的实体类User
User.java 文件:
package com.yjl.pojo; import java.io.Serializable; import java.util.Date; /** @author: 两个蝴蝶飞 @date: 2018年10月9日 下午8:53:39 @Description Hibernate操作时相应的实体类,实现序列化 */ public class User implements Serializable{ private static final long serialVersionUID = 1L; /** * @param 主键id 用Integer包装类 * @param userName 用户名 * @param password 密码 * @param sex 性别 * @param age 年龄 * @param birthday 生日 * @param description 相关描述 */ private Integer id; private String userName; private String password; private String sex; private Integer age; private Date birthday; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
一.二 在User.java中添加相应的注解
一.二.一 添加@Entity注解
直接在类User上使用 @Entity 注解即可。 这个注解@Entity 有一个属性 name,这个name指的是实体类的名称,通常省略不写.
这个这个Entity 注解位于
import javax.persistence.Entity;
下面所有的注解,都是位于这个包下。
一.二.二 添加@Table 注解
@Table 与@Entity 联合使用。 其中@Table下有一个重要的name属性,这个值指向数据库中表的名称。表示生成后的表名为user. 如果省略不写的话,表示与User实体类名相同。 通常为了可读性,常常写。 联合使用后的User 修饰为: @Entity @Table(name="user") public class User implements Serializable{ }
一.二.三 Id 标识
Id标识时用@Id 注解,默认生成策略是native, 自然的。可以省略不写。 用@GeneratedValue 注解。
@Id //表示是id @GeneratedValue //表示生成策略是native, 是默认的。 private Integer id;
其中@Id注解为:
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Id {} @GeneratedValue 注解为: @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface GeneratedValue { GenerationType strategy() default GenerationType.AUTO; //strategy 表示生成策略 String generator() default ""; }
其中GenerationType 可以取的值为:
package javax.persistence; public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO; //枚举类型取值 private GenerationType() {} }
如果是IDENTITY模式的话,那么需要写成:
@Id //表示是id @GeneratedValue(strategy=GenerationType.IDENTITY) //表示生成策略是IDENTITY private Integer id;
如果想显式说明的是native的话,可以这样:
@Id //表示是id @GeneratedValue(generator="_native") //生成策略是native形式 @GenericGenerator(name="_native",strategy="native") private Integer id;
GeneratedValue与GenericGenerator联合使用。 Value中的generator的值要与Generator中name的值保持一致。
如果想表示是uuid形式的话,可以这样:
@Id //表示是id @GeneratedValue(generator="_uuid") //生成策略是uuid @GenericGenerator(name="_uuid",strategy="uuid") private String uid;
其中GenericGenerator注解为:
@Target({java.lang.annotation.ElementType.PACKAGE, java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(GenericGenerators.class) public @interface GenericGenerator { String name(); String strategy(); Parameter[] parameters() default {}; }
一.二.四 普通属性注解@Column
普通属性用@Column 常见的配置有:
@Column(name="userName",length=20) private String userName;
可以直接@Column, 可以省略name,如果省略name的话,表示生成的表中字段与类中的属性名称一致。
其中@Column注解中有一些其他的配置,这样配置主要如下:
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.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; }
其中name表示生成表中字段的名称,如果省略不写的话,与类中属性值保持一致。
unique 表示是否是唯一的,默认为false. 如果为true,则表示唯一,是唯一约束关系。
nullable 表示是否可以为空,默认为true, 如果为false,表示不能为空。
insertable 表示是否可以插入,默认为true. 这个用的不多.
updateable 表示是否可以修改,默认为true. 如果为false的话,表示这个字段不能被修改。如sex字段。 一但被插入进去之后,就不能进行相应的修改了。
columnDefinition 表示列类型,即是int,还是varchar,date等。 但兼容性不好。
length 表示长度,即字段的长度。
precision 和scale共同构成了精度,如double类型时,precision 指的是总长度,scale 指的是位数。 即double(6,2) 样式的。
precision=6, scale=2。