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、在配置映射中(实体类与数据库中表的字段的时候)主键配置过之后,后面不需要重复进行配置

二、配置


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>


猜你喜欢

转载自blog.csdn.net/lei_1994/article/details/78185546