Hibernate的基本架构和第一个简单工程(2)

首先关于它的基本架构:


大概启动顺序便是:

1.应用程序首先读取config文件hibernate.cfg.xml,config文件会配置两种信息:有关数据库的信息和有关映射文件.hbm.xml的信息。

2.由config文件的对象建立sessionfactory,并创建session。

3.session通过事务来对数据库进行操作。


基本的工程结构可以是这样:



hibernate.cfg.xml的基本配置:

<?xml version='1.0' encoding='utf-8'?>

<!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 -->

		<!-- 设置数据库的驱动程序,此处为mysql的 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

		<!--设置数据库的连接,如:jdbc:mysql://localhost/hibernate,其中localhost表示mysql服务器名称,此处为本机, 
			hibernate是数据库名 -->
		<property name="connection.url">jdbc:mysql://localhost/animal</property>

		<!-- 用户名 密码 -->
		<property name="connection.username">XXX</property>
		<property name="connection.password">XXXXXX</property>

		<!-- JDBC connection pool (use the built-in) -->
		<!-- 数据库连接池的大小 -->
		<property name="connection.pool_size">1</property>

		<!-- SQL dialect -->
		<!-- 数据库方言 数据库虽遵循标准 但也有自己定制化的差异 -->
		<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

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

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup -->
		<!--1.create-drop : hibernate一开始就发送一个drop语句,再执行create语句创建表,当sessionFactory关闭时又执行drop语句删除表。结果就是,当表存在时,首先会被drop掉,然后又创建,最终被drop,如果表不存在,一开始虽然hibernate还是会发送drop语句,当时因为表不存在而drop失败。 
			2.create:hibernate首先会发送一个drop语句,如果表存在则会被drop掉,然后hibernate再发送create语句创建表。 
			3.update:hibernate会首先查询数据库看是否存在此表,如果存在则不管,如果不存在则会先发送一个create语句创建一个表。 
			4.validate:每次插入数据之前都会验证数据库中的表结构和hbm文件的结构是否一致。如果表不存在,则报错。
			 -->
		<property name="hbm2ddl.auto">create</property>


		<mapping resource="liu/maggie/persistence/Animal.hbm.xml" />

	</session-factory>

</hibernate-configuration>

animal.hbm.xml的配置:

<hibernate-mapping>
	<class name="liu.maggie.persistence.Animal">
		<id name="id"></id>
		<property name="name"></property>
		<property name="species"></property>
	</class>
</hibernate-mapping>

Animal实体:

package liu.maggie.persistence;

public class Animal {
	int id;
	String name;
	String species;
	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 String getSpecies() {
		return species;
	}
	public void setSpecies(String species) {
		this.species = species;
	}
	
	
}

AddAnimal主程序:

package liu.maggie.excute;

import liu.maggie.persistence.Animal;

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


public class AddAnimal {

	public AddAnimal() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Configuration config= new Configuration().configure();
		SessionFactory factory=config.buildSessionFactory();
		
		Session session=null;
		
		try {
			session=factory.openSession();
			session.beginTransaction();
			
			Animal animal=new Animal();
			animal.setId(3);
			animal.setName("hh");
			animal.setSpecies("dog");
			
			session.save(animal);
			session.getTransaction().commit();
			
		} catch (Exception e) {
			// TODO: handle exception
			
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			if(session!=null){
				if(session.isOpen()){
					session.close();
				}
			}
		}
		
		
	}

}


至此,一个最简单的工程完成了,但是仍然还有许多问题需要解决

1.只是使用xml方式映射,那使用注解又该怎样实现呢?

2.这里只实现了最简单的增加功能,还有其他复杂的查询删除等操作又该怎样完成呢?


由于我是按照于亮的【ssh进阶之路】来学习的,所以在解决自己的疑惑同时还是会按照教程的路来走。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值