hibernate(三)入门级--hibernate实例

本文探讨了软件框架轻重量级的区别标准,并通过Hibernate的实际应用案例,展示了其作为轻量级框架如何简化数据库操作并提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于框架,如何区分轻重量级?


 

在介绍我的小demo之前,我想来讨论下这个问题。现在当下的几个观点是


 

1.启动是耗费的系统资源多就是重量级

2.开发包很大就是重量级

3.对于程序的侵入性大就是重量级

 

对于第一个观点现在绝大部分主流还是这样认识的,但是有这样一个问题,spring在仅仅需要最低需求时,需要的jar是非常少的,

当然也是能实现基本功能的,启动耗费的资源也是很小,而如果要实现很多spring的功能就需要很多的资源和jar,这时候按照这个

观点来讲,它既是轻量级也是重量级,所以这个观点还是片面的。

 

对于第二个观点,那几本菜菜开始时候的认识了,不做评论了。第三个观点,个人还是比较认可这个观点的这样来讲的话,对于spring来讲就是轻量级的,而ejb就是重量级的了。而hibernate就是一个轻量级的框架,struts也是轻量级。当然也没有说轻量级就是好的,重量级就是笨重的。只是在不同的应用层次和逻辑处理情境中,各自有各自的优势。ejbj2ee企业级解决方案有他的优势,一般中小型企业一般是SSH为基础的框架开发。

 

开发环境:hibernate 3.2

 Myeclipse 10

 

配置hibernate环境



开发包



计算机生成了可选文字:刀doc毋eg毋etc奋gr。mm。r应圣‘b_一…KBKBKBKBKB一KB一KBKBSFCtestbuild.batbui!d.s卜build.xmlchangelog.城卜ibernate-logo.giflgpl.txtreadme.txt3512622,128272翻山圈口QQ园应参”'"e『na,e3.j'r一2006/10/1522:04文件夹2006/10/1522;O4文件夹2006/10/1522:O4文件夹2006/10/1522:04文件夹2006/10/1522:O4文件夹2006/10/1522:04文件夹2006/10/1522:04文件夹2005/11/107:08Windows批处理…2006/2/229:455日文件2006/10/1522:04XMLDocument2006/10/1521:48TeXtDocument20(川6/311:31GIF文件2006/10/1521:53WinRAR压缩文件20(脚6/311:31TeXtDocument2006/10/1521:48TeXtDocumentQ。

 

主要为这两个,在hiberante下的lib下全部包,以及hibernate3核心包,另外根据所需数据库加载特定的数据库驱动即可。

 


配置文件


 

*.hbm.xml



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="com.cfl.hibernate"	>

	<class name="Person" table="t_person" >
		<id name="id">
			<generator class="uuid"/>
		</id>
		
		<property name="name" />
		
		<property name="age"/>
		
	</class>
	
</hibernate-mapping>


 

主要是实体对应的映射文件,这个主要是hibernate用来形成动态sql语句的配置文件

 

Hibernate.cfg.xml/hibernate.properties


<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
				<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<mapping resource="com/cfl/hibernate/Person.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>


 

主要是配置连接数据库的配置,包括

1.配置连接数据库驱动

2.数据库方言

3.数据库用户名

4.数据库密码

5.数据库连接url

6.加载数据库资源文件



 

测试配置



保存数据

 

package com.cfl.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class GetDB {
	public static void main(String[] args){
		
		Configuration cfg=new Configuration().configure();
		SchemaExport export=new SchemaExport(cfg);
		export.create(true,true);
		savePerson();
	}
	public static void  savePerson(){
		//读取hibernate.cfg.xml文件,默认即hibernate.cfg.xml
		Configuration configuration=new Configuration().configure();
		
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		
		Session session=null;
		
		try {
			Person person=new Person();
			person.setName("cfl");
			person.setAge("24");
			
			session=sessionFactory.openSession();
			session.beginTransaction();
			session.save(person);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
		}finally{
			
			session.close();
			sessionFactory.close();
			
		}
	}
}


 

写在后面



从上面简单的demo里面,可以得知简单几点hibernate给我们带来的


1.封装了jdbc的操作,让程序员更加专注于程序设计,而不是数据库操作

2.无缝连接对象模型和关系模型,应该算是对于在持久层对于面向对象的补充

3.屏蔽数据库sql语句,这个怎么讲呢?既是好的地方,也是不好的味道。好的地方是,无需再去拼装boring的sql语句;

不好的味道是失去了作为原生sql的灵活性,所以在很多实际过程中并不是一股脑的使用hibernate,该用原生sql的地方还是会使用。



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏修

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值