2.新建一個包含所有Hibernate依賴jar包的User Library。
3.將Hibernatejar包和mysql jdbc驅動程序加入環境變量。
4.添加配置文件:從hibernate包中自帶的示例代碼中拷貝配置文件hibernate.cfg.xml到src目錄下面,而其中具體的值參考hibernate.properties文件中的內容。
5.往配置文件中添加一系列配置屬性<property>,主要包括:驅動名稱,url,username,password,mysql方言:
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">XXXXXXX</property>
6.hibernate開發環境已經搭建完成了,下面我們寫一個小的測試程序。我們使用hibernate來保存一個用戶信息User。
7.新建一個java類User:
- package net.jerryblog.hibernate.vo;
- import java.util.Date;
- public class User {
- private int id;
- private String name;
- private String passwd;
- private Date createTime;
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- 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 String getPasswd() {
- return passwd;
- }
- public void setPasswd(String passwd) {
- this.passwd = passwd;
- }
- }
8.對象已經建好,現在需要編寫映射文件,還是從hibernate包提供的映射文件為模板進行編寫。建議將映射文件與實體類放在一起,也放在對應的包中。
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="net.jerryblog.hibernate.vo.User" table="t_user">
- <id name="id">
- <generator class="uuid"/>
- </id>
- <property name="name"/>
- <property name="passwd"/>
- <property name="createTime"/>
- </class>
- </hibernate-mapping>
9.將映射文件User.hbm.xml添加到主配置文件。將其添加到<session-factory>標簽內
- <mapping resource="net/jerryblog/hibernate/vo/User.hbm.xml"/>
10.編寫工具類ExportDB,使用映射文件生成��據庫表。
- package net.jerryblog.hibernate.util;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class ExportDB {
- public static void main(String[] args) {
- Configuration cfg = new Configuration().configure();
- SchemaExport exp = new SchemaExport(cfg);
- exp.create(true, true);
- }
- }
11.在運行上面代碼之前創建數據庫先。然後再運行程序。
12.接下來,我們編寫代碼將User數據保存進數據庫中的用戶表中。為了讓我們能夠看到hibernate做了些什麼,我們添加兩個配置屬性:show_sql和format_sql。
- <property name="hibernate.show_sql">true</property>
- <property name="hibernate.format_sql">true</property>
添加這兩個配置之後,我們重新運行一下剛才的ExportDB的程序,可以看到,控制台打印了下列sql語句,並且進行了一定的格式化:650) this.width=650;" border=0>
編寫插入User信息的類InsertUserDemo如下:
- package net.jerryblog.hibernate.client;
- import java.util.Date;
- import net.jerryblog.hibernate.vo.User;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- public class InsertUserDemo {
- public static void main(String[] args) {
- Configuration cfg = new Configuration().configure();
- SessionFactory fac = cfg.buildSessionFactory();
- Session s = null;
- Transaction tx = null;
- try {
- s = fac.openSession();
- tx = s.beginTransaction();
- User u = new User();
- u.setName("張三");
- u.setPasswd("888888");
- u.setCreateTime(new Date());
- s.save(u);
- tx.commit();
- }catch(HibernateException e) {
- tx.rollback();
- if(s.isOpen()) {
- s.close();
- }
- }
- }
- }
控制台打印如下:
650) this.width=650;" border=0>
最後在數據庫中查看一下有沒有記錄插進去:
650) this.width=650;" border=0>
有亂碼,主要是因為cmd窗口的編碼不同造成的。沒關系。