天天看點

Hibernate的認識與配置一、認識Hibernate二、配置

一、認識Hibernate

1、了解Hireanate

ORM對象關系映射

具體說明:(1)編寫程式的時候,以面向對象的方式處理資料

  (2)儲存資料的時候,以關系型資料庫進行儲存

2、導包

required下的所有jsr包和資料庫驅動包ojdbc14.jar包

3、資料庫連接配接的配置路徑

安裝包下面的

documentation\quickstart\html_single\hibernate-tutorials\basic\src\test\resources

4、在映射的關系中,實體類中的類型需要與資料庫表中對應的字段類型需要對應;

5、實體類與資料庫的資訊配置路徑

包下面的

documentation\quickstart\html_single\hibernate-tutorials\basic\src\test\java\org\hibernate\tutorial\hbm

6、資料庫的方言(使用的oracle資料庫)

org.hibernate.dialect.Oracle10gDialect

RDBMS 方言

DB2 org.hibernate.dialect.DB2Dialect

DB2 AS/400 org.hibernate.dialect.DB2400Dialect

DB2 OS390 org.hibernate.dialect.DB2390Dialect

PostgreSQL org.hibernate.dialect.PostgreSQLDialect

MySQL org.hibernate.dialect.MySQLDialect

MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect

MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect

Oracle (any version) org.hibernate.dialect.OracleDialect

Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect

Oracle 11g      org.hibernate.dialect.Oracle10gDialect

Sybase org.hibernate.dialect.SybaseDialect

Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect

Microsoft SQL Server org.hibernate.dialect.SQLServerDialect

SAP DB org.hibernate.dialect.SAPDBDialect

Informix org.hibernate.dialect.InformixDialect

HypersonicSQL org.hibernate.dialect.HSQLDialect

Ingres org.hibernate.dialect.IngresDialect

Progress org.hibernate.dialect.ProgressDialect

Mckoi SQL org.hibernate.dialect.MckoiDialect

Interbase org.hibernate.dialect.InterbaseDialect

Pointbase org.hibernate.dialect.PointbaseDialect

FrontBase org.hibernate.dialect.FrontbaseDialect

Firebird

org.hibernate.dialect.FirebirdDialect

7、主鍵配置中關于主鍵唯一性表中建立序列之後的配置

<generator class="sequence">

<param name="sequence">hibernate_id</param>

</generator>

8、在配置映射中(實體類與資料庫中表的字段的時候)主鍵配置過之後,後面不需要重複進行配置

二、配置

Hibernate的認識與配置一、認識Hibernate二、配置

1、UserInfo.java代碼

package com.lyd.entity;

import java.util.Date;

public class UserInfo {
		//使用者的id
		private long userId;
		//使用者的姓名
		private String userName;
		//使用者的密碼
		private String password;
		//使用者的性别
		private String sex;
		//使用者的生日
		private Date birthday; 
		//使用者的身份證号碼
		private String identity;
		//使用者的郵箱
		private String email;
		//使用者的電話号碼
		private String mobile;
		//使用者的位址
		private String address;
		//使用者的狀态
		private String status;
		public long getUserId() {
			return userId;
		}
		public void setUserId(long userId) {
			this.userId = userId;
		}
		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 String getSex() {
			return sex;
		}
		public void setSex(String sex) {
			this.sex = sex;
		}
		public Date getBirthday() {
			return birthday;
		}
		public void setBirthday(Date birthday) {
			this.birthday = birthday;
		}
		public String getIdentity() {
			return identity;
		}
		public void setIdentity(String identity) {
			this.identity = identity;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		public String getMobile() {
			return mobile;
		}
		public void setMobile(String mobile) {
			this.mobile = mobile;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
}
           

2、UserInfo.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 package="com.lyd.entity">
<!-- 
	class:實體類對象和資料庫表做對應
	name:實體類的類名
	table:對應的資料庫的表名 -->
    <class name="UserInfo" table="HWUA_USER">
    <!-- id:表的主鍵 
    	 name:實體類中的屬性
    	 column:資料庫中表的主鍵的列名-->
        <id name="userId" column="HU_USER_ID">
        <!-- 主鍵生成的方式  注意是否是序列-->
            <!-- <generator class="uuid"/> -->
            <!-- 序列的配置格式 -->
	<generator class="sequence">
		<param name="sequence_name">SEQ_USER</param>
	</generator>
        </id>
        
        <!-- 
        	property 實體類中的普通屬性
        	name:實體類中的屬性
        	column:資料庫中表的字段名
        	type:字段的類型
         -->
         <!-- 預設類型是字元串 -->
     <!--    <property name="date" type="timestamp" column="EVENT_DATE"/> -->  
        <property name="userName"  column="HU_USER_NAME"/>
        <property name="password"  column="HU_PASSWORD"/>
        <property name="sex"  column="HU_SEX"/>
        <property name="birthday" type="date" column="HU_BIRTHDAY"/>
        <property name="identity"  column="HU_IDENTITY_CODE"/>
        <property name="email"  column="HU_EMAIL"/>
        <property name="mobile"  column="HU_MOBILE"/>
        <property name="address"  column="HU_ADDRESS"/>
        <property name="status"  column="HU_STATUS"/>
    </class>

</hibernate-mapping>
           

3、UserDaoImpl.java代碼

package com.lyd.userdaoimpl;

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

import com.lyd.entity.UserInfo;
import com.lyd.userdao.UserDao;

public class UserDaoImpl implements UserDao{

	@Override
	public void saveProjectInfo() {
		//加載配置
		Configuration config=new Configuration().configure();
		//建立sessionFactory
		SessionFactory factory=config.buildSessionFactory();
		//建立session
		Session session=factory.openSession();
		//打開事務
		Transaction tx=session.beginTransaction();
		//操作對象
		UserInfo u=new UserInfo();
		u.setUserName("hahaha");
		u.setPassword("123456");
		u.setAddress("hubei");
		//儲存資料對象
		session.save(u);
		//送出事務
		tx.commit();
		//關閉會話
		session.close();
	}
	//測試一波
	public static void main(String[] args) {
		UserDaoImpl udi=new UserDaoImpl();
		udi.saveProjectInfo();
	}
}
           

4、hibername.xml代碼

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ 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>

<!-- 資料庫的配置 -->
        <!-- Database connection settings -->
		<!-- 資料庫驅動 -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <!-- 資料庫連接配接位址 -->
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <!-- 資料庫的名稱 -->
        <property name="connection.username">lyd</property>
        <!-- 資料庫的密碼 -->
        <property name="connection.password">123456</property>

		<!-- 資料庫連接配接池  暫且不用 -->
        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property> -->

		<!-- 資料庫方言 指定資料庫的類型 -->
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

		<!-- 在運作過程中将sql語句列印在控制台,建議将開發階段将值設定為true -->
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

		<!-- 根據hbm配置檔案 ,對資料庫的表做操作
		create :重建立表(會出現很多的建表資訊)
		create-drop:删除表,再建表
		update:更新表
		none:不對表做操作(隻出現對資料的操作)-->
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">none</property>
	
		<!-- 加入資料庫和對象的映射,也就是實體類與資料庫表中字段的配置路徑 -->
        <mapping resource="com/lyd/entity/UserInfo.hbm.xml"/>

    </session-factory>

</hibernate-configuration>