对于框架,如何区分轻重量级?
在介绍我的小demo之前,我想来讨论下这个问题。现在当下的几个观点是
1.启动是耗费的系统资源多就是重量级
2.开发包很大就是重量级
3.对于程序的侵入性大就是重量级
对于第一个观点现在绝大部分主流还是这样认识的,但是有这样一个问题,spring在仅仅需要最低需求时,需要的jar是非常少的,
当然也是能实现基本功能的,启动耗费的资源也是很小,而如果要实现很多spring的功能就需要很多的资源和jar,这时候按照这个
观点来讲,它既是轻量级也是重量级,所以这个观点还是片面的。
对于第二个观点,那几本菜菜开始时候的认识了,不做评论了。第三个观点,个人还是比较认可这个观点的这样来讲的话,对于spring来讲就是轻量级的,而ejb就是重量级的了。而hibernate就是一个轻量级的框架,struts也是轻量级。当然也没有说轻量级就是好的,重量级就是笨重的。只是在不同的应用层次和逻辑处理情境中,各自有各自的优势。ejb在j2ee企业级解决方案有他的优势,一般中小型企业一般是SSH为基础的框架开发。
开发环境:hibernate 3.2
Myeclipse 10
配置hibernate环境
开发包
主要为这两个,在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的地方还是会使用。