|
值得关注的持久化技术: hibernate
发表日期:2008-1-5
|
1.简述: 学习一项新的技术要花时间的,它会不会把时间在使用它之后补回来是应该关注的问题:我们可以用要害字:"jdo ejb cmp hibernate"在Google上找一找对他们的评论文章,其中只要使用过hibernate的人都对它很推崇(我也是). 我们的对象模型可以被hibernate很好的利用,下图中的Persistent Object是简单的业务实体对象(要被持久化的对象)。通过hibernate被透明的持久化到数据库中。下面的例子会说明一切。 .  2.一个简单的例子 我们开发一个Person类: .  可以想象数据表是下面的样子: 表person .  要通过hibernate实现它要经过以下步骤: 主要是先根据对象模型编写简单的业务实体类(简单的bean规范类,即有get,set方法的类,可以用jac等工具生成),再用XML映射文件描述其映射数据库的方式(很easy),最后就可以通过很少的hibernate api写测试类对其进行持久化操作。 2.1 写Person类(Persistent Object),它和普通的类没甚末不同,但注重它应该复合bean的规范,为每个属性提供get,set方法: h.Person // // WARNING: this file has been automatically generated by JAC // DO NOT MODIFY unless you know what you are doing // package h; public class Person { String name; /** Sets the value of field name. @param name value of field name */ public void setName(String value) { this.name=value; } /** Gets the value of field name. @return value of field name */ public String getName() { return name; } String address; /** Sets the value of field address. @param address value of field address */ public void setAddress(String value) { this.address=value; } /** Gets the value of field address. @return value of field address */ public String getAddress() { return address; } String id; /** Sets the value of field id. @param id value of field id */ public void setId(String value) { this.id=value; } /** Gets the value of field id. @return value of field id */ public String getId() { return id; } } 2.2 填写对象-关系映射xml文件,不用担心它的复杂程度,我们可以抄嘛:和所有o-r 映射工具一样都要写这种映射文件。 Person.hbm.xml 建议命名为:"类名"+"hbm.xml" 并且放置在Person类相同包目录下 <?xml version="1.0" encoding="GB2312"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="h.Person"> <!--hibernate为我们生成主键id--> <id name = "id" unsaved-value = "null"> <generator class="uuid.hex"/> </id> <!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式--> <property name="name"/> <property name="address"/> </class> </hibernate-mapping> 2.3 TestPerson 我们用hibernate api来进行持久化Person h.TestPerson package h; import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaEXPort; class TestPerson{ private static SessionFactory sessions; public static void main(String[] args) throws Exception{ //配置环境,分析xml映射文件 Configuration conf= new Configuration() .addClass(Person.class); //生成并输出sql到文件(当前目录)和数据库 SchemaExport dbExport=new SchemaExport(conf); dbExport.setOutputFile("build\\sql.txt"); dbExport.create(true, true); sessions = conf.buildSessionFactory(); //以上都是些固定格式的环境配置 //start...... Session s = sessions.openSession(); Transaction t = s.beginTransaction(); //1.用普通使用方式建立对象,填充数据 Person p1=new Person(); p1.setName("陈鹏"); p1.setAddress("西安东郊黄陵"); //2.持久化 s.save(p1); //此时p1已经可以在数据库中找到 t.commit(); s.close(); } } 2.4 为了运行当然要配置数据库:我以mysql数据库为例子:(只用劳动1次即可) hibernate.properties 在hibernate源程序的根目录可以找到此文件模板,copy到我们的类的根目录。即:“..\h” ## MySQL ## 前两行人家都给你填好了,只用填数据库连接和username,passWord hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class org.gjt.mm.mysql.Driver hibernate.connection.url jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK hibernate.connection.username root hibernate.connection.password 2.5 运行TestPerson类,在mysql中观察create好的表person和其中的数据 ok!整个过程主要的是前3步:写bean类(Persistent Object),写o-r映射文件,写存取测试类。 3. 复杂关系例子 我们换一个稍微复杂的例子来看看: .  可以想象数据表是下面的样子: 表person .  表Company .  3.1 Person(已有),Company类:Persistent Object h.Company package h; public class Company { String id; public void setId(String value) { this.id=value; } public String getId() { return id; } String name; public void setName(String value) { this.name=value; } public String getName() { return name; } Java.util.List employees=new java.util.Vector(); public void setEmployees(java.util.List value) { this.employees=value; } public java.util.List getEmployees() { return employees; } public void addEmployee(h.Person value) { employees.add(value); } public void removeEmployee(h.Person value) { employees.remove(value); } public void clearEmployees() { employees.clear(); } } 3.2 填写对象-关系映射xml文件,Person.hbm.xml照旧 Company.hbm.xml 也放到h.Company类相同目录下 <?xml version="1.0" encoding="GB2312"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="h.Company"> <!--hibernate为我们生成主键id--> <id name = "id" unsaved-value = "null"> <generator class="uuid.hex"/> </id> <property name="name"/> <!--1:n关系的映射--> <list name="employees" cascade="all"> <key column="company_id"/> <index column="posn"/>
|
|
上一篇:我为什么学习Hibernate
人气:650
下一篇:spring关于底层资源的抽象
人气:373 |
浏览全部Java的内容
Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐
|
|