• Hibernate配置XML连接数据库


    一、hibernate.cfg.xml

    1、配置连接Oracle

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
    
            <property name="hbm2ddl.auto">create</property>
            <property name="dialect">  org.hibernate.dialect.Oracle9Dialec  </property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE </property>
            <property name="connection.username">system</property>
            <property name="connection.password">root</property>
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            
         <property name="format_sql">true</property>
         <property name="show_sql">true</property>
            
            
            <mapping resource="cn/wym/hibernate/Users.hbm.xml" />
     
        </session-factory>
    </hibernate-configuration>

    2、配置连接MySQL

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
        <session-factory>
    
            <property name="hbm2ddl.auto">update</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.url">jdbc:mysql:///test</property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="myeclipse.connection.profile">MysqlConnection</property>
            <property name="hbm2ddl.auto">update</property>
            
    
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
    
            <mapping resource="cn/wym/hiberate/domain/User.hbm.xml" />
            
            
        </session-factory>
    </hibernate-configuration>
     

    3、配置连接Microsoft SQL 2005

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
        <session-factory>
            <property name="hbm2ddl.auto">create</property>
            <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
            <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=testhjxc</property>
            <property name="connection.username">wym</property>
            <property name="connection.password">root</property>
            <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="myeclipse.connection.profile">MS2005</property>
            
            <property name="format_sql">true</property>
            <property name="show_sql">true</property>
            <mapping resource="cn/wym/hiberate/domain/User.hbm.xml" />          
        </session-factory>
    </hibernate-configuration>

    二、cn/wym/hiberate/domain/User.hbm.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--该文件要清楚地表述出 类 和 表 的对应关系-->
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     <hibernate-mapping package="cn.wym.hiberate.domain">
    
        <class name="User" table="user">
             <id name="id" column="id">        <!-- 映射主键 -->
                  <generator class="native"> </generator>
            </id>
             <property name="name"/>
            <property name="birthday"/>
            
          </class>
    </hibernate-mapping>

    三、cn/wym/hiberate/domain/User.java

    package cn.wym.hiberate.domain;
    
    import java.util.Date;
    
    public class User {
        private String name;
        private int id;
        private Date birthday;
    
        /**
         * 必须要有的
         */
        public User() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
    }
  • 相关阅读:
    WCF获取客户端IP,端口
    关闭应用程序(主程序)(WPF)
    WPF中自定义漂亮的进度条
    使用Silverlight4与Wcf4的Net.tcp实现的简单聊天室
    NET使用Js调用WebService
    [转]IBM服务器系列产品分类指南
    [转]delphi 变参函数:array of const
    [转]合并BPL包图文教程!
    [转]ABAP流程处理控制命令的说明
    [转]QR代码(Quick Response Code)简介
  • 原文地址:https://www.cnblogs.com/J-wym/p/3260861.html
Copyright © 2020-2023  润新知