• Hibernate二 配置


    1、hibernate.cfg.xml文件,配置数据库连接信息和实体的映射文件信息

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">bjsxt</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


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


            <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>  //这里是实体的映射文件
        </session-factory>
    </hibernate-configuration>

    2、具体对象的映射信息

    <hibernate-mapping>
        <class name="com.bjsxt.hibernate.User">
            <id name="id">
                <generator class="uuid"/>
            </id>
            <property name="name"/>
            <property name="password"/>
            <property name="createTime"/>
            <property name="expireTime"/>
        </class>
    </hibernate-mapping>

     

    开发流程

    1、新建java项目

    2、创建User Library,加入如下jar
        * HIBERNATE_HOME/hibernate3.jar
        * HIBERNATE_HOME/lib/*.jar
        * MySql jdbc驱动
    3、创建hibernate配置文件hibernate.cfg.xml,为了便于调试最好加入log4j配置文件

    4、定义实体类

    5、定义User类的映射文件User.hbm.xml

    6、将User.hbml.xml文件加入到hibernate.cfg.xml文件中

    7、编写hbm2ddl工具类,将实体类生成数据库表

    8、开发客户端
    为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入<property name="hibernate.show_sql">true</property>

  • 相关阅读:
    xshell不能输入中文,显示为??
    ansible-2.1.0.0_module
    ansible
    解决ssh连接问题2
    Java Socket网络编程Server端详解
    Java Socket网络编程Client端详解
    Spring4.* 中整合 Hibernate
    自定义规则,对List<Map<String,Object>> List<Object>进行排序
    自定义注解和注解的相关使用
    枚举类型的相关解析
  • 原文地址:https://www.cnblogs.com/guaniu/p/2325825.html
Copyright © 2020-2023  润新知