天天看点

Hibernate基础配置

@Entity
@Table(name = "teacher")
// 指定该类对应的表明
public class Teacher {// 实体与

	private int id;
	private String title;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}           

1、hibernate.cfg.xml:show_sql 显示sql语句  

hibernate.cfg.xml:format_sql   将显示的sql语句格式化打印出来

2、表和类名不同,对表明进行处理

对程序:使用Anontation,在类的前面加@table

xml:?

3、字段名和属性相同

  默认认为basic

 xml中不用写column

4、字段名与属性不同时

Anonnontation:@column

xml:?

5、不需要使用psersistence的字段

Anontation:@Transient

xml:指定type

6、映射日期与时间类型,指定时间精度

Anontation:@Temporal

type="long"可以指定多种hibernate的类型

图1 

7、映射枚举类型

@Enumerated

8、Anontation字段映射的位置

例如@Id应该写在成员变量上,还是写在get方法上;最好写在get方法上,不破坏其封装性!