zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }
  • 相关阅读:
    BZOJ 1221 [HNOI2001] 软件开发 费用流_建模
    BZOJ 1180 / 2843 LCT模板题 + 双倍经验
    bzoj 4372: 烁烁的游戏 动态点分治+树链剖分+线段树
    bzoj 3730: 震波 动态点分治+树链剖分+线段树
    luogu P2634 [国家集训队]聪聪可可 点分治
    bzoj 1468: Tree 点分治
    bzoj 1296: [SCOI2009]粉刷匠 动态规划
    bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化
    BZOJ 1042: [HAOI2008]硬币购物 容斥原理+背包
    matplotlib模块
  • 原文地址:https://www.cnblogs.com/J-wym/p/3260861.html
Copyright © 2011-2022 走看看