相关基础概念请从其它教材简单了解,这里仅记录下第一个Hibernate程序的实现步骤。环境说明:java开发工具:eclipse MARS.2 Release(4.5.2)hibernate版本:hibernate-release-4.3.6.FinalWeb 容器:Tomcat ...
相关基础概念请从其它教材简单了解,这里仅记录下第一个Hibernate程序的实现步骤。
环境说明:
java开发工具:eclipse MARS.2 Release(4.5.2)
hibernate版本:hibernate-release-4.3.6.Final
Web 容器:Tomcat v8.0
数据库:MySQL 5.6.19-enterprise-commercial-advanced
jdbc驱动:mysql-connector-java-commercial-5.1.30-bin.jar
1.下载hibernate。
2.下载jdbc驱动文件。
3.在eclipse中新建web project,命名为firsthibernate。
4.拷贝hibernate/lib/required文件夹下的所有jar文件、以及jdbc驱动文件(此文件另外下载)到项目的WEB-INF/lib文件夹下,拷贝hibernate/project/etc下的hibernate.cfg.
5.新建实体类Cat,Hibernate中配置实体类有两种方法:
package com.levice.firsthibernate.bean;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;@Entity //注解Entity表示该类能被Hibernate持久化@Table(name = "tb_cat") //指定该Entity对应的数据表名public class Cat { @Id //指定该列为主键。主键类型最好不要使用int等原始类型 @GeneratedValue(strategy = GenerationType.AUTO) //主键类型auto表示该主键为自增长型 private Integer id; @Column(name = "name") //指定该属性对应的数据库表的列为name,列名与属性名一样时这句注解可省略 private String name; @Column(name = "description") private String description; @ManyToOne //指定实体类之间的关系,本例表示多对一关系 @JoinColumn(name = "mother_id") private Cat mother; @Temporal(TemporalType.TIMESTAMP)//日期类型(DATE,TIME或TIMESTEMP) @Column(name = "birthday") private Date birthday; //getters and setters public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Cat getMother() { return mother; } public void setMother(Cat mother) { this.mother = mother; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
原标题:Hibernate入门笔记
关键词:Hibernate
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。