学好hibernate系列之了解hibernate配置文件

    xiaoxiao2026-04-17  2

    1.跟往常一样,我觉得要学一个框架或者是语言,首先我们要提出一个很好的问题就是我们为什么要学习hibernate框架?

    我不知道大家是不是会有这些烦恼,在我们编写dao(持久层)代码的时候会不会感到特别的烦,不是因为代码很多,而是代码很多而且很多都是重复的,特别是在封装sql语句的时候,总是要写连接数据,插入语句等等。。反正我就觉得很烦,从那个时候我就开始想有没有什么办法或者是框架可以帮助我们去完成这一部分的内容呢?刚开始的时候我并不知道有hibernate框架,于是我很 努力的去封装sql语句,发现还是很麻烦,知道遇见了hibernate框架,才知道它的好,是一个强大的,高性能的对象关系持久性和对任何Java应用程序的查询服务。

    2.hibernate框架是ORM映射关系框架。

    到底什么是ORM映射关系框架呢?还是那个老话,遇到这种问题我们必须要把ORM补全了,O代表的是Oriented——对象,R对应的是Relationship-------关系,M代表的是Map--------映射,所以说hibernate框架是关系对象映射框架,而且它是轻量级的,并且把事务跟数据库操作封装起来的的框架,它几乎支持所有的数据库。

    3.hibernate怎么做到ORM关系映射的呢?

    我个人理解的话,很简单的概括就是一句话:一个类对应一张数据库里面的表,用的就是*.hbm.xml映射文件。是不是感觉挺简单的呢?那么我们在进入下一个讨论。

    4.我们来谈一谈hibernate框架的运行过程;

    第一步:毫无疑问就是建立一个hibernate.cfg.xml文件,这个是万事的开头。我们最后理解一下这个xml文件里面的内容,我们只要看看它的根节点,就知道了

    <hibernate-configuration>

    <session-factory>

    </session-factory>

    </hibernate-confihuration>

    我们其实要特别的重点记住这两个节点。为什么呢?下面跟着步骤往下走

    第二部:建立实体类Entity,或者说pojo类,亦或是model类

    第三部:建立实体映射文件*.hbm.xml,放在同一个包内

    第四部:检查一下*.hbm.xml文件里面设置的表字段是否是我们想要的,在这里要重点记住

    <hibernate-mapping>

    <class name="" table="">

    <id>

    <column name="">

    <generator class="填主键的类型">//一般常用的有assigned这个主键是我们传进去的值作为主键,native:我们常用的mysql数据库提供的主键,自增长,uuid:hibernate生成主键策略,类似mysql。

    </id>

    </hibernate-mapping>

    第五部:建立hibernate.cfg.xml 跟 *.hbm.xml之间的映射,用<mapping resource="*.hbm.xml"/>,至此我们就完成了hibernate框架的基本配置。

    下面我们再来看看怎么使用这秘籍:(其实也可以分为5步):

    1.前面我们不是说过要注意hibernate.cfg.xml文件里面的两个标签吗?

    <hibernate-configuration>

    <session-factory>

    </session-factory>

    </hibernate-confihuration>

    所以我们

    第一步:

    建立Configuration 对象:Configuration config = new Configuration().config();

    第二步:

    //在hibernate4.0后我们要先建立服务注册对象 ServiceRegistry serviceRegistry = new serviceResigtryBuilder().applySettings(config.getPro[erties()).buildServiceRegistry();

    //然后把serviceRegistry传进config.buildSessionFactory(serviceRegistry);

    建立SessionFactory对象:SessionFactory sf = config.buildSessionFactory();

    第三步:

    建立Session对象 Session session  = sf.openSession();

    第四步:

    建立Transaction对象开始事务  Transaction tx = session.beginTransaction();

    第五步:

    用session调用hibernate框架的api函数进行开发。

    用完记得要记住要关闭session.close();这很重要,不要连接池会溢出;

    下面通过一个例子来演示一下:(在eclipse环境进行,安装好了hibernate-tools)

    建立hibernate.cfg.xml文件:

    <?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> <session-factory> <!-- 配置连接数据库mysql,驱动,URL,username,password,是否用方言 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">yqy</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 是否在控制台打印输出sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 是否在控制台打印sql语句时分组,有格式 --> <property name="hibernate.format_sql">true</property> <!-- 更新数据的时候采取的策略,有create,update,一般用update在更新数据的时候不会先删除表再建表再插数据,但create相反 --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/hibernate/test01/dao/pojo/student.hbm.xml"/> </session-factory> </hibernate-configuration> 第二步:实体类student:

    package com.hibernate.test01.dao.pojo; import java.util.Date; public class student { private int studentId; private String studentName; private Date Birth; private String address; public student() { // TODO Auto-generated constructor stub } public student(int studentId, String studentName, Date birth, String address) { super(); this.studentId = studentId; this.studentName = studentName; Birth = birth; this.address = address; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Date getBirth() { return Birth; } public void setBirth(Date birth) { Birth = birth; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "student [studentId=" + studentId + ", studentName=" + studentName + ", Birth=" + Birth + ", address=" + address + "]"; } } 第三步:建立student.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"> <!-- Generated 2016-8-16 12:29:42 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.hibernate.test01.dao.pojo.student" table="t_student"> <id name="studentId" type="int"> <column name="student_id" /> <generator class="assigned" /> </id> <property name="studentName" type="java.lang.String"> <column name="student_name" /> </property> <property name="Birth" type="java.util.Date"> <column name="Birth" /> </property> <property name="address" type="java.lang.String"> <column name="address" /> </property> </class> </hibernate-mapping> 第四步:我们测试一下:

    package com.hibernate.test01.dao.pojo; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; public class studentTest { @Test public void teststudent() { //注册配置对象 Configuration config = new Configuration().configure(); //注册服务对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sf = config.buildSessionFactory(serviceRegistry); Session session = sf.openSession(); student s = new student(1,"yqy",new Date(),"广州市天河区"); Transaction tx = session.beginTransaction(); session.save(s); tx.commit(); } @Test public void getSessionObj(){ Configuration config = new Configuration().configure(); //注册服务对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sf = config.buildSessionFactory(serviceRegistry); Session session = sf.openSession(); Object o = session.get(student.class, 1); System.out.println(o); } } 结果:查看数据库成功插进去数据。

    转载请注明原文地址: https://ju.6miu.com/read-1308938.html
    最新回复(0)