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

思路: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();
}
}

最新文章

  1. JUnit报错需导入两个jar包
  2. (转)php-curl响应慢(开发微信授权登陆时碰到的问题)
  3. AngularJS中使用service,并同步数据
  4. 在SQL中使用CLR提供基本函数对二进制数据进行解析与构造
  5. 异常详细信息: System.Data.SqlClient.SqlException:用户 &#39;IIS APPPOOL\DefaultAppPool&#39; 登录失败解决办法
  6. ERP联系记录管理(十七)
  7. java StreamTokenizer使用
  8. hdu 3952
  9. java 单例模式及getInstance的好处
  10. 关于canvas画布使用fillRect()时高度出现双倍效果解决办法
  11. BZOJ 2631: tree [LCT splay区间]
  12. JavaScript压缩工具JSA使用介绍
  13. 网络Socket编程及实例
  14. java线程学习之wait方法
  15. Deepfakes:AI换脸技术自制明星XX片
  16. 缓存Memcached 与 Redis 相同点差异点分析
  17. 2015/11/4用Python写游戏,pygame入门(4):获取鼠标的位置及运动
  18. HDU 5117 Fluorescent (数学+状压DP)
  19. Java反射机制的基本概念与使用
  20. 创建mysql表

热门文章

  1. 4.1 基本类型和引用类型的值【JavaScript高级程序设计第三版】
  2. OLAP和OLTP
  3. python -- sftp的方式下载终端文件
  4. No module named &#39;PyQt5.sip&#39;
  5. 2,版本控制git --分支
  6. Internet接入方式
  7. Pascal小游戏 贪吃蛇
  8. python学习笔记六:内置函数
  9. ASP NET Core ---FluentValidation
  10. Python全栈工程师(每周总结:1)