Home > Java > javaTutorial > Detailed explanation of the steps to build the hibernate framework environment

Detailed explanation of the steps to build the hibernate framework environment

零下一度
Release: 2017-06-25 10:34:00
Original
1836 people have browsed it

1. Overview: The hibernate framework acts on the dao layer to achieve persistent storage of data. It operates the database in an object-oriented manner.

2. Construction of the hibernate framework

1. Import all jar packages in the required folder in the lib directory.

Mysql driver package.

2. Create a database in the table.

 3. Create entity class.

 4. Create entity mapping file (take crm practice Customer class as an example)

  Entity class name.hbm.xml

  Introduction of constraints File

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- 根元素
        package(可选):填写包名.后面凡是需要完整类名的地方,都可以省略包名了. --><hibernate-mapping package="cn.itcast.domain" ><!-- class:映射类与表的关系
            name属性:实体属性名
            table属性:对应的表名     --><class name="Customer" table="cst_customer" ><!-- id:映射主键属性名(OID)与主键列对应关系
            name属性: OID名称
            column属性(可选):主键列名,默认值就是name属性值
            length属性(可选):指定属性长度.默认值使用数据库对应列长度
            type属性(可选):指定当前列(属性)的类型.默认值会根据数据库类型自动指定类型.
                type="long"                hibernate类型
                type="java.lang.Long"    java类型
                <column name="cust_id" sql-type="bigint" ></column>  数据库类型     --><id name="cust_id"  ><!--主键生成策略 
        increment:hibernate每次保存数据是,会查询数据库中最大的值,在最大值的基础上加1作为新的主键值(测试时使用)
            identity:主键自增,有数据库负责生成主键值
            sequence:序列,Oracle时使用
            hilo:高低位算法,适用于既不支持自增也不支持序列的库(用不着)
            native:identity|sequence|hilo自动三选一
            uuid:主键类型为字符串是使用.
            assigned:有我们手动指定ID值
        --><generator class="native"></generator></id><!-- property:映射非主键属性名与非主键列对应关系
            name属性: 属性名
            column属性(可选):非主键列名,默认值就是name属性值
            length属性(可选):指定属性长度.默认值使用数据库对应列长度
            type属性(可选):指定当前列(属性)的类型.默认值会根据数据库类型自动指定类型.
                type="long"                hibernate类型
                type="java.lang.Long"    java类型
                <column name="cust_id" sql-type="bigint" ></column>  数据库类型     --><property name="cust_name" column="cust_name"  ></property><property name="cust_source" ></property><property name="cust_industry" column="cust_industry" ></property><property name="cust_level" column="cust_level" ></property><property name="cust_phone" column="cust_phone" ></property><property name="cust_mobile" column="cust_mobile" ></property></class></hibernate-mapping>
Copy after login

Create the main configuration file

hibernate.cfg.xml (under src)

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5     <!-- 根元素 --> 6     <hibernate-configuration> 7         <!-- 以下都是为sessionFactory对象配置的 --> 8         <session-factory> 9         <!-- 必选配置10         11             //方言12             //所有数据库的sql语句都是基于SQL99标准的13             //每个数据库遵循SQL99标准的同时,也会扩充一部分SQL语句.这些标准之外的sql语句叫做方言   mysql方言: limit 0,514             //注意:mysql方言类一共有3个.一定要选最短的15             #hibernate.dialect org.hibernate.dialect.MySQLDialect16             //数据库驱动17             #hibernate.connection.driver_class com.mysql.jdbc.Driver18             //数据库连接url19             #hibernate.connection.url jdbc:mysql:///test20             //连接用户名21             #hibernate.connection.username gavin22             //连接密码23             #hibernate.connection.password24          -->25             <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>26             <property name="hibernate.connection.url">jdbc:mysql:///hibernate_54</property>27             <property name="hibernate.connection.username">root</property>28             <property name="hibernate.connection.password">1234</property>29             <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>30         <!-- 可选配置 
31             //是否在控制台显示hibernate生成的sql32             hibernate.show_sql true33             //是否对显示到控制台的sql语句格式化34             hibernate.format_sql true35             //自动建表36             # create(测试时使用)        :  自动建表,每次启动hibernate的时候都会自动建表.37             # create-drop(测试时使用)     :  自动建表,每次启动hibernate的时候都会自动建表.释放资源时会将所有表删除.38             # update(常用)    :  自动建表,有表就不会再创建,如果已经存在的表不完全匹配.会自动修改表结构.39             # validate        :  校验表结构.不会自动建表.每次hibernate启动时都会检查表结构是否正确.40                                         //不正确=>抛出异常.41         -->42             <property name="hibernate.show_sql">true</property>43             <property name="hibernate.format_sql">true</property>44             45             <property name="hibernate.hbm2ddl.auto">update</property>46             47             <!-- 指定数据库隔离级别 
48                 ## specify a JDBC isolation level49                 #hibernate.connection.isolation 450                 mysql 默认级别是451                 Oracle 默认级别是252             -->53             <property name="hibernate.connection.isolation">4</property>54             <!-- 配置session与当前线程绑定 -->55             <property name="hibernate.current_session_context_class">thread</property>56             57             58         <!-- 映射引入配置 
59                 resource属性:填写引入映射文件的路径. 相对于src目录下.60         -->61             <mapping resource="cn/itcast/domain/Customer.hbm.xml" />62         </session-factory>63     </hibernate-configuration>
Copy after login

  

The above is the detailed content of Detailed explanation of the steps to build the hibernate framework environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template