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

思路: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. HTML5--混合布局

    1.先上效果图,大家来看看 2.代码如下: <!doctype html> <meta charset='utf-8' content='text/html' /> <h ...

  2. python__系统 : socket_TCP相关

    tcp和udp对比起来.还是tcp相对稳定一些,但是因为有三次挥手和四次握手,以及确认包(ack)的存在,可能在速度上会比udp慢. 用python的socket模块可以建立tcp服务端: from ...

  3. 使用file_get_contents()和curl()抓取网络资源的效率对比

    使用file_get_contents()和curl()抓取网络资源的效率对比 在将小程序用户头像合成海报的时候,用到了抓取用户头像对应的网络资源,那么抓取方式有很多,比如 file_get_cont ...

  4. yii2 的登录注册 轮子

    //利用到了yii2 框架之中的验证规则 进行判定而已 也不是很高深的东西  但是 使用框架自身的轮子 会有安全性能的隐患 1注册reg controller 中 我都以admin 为例子 publi ...

  5. python web框架的介绍

    随着互联网的兴起,web开发变得愈发的重要.Python作为当前火热的语言, 其中的web开发框架可以说是百花齐放,下面聊一聊这些框架. 一: 基于Python的代表性Web框架 Django Dja ...

  6. 8,Linux系统基础优化及常用命令

    Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和 ...

  7. win32 signal

    Remarks   The signal function enables a process to choose one of several ways to handle an interrupt ...

  8. Github前端项目排名

      Github前端项目排名(2016-04-04) 一.前言 近几年前端技术日新月异,从 RequireJS 到 AngularJS 再到 React,似乎每天都有新的技术诞生.而大神们总能第一时间 ...

  9. 站在C#和JS的角度细谈函数式编程与闭包

    1.函数式编程是什么? 摘自百度的说法是.函数式编程是种编程典范,它将电脑运算视为函数的计算.函数编程语言最重要的基础是 λ 演算(lambda calculus).而且λ演算的函数可以接受函数当作输 ...

  10. hasOne

    public boolean hasOne(int n) { int lastdigit=0; while( n >0 ){ lastdigit=(n % 10); if(lastdigit== ...