天天看點

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

使用Eclipse進行Hibernate配置

*所有步驟按照老師視訊的順序進行,過程中遇到的問題已經基本解決。

1.建立Java檔案,導入Hibernate檔案所需要的類包、連接配接MySQL所使用的jdbc以及日志類"log4j-1.2.17.jar和slf4j-log4j12-1.7.7.jar。

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

所需的所有類包如下:

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

2.在src目錄下添加配置檔案hibernate.cfg.xml。

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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>
 
<property name="hibernate.dialect">MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="show_sql">true</property>
<mapping resource="hrbust/pojo/User.hbm.xml"/>
 
</session-factory>
</hibernate-configuration>

           

在進行此步時,在配置連接配接資料庫時發現show view中沒有DB browser選項,查閱資料發現可以使用Data Source Explorer,使用Data Source Explorer連接配接資料庫。

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

測試連接配接時報錯,錯誤日志顯示java.sql.SQLException: The server time zone value ’ й ׼ʱ ’ is unrecognized or represents more than one time zone. want to utilize time zone support.

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

查閱資料後發現該jdbc版本會報關于時區的錯誤。解決方案是在配置url中添加serverTimezone=UTC,測試連結成功。

使用Eclipse進行Hibernate配置使用Eclipse進行Hibernate配置

3.添加實體類和映射檔案User.hbm.xml ,通常放在一個目錄下。

實體類:

package hrbust.pojo;
 
public class User {
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;
}
}

           

User.hbm.xml :

<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 
    <class name="hrbust.pojo.User" table="h_user">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="age"/>
 
    </class>
    
 
</hibernate-mapping>
 

           

4.編寫manageUser類。

package hrbust.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import hrbust.pojo.User;


public class manageUser {
	public static void main(String[] args) {
		//TODO Auto-generated method stub
		Configuration cfg =null;
		SessionFactory sf = null;
		Session session = null;
		Transaction ts =null;
		
		User u = new User();
		u.setName("張三");
		u.setAge(21);

	try{
		cfg = new Configuration().configure();
		sf = cfg.buildSessionFactory();
		session = sf.openSession();
		ts =session.beginTransaction();
		session.save(u);
		ts.commit();
	} catch (HibernateException e) {
		//TODO Auto-generated catch block
		e.printStackTrace();
		if(ts!= null){
			ts.rollback();
		}
	}finally {
		session.close();
		sf.close();}

}}
           

至此,基本配置結束。

繼續閱讀