歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Hibernate基本開發環境搭建

Hibernate基本開發環境搭建

日期:2017/2/28 15:59:03   编辑:Linux教程
1.建立一個Java工程。

2.新建一個包含所有Hibernate依賴jar包的User Library。

3.將Hibernatejar包和mysql jdbc驅動程序加入環境變量。

4.添加配置文件:從hibernate包中自帶的示例代碼中拷貝配置文件hibernate.cfg.xml到src目錄下面,​而其中具體的值參考hibernate.properties文件中的內容。

5.往配置文件中添加一系列配置屬性<property>,主要包括:驅動名稱,url,username,password,mysql方言:

  1. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  2. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  3. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  4. <property name="hibernate.connection.username">root</property>
  5. <property name="hibernate.connection.password">XXXXXXX</property>

​6.hibernate開發環境已經搭建完成了,下面我們寫一個小的測試程序。我們使用hibernate來保存一個用戶信息User。

7.新建一個java類User:

  1. package net.jerryblog.hibernate.vo;
  2. import java.util.Date;
  3. public class User {
  4. private int id;
  5. private String name;
  6. private String passwd;
  7. private Date createTime;
  8. public Date getCreateTime() {
  9. return createTime;
  10. }
  11. public void setCreateTime(Date createTime) {
  12. this.createTime = createTime;
  13. }
  14. public int getId() {
  15. return id;
  16. }
  17. public void setId(int id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getPasswd() {
  27. return passwd;
  28. }
  29. public void setPasswd(String passwd) {
  30. this.passwd = passwd;
  31. }
  32. }

8.對象已經建好,現在需要編寫映射文件,還是從hibernate包提供的映射文件為模板進行編寫。建議將映射文件與實體類放在一起,也放在對應的包中。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  3. <hibernate-mapping>
  4. <class name="net.jerryblog.hibernate.vo.User" table="t_user">
  5. <id name="id">
  6. <generator class="uuid"/>
  7. </id>
  8. <property name="name"/>
  9. <property name="passwd"/>
  10. <property name="createTime"/>
  11. </class>
  12. </hibernate-mapping>

9.將映射文件User.hbm.xml添加到主配置文件。將其添加到<session-factory>標簽內

  1. <mapping resource="net/jerryblog/hibernate/vo/User.hbm.xml"/>

10.編寫工具類ExportDB,使用映射文件生成��據庫表​。

  1. package net.jerryblog.hibernate.util;
  2. import org.hibernate.cfg.Configuration;
  3. import org.hibernate.tool.hbm2ddl.SchemaExport;
  4. public class ExportDB {
  5. public static void main(String[] args) {
  6. Configuration cfg = new Configuration().configure();
  7. SchemaExport exp = new SchemaExport(cfg);
  8. exp.create(true, true);
  9. }
  10. }

11.在運行上面代碼之前創建數據庫先。然後再運行程序。

12.接下來,我們編寫代碼將User數據保存進數據庫中的用戶表中。為了讓我們能夠看到hibernate做了些什麼,我們添加兩個配置屬性:show_sql和format_sql。

  1. <property name="hibernate.show_sql">true</property>
  2. <property name="hibernate.format_sql">true</property>

添加這兩個配置之後,我們重新運行一下剛才的ExportDB的程序,可以看到,控制台打印了下列sql語句,並且進行了一定的格式化:650) this.width=650;" border=0>

編寫插入User信息的類InsertUserDemo如下:

  1. package net.jerryblog.hibernate.client;
  2. import java.util.Date;
  3. import net.jerryblog.hibernate.vo.User;
  4. import org.hibernate.HibernateException;
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.Transaction;
  8. import org.hibernate.cfg.Configuration;
  9. public class InsertUserDemo {
  10. public static void main(String[] args) {
  11. Configuration cfg = new Configuration().configure();
  12. SessionFactory fac = cfg.buildSessionFactory();
  13. Session s = null;
  14. Transaction tx = null;
  15. try {
  16. s = fac.openSession();
  17. tx = s.beginTransaction();
  18. User u = new User();
  19. u.setName("張三");
  20. u.setPasswd("888888");
  21. u.setCreateTime(new Date());
  22. s.save(u);
  23. tx.commit();
  24. }catch(HibernateException e) {
  25. tx.rollback();
  26. if(s.isOpen()) {
  27. s.close();
  28. }
  29. }
  30. }
  31. }

控制台打印如下:

650) this.width=650;" border=0>

最後在數據庫中查看一下有沒有記錄插進去:

650) this.width=650;" border=0>

​有亂碼,主要是因為cmd窗口的編碼不同造成的。沒關系。

Copyright © Linux教程網 All Rights Reserved