Hibernate配置小结

  1. 下载hibernate开发包

  2. 解压并在需要配置的项目下导入hibernate-release-5.2.9.Final\lib\required下的所有jar包

  3. 配置hibernate.cfg.xml 文件(hibernate对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>

                  <!-- 数据库配置 -->

                  <!-- 数据库驱动 -->

                  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

                  <!-- 数据库连接地址 -->

                  <property name="connection.url">jdbc:mysql://localhost:3306/ncsxy.xmr?characterEncoding=utf-8</property>

                  <!-- 数据库用户名 -->

                  <property name="connection.username">Tianc</property>

                  <!-- 数据库密码 -->

                  <property name="connection.password">tianc</property>

                  <!-- 数据库方言 不同的数据库配置不同,此处为mysql。oracle为org.hibernate.dialect.Oracle10gDialect -->

                  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

                  <!-- mysql版本 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

                   <property name="connection.url">jdbc:mysql://localhost:3306/XXXX?characterEncoding=utf-8</property>

                   <property name="connection.username">root</property>

                       <property name="connection.password">SSSSS</property>                     

                       <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->

                      <!-- 运行过程中,将sql语句打印在控制台,建议在开发阶段将值设置为true -->

                      <property name="show_sql">true</property>

                      <!-- 根据hbm配置文件,对数据库的表做操作 create:重新建表 create-drop:删除表,再建表 update:更新表 none:不对表操作 -->

                     <property name="hbm2ddl.auto">none</property>

                    <!-- 加入数据库和对象的映射文件 这个需要自己去配置,配置方法在后面 -->

              <mapping resource="ncsxy/xmr/data/UserData.hbm.xml" />

              <mapping resource="ncsxy/xmr/data/AdminData.hbm.xml" />

              <mapping resource="ncsxy/xmr/data/ConsumptionData.xml" />

              <mapping resource="ncsxy/xmr/data/MessageData.xml" />

              <mapping resource="ncsxy/xmr/data/UserShareData.xml" />

       </session-factory>

</hibernate-configuration>

 

 

<!-- 配置实体类和数据库关系即bean与数据表的列对应关系 -->

<?xml version="1.0"?>

<!-- ~ 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-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 由此开始配置 以上为默认引用-->

<!--  package="ncsxy.xmr.data"这个package后面双引号内写入的是你bean的包名称表示你的bean都在该包下 -->

<hibernate-mapping package="ncsxy.xmr.data">

<!-- name为bean名称 table为对应的表名称 -->

    <class name="MessageData" table="messagedata">

<!-- id为配置主键,name为bean下为主键的字段名 column为数据库表中的主键列名 -->
        <id name="messageNum" column="message_num">
            <generator class="identity" />
        </id>

<!-- 此处配置其他属性关系 name为bean字段名 column为数据表列名 配置完成后加到 hibernate.cfg.xml的mapping去-->
        <property name="messageTitle" column="message_title"></property>
        <property name="messageBody" column="message_body"></property>
        <property name="userAccount" column="user_account"></property>
        <property name="messageDate" column="message_date"></property>
        <property name="messageTime" column="message_time"></property>
        <property name="isReply" column="isreply"></property>
        <property name="messageReply" column="message_reply"></property>
        <property name="messageReplyDate" column="message_replydate"></property>
        <property name="messageReplyTime" column="message_replytime"></property>
    </class>

</hibernate-mapping>
配置完成即可使用hibernate的功能啦(ps:我也是新手,各位大佬手下留情)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值