zoukankan      html  css  js  c++  java
  • hibernate 的映射文件 配置文件 实体bean

    实体bean,文件名字:Employee.java

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package cn.toher.hibernate.model;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    /**
     *
     * @author Administrator
     */
    @Entity
    public class Employee {
        private String id;
        private String firstName; 
        private String lastName;   
        private int salary;  
    
        public Employee() {}
        public Employee(String fname, String lname, int salary) {
            this.firstName = fname;
            this.lastName = lname;
            this.salary = salary;
        }
        @Id
        public String getId() {
            return id;
        }
        public void setId( String id ) {
            this.id = id;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName( String first_name ) {
            this.firstName = first_name;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName( String last_name ) {
            this.lastName = last_name;
        }
        public int getSalary() {
            return salary;
        }
        public void setSalary( int salary ) {
            this.salary = salary;
        }
    }

    映射文件  文件名字:Employee.hbm.xm<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC 

     "-//Hibernate/Hibernate Mapping DTD//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
    
    <hibernate-mapping>
    //这是完整的包名,如果没有回报找不到实体bean的错误
    <class name="cn.toher.hibernate.model.Employee" table="EMPLOYEE"> <meta attribute="class-description"> This class contains the employee detail. </meta>
    // 这里面的string的s 是小写的,如果写成大写的回报错误
    <id name="id" type="string" column="id">

    //id生成策略
    <generator class="uuid"/> </id> <property name="firstName" column="first_name" type="string"/> <property name="lastName" column="last_name" type="string"/> <property name="salary" column="salary" type="int"/> </class> </hibernate-mapping>

    配置文件 文件名字:hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!--
    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    
    Copyright (c) 2008, 2016 Oracle and/or its affiliates. All rights reserved.
    
    Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    Other names may be trademarks of their respective owners.
    
    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common
    Development and Distribution License("CDDL") (collectively, the
    "License"). You may not use this file except in compliance with the
    License. You can obtain a copy of the License at
    http://www.netbeans.org/cddl-gplv2.html
    or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    specific language governing permissions and limitations under the
    License.  When distributing the software, include this License Header
    Notice in each file and include the License file at
    nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    particular file as subject to the "Classpath" exception as provided
    by Oracle in the GPL Version 2 section of the License file that
    accompanied this code. If applicable, add the following below the
    License Header, with the fields enclosed by brackets [] replaced by
    your own identifying information:
    "Portions Copyrighted [year] [name of copyright owner]"
    
    If you wish your version of this file to be governed by only the CDDL
    or only the GPL Version 2, indicate your decision by adding
    "[Contributor] elects to include this software in this distribution
    under the [CDDL or GPL Version 2] license." If you do not indicate a
    single choice of license, a recipient has the option to distribute
    your version of this file under either the CDDL, the GPL Version 2 or
    to extend the choice of license to its licensees as provided above.
    However, if you add GPL Version 2 code and therefore, elected the GPL
    Version 2 license, then the option applies only if the new code is
    made subject to such option by the copyright holder.
    
    Contributor(s):
    -->
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>     
          <!--表明以下的配置是针对session-factory配置的,SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作-->     
          <session-factory>   
              <!-- 1,数据库连接信息: -->       
              <!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序-->     
              <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>     
              <!--设置数据库的连接url:jdbc:mysql://localhost:3306/dbname,其中localhost表示mysql服务器名称,此处为本机,    dbname是数据库名-->      
              <property name="hibernate.connection.url">jdbc:sqlserver://192.168.1.131:1433;DatabaseName=Test</property>      
              <!--连接数据库是用户名-->     
              <property name="hibernate.connection.username">sa</property>     
              <!--连接数据库是密码-->     
              <property name="hibernate.connection.password">123456a</property>   
              <!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->     
              <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>   
                
              <!-- 以下信息可选 -->  
              <!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助-->     
    <!--          <property name="jdbc.use_scrollable_resultset">false </property>     
              connection.useUnicode连接数据库时是否使用Unicode编码     
              <property name="Connection.useUnicode">true </property>     
              connection.characterEncoding连接数据库时数据的传输字符集编码方式,     
              <property name="connection.characterEncoding">utf-8 </property>        -->
                         
    <!--          2,数据库连接池信息:    注意:现在还用不上,所以先注销掉 
              <property name="hibernate.connection.pool.size">20 </property>     
               指定连接池的最大连接个数,使用连接池需要加载所有的链接池的JAR文件,JAR文件在Hibernate文件夹下的“liboptionalc3p0”中     
               <property name="hibernate.c3p0.max_size">30</property>  
               <property name="hibernate.c3p0.min_size">10</property>   
               <property name="hibernate.c3p0.timeout">5000</property> 指定连接池里连接超时时长,即最大时间    -->
                 
              <!--3,数据库一次操作时的记录数::-->     
              <!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->     
    <!--          <property name="jdbc.fetch_size">50 </property>     
              jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大     
              <property name="jdbc.batch_size">23 </property>     -->
                
                 
              <!--4,是否显示sql:-->            
              <!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->     
              <property name="hibernate.show_sql">true </property>   
      
                
                
              <!--  
               create: 先删表,再建表。  
               create-drop: 启动时建表,退出前删表。  
               update: 如果表结构不一致,就创建或更新。  
               validate: 启动时验证表结构,如果不致就抛异常。  
              -->  
              <property name="hibernate.hbm2ddl.auto">update</property> 
                
              <!-- 5,开启二级缓存使用: 4.X二级缓存里面缺少很多包,所以先别用-->   
    <!--          <property name="hibernate.cache.use_second_level_cache">true</property>  
              <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 指定缓存产品所需的类   
              <property name="hibernate.cache.use_query_cache">true</property> 启用查询缓存   -->
                
                             
               <!--6,指定映射文件,可映射多个映射文件,映射的就是实体类,这是基于XML映射文件的-->            
              <!--<mapping resource="cn/toher/hibernate/model/Student.hbm.xml" />-->     
              <!--这是基于Annotation注解的-->
            <mapping resource="cn/toher/hibernate/model/Employee.hbm.xml" />
              <!--<mapping class="cn.toher.hibernate.model.Employee" />-->     
    <!--          <mapping class="cn.toher.hibernate.model.OutboundLog" />     
              <mapping class="cn.toher.hibernate.model.AfterSaleLog" />     -->
      </session-factory>     
      </hibernate-configuration>   
  • 相关阅读:
    禁止Crontab产生邮件
    centos6.2 ftp 配置
    [转] 夏天折T恤的方法
    [转] 55个经典开源Windows工具(2)
    [转] 55个经典开源Windows工具(3)
    [**轻松一下**] 程序员喝酒喝出的计算机文化!
    [转] 55个经典开源Windows工具(1)
    [转] 关于mscomm的用法,提高篇
    [转] 传说中的减肥............
    [转] 关于MSCOMM控件的一些说明
  • 原文地址:https://www.cnblogs.com/itchenfirst/p/7492069.html
Copyright © 2011-2022 走看看