zoukankan      html  css  js  c++  java
  • hibernate用配置文件的方式实现orm

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

    思路: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的代码如下:
     1 package com.qls.domain;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * Created by 秦林森 on 2017/5/21.
     7  */
     8 public class Person {
     9     private Integer id;
    10     private String  name;
    11     private Date enterCampusDate;
    12 
    13     public Integer getId() {
    14         return id;
    15     }
    16 
    17     public void setId(Integer id) {
    18         this.id = id;
    19     }
    20 
    21     public String getName() {
    22         return name;
    23     }
    24 
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28 
    29     public Date getEnterCampusDate() {
    30         return enterCampusDate;
    31     }
    32 
    33     public void setEnterCampusDate(Date enterCampusDate) {
    34         this.enterCampusDate = enterCampusDate;
    35     }
    36 }

    Person.hbm.xml的文件的代码如下:

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.qls.domain">
     7     <class name="Person" table="person">
     8         <id name="id" column="person_id">
     9             <generator class="native"/>
    10         </id>
    11         <property name="name"/>
    12         <property name="enterCampusDate" type="timestamp"/>
    13     </class>
    14 </hibernate-mapping>

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

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!--
     3   ~ Hibernate, Relational Persistence for Idiomatic Java
     4   ~
     5   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
     6   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
     7   -->
     8 <!DOCTYPE hibernate-configuration PUBLIC
     9         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    10         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    11 
    12 <hibernate-configuration>
    13 
    14     <session-factory>
    15 
    16         <!-- Database connection settings -->
    17         <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    18         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    19         <property name="connection.username">scott</property>
    20         <property name="connection.password">a123456</property>
    21 
    22         <!-- JDBC connection pool (use the built-in) -->
    23         <property name="connection.pool_size">10</property>
    24 
    25         <!-- SQL dialect -->
    26         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    27         <!-- Echo all executed SQL to stdout -->
    28         <property name="show_sql">true</property>
    29 
    30         <!-- Drop and re-create the database schema on startup -->
    31      <!--   <property name="hbm2ddl.auto">create</property>-->
    32 
    33         <!-- Names the annotated entity class -->
    34         <mapping class="com.qls.test.Ouyangfeng"/>
    35         <mapping class="com.qls.domain.DiaoChan"/>
    <!--Person.hbm.xml文件的位置--> 36 <mapping resource="com/qls/configurationFile/Person.hbm.xml"/> 37 </session-factory> 38 39 </hibernate-configuration>

     测试类的代码如下:

     1 package com.qls.test;
     2 
     3 import com.qls.domain.Person;
     4 import org.hibernate.Session;
     5 import org.hibernate.SessionFactory;
     6 import org.hibernate.Transaction;
     7 import org.hibernate.cfg.Configuration;
     8 
     9 import java.text.ParseException;
    10 import java.text.SimpleDateFormat;
    11 
    12 /**
    13  * Created by ${秦林森} on 2017/5/21.
    14  */
    15 public class Test3 {
    16     public static void main(String[] args) throws  Exception{
            //得到会话工厂
    17 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 18 Session session = sessionFactory.openSession(); 19 Transaction tx = session.beginTransaction();//开启事务。 20 Person person = new Person(); 21 person.setName("王昭君");
          //赋予特定的时间类型格式。
    22 person.setEnterCampusDate(new SimpleDateFormat("yyyy_MM_dd").parse("0093_07_01")); 23 session.save(person); 24 tx.commit();//提交事务。 25 //关闭回话: 26 session.close(); 27 } 28 }
  • 相关阅读:
    Python 学习日记 第七天
    Python 学习日记 第六天
    Python 学习日记 第五天
    Python 学习日记 第四天
    Redis 中的数据类型及基本操作
    Asp.net mvc 中View 的呈现(二)
    Asp.net mvc 中View的呈现(一)
    Asp.net mvc 中Action 方法的执行(三)
    Asp.net mvc 中Action 方法的执行(二)
    Asp.net mvc 中Action 方法的执行(一)
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/6884579.html
Copyright © 2011-2022 走看看