hibernate第一天:环境搭建
hibernate第一天:环境搭建
1.hibernate官网:https://www.hibernate.org
2.下载开发包注意兼容性
Compatibility matrix 3.5以后合并了
3.hibernate-distribution-3.5.3-Final-dist.zip
官方文档
hibernate-distribution-3.5.3-Final\documentation\manual\zh-CN\html_single
相关jar包
\hibernate-distribution-3.5.3-Final\hibernate3.jar
\hibernate-distribution-3.5.3-Final\lib\required*.jar
\slf4j-1.5.8\slf4j-nop-1.5.8.jar
\odbc6.jar
4.建立xml配置的项目
4.1 实体类Student.java
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
...
}
4.1 Student.hbm.xml
<hibernate-mapping>
<class name="com.hltech.hibernate.Student">
<id name="id" />
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
4.2数据存储
Student std =new Student();
std.setId(11);
std.setName("madison");
Configuration conf=new Configuration();
SessionFactory sf=conf.configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
s.save(std);
s.getTransaction().commit();
5.建立annotation配置的hello项目,hibernate3以后支持annotation
\hibernate-distribution-3.5.3-Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar
5.1 实体类Teacher.java
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
...
}
5.2数据存储
Teacher t = new Teacher();
t.setId(1);
t.setName("georgia");
t.setTitle("president");
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
6.两者在hibernate.cfg.xml配置文件中区别
<mapping resource="com/hltech/hibernate/Student.hbm.xml"/>
<mapping class="com.hltech.hibernate.Teacher"/>