第一個Hibernate例子

Hibernate開發的環境搭建

(一)環境搭建首先得下載Hibernate需要的jar包,通常導入hibernqte的核心包和
required文件夾中的所有jar包。
(二)加入數據庫驅動。下面的例子中主要是採用Mysql數據庫來演示的,
所以引入MysqL的JDBC驅動mysql-connector-java-5.1.26-bin.jar。
(三)提供核心配置文件hibernate.cfg.xml文件(在src文件夾下即可)。其中的
配置如下(針對mysql)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 數據庫驅動 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 數據庫名稱 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
        <!-- 數據庫的登陸用戶名 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 數據庫的登陸密碼 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 方言:爲每一種數據庫提供適配器,方便轉換 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>  
        
        <mapping resource="hibernateExample/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

該例子的目錄結構如下圖:



其中hibernate4裏面是hibernate核心包,Referenced Libraries裏包含了所
需要的hibernate的依賴包。

1:新建一個普通的java項目,按照上面的步驟導入相關的jar包和配置文件。

2:建立User實體類

package hibernateExample;

import java.util.Date;

public class User {
	private String id;
	private String username;
	private String password;
	private Date createTime;
	private Date expireTime;
	
	public String getId(){
		return id;
	}
	public void setId(String id){
		this.id = id;
	}
	public String getUsername(){
		return username;
	}
	public void setUsername(String username){
		this.username = username;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password = password;
	}
	public Date getCreateTime(){
		return createTime;
	}
	public void setCreateTime(Date createTime){
		this.createTime = createTime;  
	}
	public Date getexpireTime(){
		return expireTime;
	}
	public void setExpireTime(Date expireTime){
		this.expireTime = expireTime;
	}
}
3.User.hbm.xml,完成實體類的映射

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping>
	<class name="hibernateExample.User">
		<id name="id">
			<generator class="uuid.hex"/>
		</id>
		<property name="username"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
	</class>
</hibernate-mapping>

4.生成表:編寫工具類ExportDB.java,將hbm生成ddl

package hibernateExample;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExoprtDB {
    public static void main(String[] args){
        //默認讀取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();
        //創建SchemaExport對象
        SchemaExport export = new SchemaExport(cfg);
        //創建數據庫表
        export.create(true,true);
    }
}



5.向表中添加數據
package hibernateExample;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Client {
	public static void main(String[] args){
		/*讀取配置文件*/
		Configuration cfg = new Configuration().configure();
		/*爲了獲得實例,先創建工廠*/
		SessionFactory factory = cfg.buildSessionFactory();
				
		Session session = null;
		try{
			/*session通過SessionFactory打開,在所有的工作完成後關閉 */
			session = factory.openSession();
			/*開啓事務 */			
			session.beginTransaction();
			
			User user = new User();
			
			user.setUsername("用戶名");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			session.save(user);
			/*提交事務*/
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			/*回滾事務*/
			session.getTransaction().rollback();
		}finally{
			if(session !=null){
				if(session.isOpen()){
					/*關閉session*/
					session.close();
				}
			}
		}
	}
}


完成後,執行Client.java就可以向表中增加數據了:







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章