使用的是Hibernate3.2.x版本。数据库采用的是MySQL5.1。实现保存用户的功能。由于主要是搭建开发环境以及写第一个测试的小应用,因此里面的代码只是简单的实现功能。
1.下载Hibernate3.2。 www.hibernate.org现在可能很难找到该版本,所以可以在百度搜索一下。下载后将压缩文件解压到本地硬盘,例如,我解压到F:\hibernate-3.2目录。
2.安装MySQL数据库,创建一个叫hibernatefirst的数据库,然后use该数据库(关于MySQL安装以及MySQL如何创建数据库等这里就不详细说了。)
3.在MyEclipse中新建一个Java项目(不用是Web项目,Java项目即可),取名为hibernate。在项目下新建一个Folder,取名为lib,将hibernate解压路径下的hibernate3.jar文件,以及hibernate解压路径下的lib目录下的所有jar文件一并拷贝到该lib目录下。此外,hibernate作为一个持久层框架,其底层使用的依然是JDBC,因此,需要将MySQL的JDBC驱动加入该lib目录下。这样hibernate需要的jar文件就齐了。但是这些jar文件并没有加入该项目的classpath中,因此是无法在项目中引用的。因此,选中项目下lib目录下的所有jar文件,右键—>buildpath—>Add to Buildpath。
4.环境已经搭建好了,开始第一个简单的hibernate项目吧。在src目录下新建包:com.xiaobai.hibernate.domain,在其下创建类User,代码如下:
5.为User类编写映射文件User.hbm.xml。在com.xiaobai.hibetnate.domain包下创建该文件,作为新手肯定不知道该文件中到底应该写点什么,没关系,hibernate为我们提供的示例中有,copy过来再进行修改就好。到hibernate解压路径下的eg\org\hibernate\auction路径下,拷贝一个User.hbm.xml文件过来,将<hibernate-mapping>标签内的内容全部删除,将<hibernate-mapping>标签的package改成User类所在的包,也就是该映射文件所在的包。然后在<hibernate-mapping>标签对中添加如下内容:
6.编写hibernate配置文件hibernate.cfg.xml。到hibernate解压路径下的etc目录下拷贝一份到src目录下,注意不在包下,而是在src目录下。将以下代码覆盖copy过来的版本:
7.配置搞定,下面编写一个测试类,代码如下:
这段代码仅仅是为了实现功能,实际上是很不完善的,后面的文章会讨论如何写完善的合理的代码。运行该类。进入数据库,发现数据库中多了一张user表,查看里面的数据,已经添加了一条name为“张三”的数据。OK,第一个hibernate小示例完成了。
1.下载Hibernate3.2。 www.hibernate.org现在可能很难找到该版本,所以可以在百度搜索一下。下载后将压缩文件解压到本地硬盘,例如,我解压到F:\hibernate-3.2目录。
2.安装MySQL数据库,创建一个叫hibernatefirst的数据库,然后use该数据库(关于MySQL安装以及MySQL如何创建数据库等这里就不详细说了。)
3.在MyEclipse中新建一个Java项目(不用是Web项目,Java项目即可),取名为hibernate。在项目下新建一个Folder,取名为lib,将hibernate解压路径下的hibernate3.jar文件,以及hibernate解压路径下的lib目录下的所有jar文件一并拷贝到该lib目录下。此外,hibernate作为一个持久层框架,其底层使用的依然是JDBC,因此,需要将MySQL的JDBC驱动加入该lib目录下。这样hibernate需要的jar文件就齐了。但是这些jar文件并没有加入该项目的classpath中,因此是无法在项目中引用的。因此,选中项目下lib目录下的所有jar文件,右键—>buildpath—>Add to Buildpath。
4.环境已经搭建好了,开始第一个简单的hibernate项目吧。在src目录下新建包:com.xiaobai.hibernate.domain,在其下创建类User,代码如下:
1
package
com.xiaobai.hibernate.domain;
2
3 import java.util.Date;
4
5 public class User {
6
7 private int id;
8 private String name;
9 private Date birthday;
10 public int getId() {
11 return id;
12 }
13 public void setId(int id) {
14 this.id = id;
15 }
16 public String getName() {
17 return name;
18 }
19 public void setName(String name) {
20 this.name = name;
21 }
22 public Date getBirthday() {
23 return birthday;
24 }
25 public void setBirthday(Date birthday) {
26 this.birthday = birthday;
27 }
28}
29
2
3 import java.util.Date;
4
5 public class User {
6
7 private int id;
8 private String name;
9 private Date birthday;
10 public int getId() {
11 return id;
12 }
13 public void setId(int id) {
14 this.id = id;
15 }
16 public String getName() {
17 return name;
18 }
19 public void setName(String name) {
20 this.name = name;
21 }
22 public Date getBirthday() {
23 return birthday;
24 }
25 public void setBirthday(Date birthday) {
26 this.birthday = birthday;
27 }
28}
29
5.为User类编写映射文件User.hbm.xml。在com.xiaobai.hibetnate.domain包下创建该文件,作为新手肯定不知道该文件中到底应该写点什么,没关系,hibernate为我们提供的示例中有,copy过来再进行修改就好。到hibernate解压路径下的eg\org\hibernate\auction路径下,拷贝一个User.hbm.xml文件过来,将<hibernate-mapping>标签内的内容全部删除,将<hibernate-mapping>标签的package改成User类所在的包,也就是该映射文件所在的包。然后在<hibernate-mapping>标签对中添加如下内容:
1
<
class
name
="User"
>
2 < id name ="id" >
3 < generator class ="native" />
4 </ id >
5 < property name ="name" />
6 < property name ="birthday" />
7 </ class >
差点忘了说明,映射文件的命名是XXX.hbm.xml,其中的XXX是你所要映射的domain对象的名称,例如这里是User。
2 < id name ="id" >
3 < generator class ="native" />
4 </ id >
5 < property name ="name" />
6 < property name ="birthday" />
7 </ class >
6.编写hibernate配置文件hibernate.cfg.xml。到hibernate解压路径下的etc目录下拷贝一份到src目录下,注意不在包下,而是在src目录下。将以下代码覆盖copy过来的版本:
1
<!
DOCTYPE hibernate-configuration PUBLIC
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
4
5 < hibernate-configuration >
6 < session-factory >
7 < property name ="hibernate.connection.driver_class" > com.mysql.jdbc.Driver </ property >
8 < property name ="hibernate.connection.url" > jdbc:mysql:///hibernate </ property >
9 < property name ="hibernate.connection.username" > root </ property >
10 < property name ="hibernate.connection.password" >****** </ property >
11 < property name ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >
12 < property name ="hibernate.hbm2ddl.auto" > create </ property >
13
14 < property name ="show_sql" > true </ property >
15 < mapping resource ="com/xiaobai/hibernate/domain/User.hbm.xml" />
16 </ session-factory >
17 </ hibernate-configuration >
前面数过,hibernate底层也是通过JDBC实现的,因此也需要提供连接数据库的信息,所以前面四条属性就不难理解了。由于hibernate能支持很多关系型数据库,那么生成sql语句时,会根据数据库不同而作一定的调整,所以,需要配置数据库方言的信息,这就是第五条所配置的。默认情况下,我们只创建了数据库,数据库中并没有User对应的表,因此,保存User对象前需要先创建表,那么第六条配置的就是这个内容。另外,第七条配置让hibernate在打印生成的sql语句。最后,不要忘了将映射文件以资源的形式加入配置文件,这就是<mapping>标签中配置的内容。这么多的属性name是不用记住的,进入hibernate解压路径,etc目录下有一个hibernate.properties文件,里面包含了所有的属性的示例。在配置的时候照猫画虎拷贝需要的过来进行修改即可。
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
4
5 < hibernate-configuration >
6 < session-factory >
7 < property name ="hibernate.connection.driver_class" > com.mysql.jdbc.Driver </ property >
8 < property name ="hibernate.connection.url" > jdbc:mysql:///hibernate </ property >
9 < property name ="hibernate.connection.username" > root </ property >
10 < property name ="hibernate.connection.password" >****** </ property >
11 < property name ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >
12 < property name ="hibernate.hbm2ddl.auto" > create </ property >
13
14 < property name ="show_sql" > true </ property >
15 < mapping resource ="com/xiaobai/hibernate/domain/User.hbm.xml" />
16 </ session-factory >
17 </ hibernate-configuration >
7.配置搞定,下面编写一个测试类,代码如下:
1
package
com.xiaobai.hibernate;
2
3 import java.util.Date;
4
5 import org.hibernate.Session;
6 import org.hibernate.SessionFactory;
7 import org.hibernate.Transaction;
8 import org.hibernate.cfg.Configuration;
9
10 import com.xiaobai.hibernate.domain.User;
11
12 public class Base {
13 public static void main(String[] args) {
14 Configuration cfg = new Configuration().configure();
15
16 SessionFactory factory = cfg.buildSessionFactory();
17
18 Session session = factory.openSession();
19
20 Transaction tx = session.beginTransaction();
21
22 User user = new User();
23 user.setName("张三");
24 user.setBirthday(new Date());
25
26 session.save(user);
27 tx.commit();
28
29 session.close();
30 factory.close();
31
32
33 }
34}
35
2
3 import java.util.Date;
4
5 import org.hibernate.Session;
6 import org.hibernate.SessionFactory;
7 import org.hibernate.Transaction;
8 import org.hibernate.cfg.Configuration;
9
10 import com.xiaobai.hibernate.domain.User;
11
12 public class Base {
13 public static void main(String[] args) {
14 Configuration cfg = new Configuration().configure();
15
16 SessionFactory factory = cfg.buildSessionFactory();
17
18 Session session = factory.openSession();
19
20 Transaction tx = session.beginTransaction();
21
22 User user = new User();
23 user.setName("张三");
24 user.setBirthday(new Date());
25
26 session.save(user);
27 tx.commit();
28
29 session.close();
30 factory.close();
31
32
33 }
34}
35
这段代码仅仅是为了实现功能,实际上是很不完善的,后面的文章会讨论如何写完善的合理的代码。运行该类。进入数据库,发现数据库中多了一张user表,查看里面的数据,已经添加了一条name为“张三”的数据。OK,第一个hibernate小示例完成了。