Hibernate入门--第一个实例

Hibernate:将对关系数据库的操作转换为面向对象的操作。

ORM技术就是关系数据库的操作和面向对象操作相互转换的一种技术。

 

 

创建一个Hibernate的Java应用实例步骤:<?xml:namespace prefix = o />

 

(1)加载hibernate.jar包以及lib中为hibernate提供支持的jar包

 

(2)加载JDBC驱动包

 

(3)创建User类,包括三个属性:id(唯一标识),name(姓名),birthday(日期)

 

(4)为User类配置映射文件

 

(5)配置Hibernate的默认配置文件hibernate.cfg.xml

 

(6)编写主程序代码进行验证。

 

User.java类;

 

<divre mce_tmp="1"></divre>

package cn.itcast.hibernate.domain;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;
	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 Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

  

 

User.hbm.xml映射文件:

 

<?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是根元素
-->
<hibernate-mapping package="cn.itcast.hibernate.domain">
    <class name="User" table="user">
    	<id name="id" type="int" column="id" unsaved-value="1">
    		<generator class="native"></generator>
    	</id>
    	<property name="name" type="string" column="name"></property>
    	<property name="birthday" type="date" column="birthday"></property>
    </class>
    
</hibernate-mapping>

 

 

hibernate.cfg.xml配置文件:

 

<divre mce_tmp="1"></divre>

<?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是连接配置文件的根元素  -->
<hibernate-configuration>
	
	<session-factory>
	
		<!-- 驱动连接字符串 -->
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>	
		
		<!-- 连接数据库的URL -->
		<property name="connection.url">
			jdbc:mysql://localhost:3306/bookdb
		</property>
		
		<!-- 连接数据库的用户名 -->
		<property name="connection.username">
			root
		</property>
		<!-- 连接数据库的密码 -->
		<property name="connection.password">
			123456
		</property>
		
		<!-- 指定数据库方言 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLInnoDBDialect
		</property>
		
		<!-- 控制台显示SQL语句 -->
		<property name="show_sql">
			true
		</property>
		
		<!-- 根据需要自动创建数据表 -->
		<property name="hbm2ddl.auto">update</property>
		
		
		<!-- 罗列所有的映射文件 -->
		<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml"/>
	</session-factory>

</hibernate-configuration>

  

 

hibernate.cfg.xml配置文件中属性分析:

(1)hbm2ddl.auto属性

<property name="hbm2ddl.auto">create-drop</property><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

在应用运行以前创建一张表,在应用运行之后把表删除,推荐在测试中使用。

 

<property name="hbm2ddl.auto">create</property>

 

在应用运行以前创建一张表,但是在应用运行之后不会把表删除,推荐在测试中使用

 

<property name="hbm2ddl.auto">update</property>

 

不新建表也不删除表,只更新表中的数据。

 

<property name="hbm2ddl.auto">validate</property>

 

对O-R映射表进行检验,如果检验错误,则会报异常。推荐在投入实际运用后使用。因为使用这种方式更为安全。

 

(2)dialect属性

有关使用的方言是否支持事务的问题。

 

 

<!-- 指定数据库方言 -->

 

<property name="dialect">

 

org.hibernate.dialect.MySQLInnoDBDialect

 

</property>

 

该种方言是支持事务的。

 

但是对于MySQLDialect是不支持事务的。所以对MySQL的开发还是使用MySQLInnoDBDialect这种方言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值