本文主要讲用配置文件的方式讲如何把一个对象和数据库中的表关联起来,其实用配置文件本质是和用注解的方式是一样的。

思路:1.写一个domain对象(如Person.java)

2.写这个domain对象的配置文件:Person.hbm.xml。这个配置文件主要是把damain对象的属性和列名进行指定。以及domain的表名。主键生成策略。

   3.在hibernate_cfg.xml中映射到Person.hbm.xml :

<mapping resource="com/qls/configurationFile/Person.hbm.xml"/>

domain对象Person的代码如下:
 package com.qls.domain;

 import java.util.Date;

 /**
* Created by 秦林森 on 2017/5/21.
*/
public class Person {
private Integer id;
private String name;
private Date enterCampusDate; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getEnterCampusDate() {
return enterCampusDate;
} public void setEnterCampusDate(Date enterCampusDate) {
this.enterCampusDate = enterCampusDate;
}
}

Person.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.qls.domain">
<class name="Person" table="person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="enterCampusDate" type="timestamp"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml文件的代码如下:

 <?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">a123456</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property>--> <!-- Names the annotated entity class -->
<mapping class="com.qls.test.Ouyangfeng"/>
<mapping class="com.qls.domain.DiaoChan"/>
<!--Person.hbm.xml文件的位置-->
<mapping resource="com/qls/configurationFile/Person.hbm.xml"/>
</session-factory> </hibernate-configuration>

测试类的代码如下:

 package com.qls.test;

 import com.qls.domain.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import java.text.ParseException;
import java.text.SimpleDateFormat; /**
* Created by ${秦林森} on 2017/5/21.
*/
public class Test3 {
public static void main(String[] args) throws Exception{
        //得到会话工厂
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();//开启事务。
Person person = new Person();
person.setName("王昭君");
      //赋予特定的时间类型格式。
person.setEnterCampusDate(new SimpleDateFormat("yyyy_MM_dd").parse("0093_07_01"));
session.save(person);
tx.commit();//提交事务。
//关闭回话:
session.close();
}
}

hibernate用配置文件的方式实现orm的更多相关文章

  1. hibernate用注解的方式实现orm

    hibernate 有两种方式实现把一张表映射成一个对象,一种是配置文件的方式,一种是注解的方式.这里用hibernate提供的注解的方式实现一个对象和一张表之间的对应. 思路: 首先在hiberna ...

  2. hibernate的配置文件,使用XML方式

    <?xml version="1.0" encoding="UTF-8"?> <!-- 标准的XML文件的起始行,version='1.0'表 ...

  3. 2018.10.6 Hibernate配置文件详解-------ORM元数据配置 &&& hibernate主配置文件

    ORM既然是实体与关系数据库的映射,那就需要建立实体和关系数据库之间的基础数据,也可以称为元数据.简单的说就是表示类与表.列与属性(get.set方法)等等之间对应关系的数据. Customer.hb ...

  4. Hibernate的配置文件解析

    配置mybatis.xml或hibernate.cfg.xml报错: <property name="connection.url">jdbc:mysql://loca ...

  5. Spring 集成Hibernate的三种方式

    首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...

  6. Spring整合Hibernate的两种方式

    在使用spring注解整合hibernate时出现"org.hibernate.MappingException: Unknown entity: com.ssh.entry.Product ...

  7. eclipse 新建 maven 项目 添加 spring hibernate 的配置文件 详情

    主要配置文件 pom.xml 项目的maven 配置文件 管理项目所需 jar 依赖支持 web.xml 项目的总 配置文件  :添加 spring和hibernate 支持 applicationC ...

  8. Hibernate常用配置文件详解

    本文转载自:http://blog.csdn.net/csh624366188/article/details/7578939 初学hibernate的童鞋,刚开应该都有这种感觉,hibernate的 ...

  9. Hibernate之深入Hibernate的配置文件

    1.创建Configuration类的对象 Configuration类的对象代表了应用程序到SQL数据库的映射配置.Configuration类的实例对象,提供一个buildSessionFacto ...

随机推荐

  1. php解决sql_server连接问题

    1.首先根据phpinfo()查看当前php版本以及线程安全情况(ts或者nts):2.下载sqlsrv扩展(适用32位php) 下载链接为https://www.microsoft.com/en-u ...

  2. DNS无法区域传送(axfr,ixfr)

    这两天博主在学习dns服务器的配 首先简单介绍一下axfr,ixfr axfr:完全区域传送 ixfr :增量区域传送 主要是在dns主从服务器上面进行备份更新的. ----------------- ...

  3. C语言数组篇(四)二维数组

      二维数组声明: ][] ={{,,},{,,}; //两行 三列         二维数组在声明的时候可以不写行,但一定要写列 ] = {{,},{,,},{}}; //未声明的地方自动补零 二维 ...

  4. 第五章 标准I/O

    5.1 引言 本章说明标准 I/O 库.因为不仅在 UNIX 上,而且在很多操作系统上都实现了此库,所以它由 ISO C 标准说明. 标准 I/O 库处理很多细节,例如缓冲区分配,以优化长度执行 I/ ...

  5. 1,VMware与Centos系统安装

    选择性 pc可以选择 -纯系统 Linux/windows -双系统 Windows+Linux -虚拟化技术 Windows+vmware workstation 服务器 -物理机纯系统 -物理机+ ...

  6. MAC中mongodb的连接遇到的问题及调试

    今天在MAC环境下连接mongodb,遇到了一些报错,最终调试全部搞定.在此特做记录! 首先,mongod启动失败 上面有一句话是 exception in initAndListen: 20 Att ...

  7. P1862 输油管道问题

    P1862 输油管道问题 题目背景 听说最近石油危机 所以想到了这题 题目描述 某石油公司计划建造一条由东向西的主要输油管道.该管道要穿过一个有n口油井的油田.从每口油井都要有一条输油管道沿最短路径( ...

  8. Trident整合Kafka

    首先编写一个打印函数KafkaPrintFunction import org.apache.storm.trident.operation.BaseFunction; import org.apac ...

  9. Android保持屏幕常亮唤醒状态

    第一步:  首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses ...

  10. script async和defer

    1.没有async和defer,html解析时遇到script标签,会先去下载js文件,文件加载完成立即执行,执行完了再开始解析后面的html,是一个顺序流的过程 2.async,加载和渲染后续文档元 ...