Hibernate5.2.4Final学习(一)配置

1.下载Hibernate
http://www.hibernate.org
2.将下载完的hibernate-release-5.2.4.Final.zip文件解压,进入到lib,可以看到在spatial里边有一个slf4j-api-1.7.7.jar
包,这个是以后需要我们导入的包之一,因此需要去网上搜索与之对应的slf4j。其下载地址如下:
http://www.slf4j.org/dist/
3.打开eclipse,进入Window->Preference->Java->Build Path->User Libraries, New 一个lib取名为hibernate,选择右边
Add External JARs,然后将解压后的hibernate的lib/required目录下所有的 jar 包全部导入,把2中提到的slf4j-api-1.7.7.jar
以及从slf4j官网上下载的对应的slf4j-1.7.7目录下的slf4j-nop-1.7.7.jar也一同导入。

所有的包导入后的截图如下
Paste_Image.png

4.开启MySql数据库,并建一张表,见MySQL学习笔记一文,

5.新建一个java project, 取名为hibernate, 然后右键Build Path,将刚才建立的User Library导入。并在src下建立一个PO类,取名为Student,该名称与数据库表的名称一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

6.在hibernate项目的src目录下新建一个文件,取名为hibernate.cfg.xml,名称最好不要更改,
然后,在里边书写如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property>-->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">update</property>-->
<mapping resource="com/nick/testhibernate/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

7.在Student.java所在的包路径下新建一个文件取名为Student.hbm.xml,也建议不要采用其他名字
该名称与上面的hibernate.cfg.xml的第34行的名称一致。文件的内容如下

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nick.testhibernate">
<class name="Student" >
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>

8.最后建一个测试类StudentTest.java来完成我们第一个hibernate框架下的程序。当然也可以使用Junit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.nick.testhibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setAge(19);
s.setId(1);
s.setName("vip");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
您的支持将是我前进的不懈动力