search
HomeJavajavaTutorialDetailed explanation of the steps to build the hibernate framework environment

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"?>nbsp;hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- 根元素
        package(可选):填写包名.后面凡是需要完整类名的地方,都可以省略包名了. --><hibernate-mapping><!-- class:映射类与表的关系
            name属性:实体属性名
            table属性:对应的表名     --><class><!-- 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><!--主键生成策略 
        increment:hibernate每次保存数据是,会查询数据库中最大的值,在最大值的基础上加1作为新的主键值(测试时使用)
            identity:主键自增,有数据库负责生成主键值
            sequence:序列,Oracle时使用
            hilo:高低位算法,适用于既不支持自增也不支持序列的库(用不着)
            native:identity|sequence|hilo自动三选一
            uuid:主键类型为字符串是使用.
            assigned:有我们手动指定ID值
        --><generator></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></property><property></property><property></property><property></property><property></property><property></property></class></hibernate-mapping>

Create the main configuration file

hibernate.cfg.xml (under src)

 1 <?xml  version="1.0" encoding="UTF-8"?> 2 nbsp;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>com.mysql.jdbc.Driver</property>26             <property>jdbc:mysql:///hibernate_54</property>27             <property>root</property>28             <property>1234</property>29             <property>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>true</property>43             <property>true</property>44             45             <property>update</property>46             47             <!-- 指定数据库隔离级别 
48                 ## specify a JDBC isolation level49                 #hibernate.connection.isolation 450                 mysql 默认级别是451                 Oracle 默认级别是252             -->53             <property>4</property>54             <!-- 配置session与当前线程绑定 -->55             <property>thread</property>56             57             58         <!-- 映射引入配置 
59                 resource属性:填写引入映射文件的路径. 相对于src目录下.60         -->61             <mapping></mapping>62         </session-factory>63     </hibernate-configuration>

  

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!

Statement
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
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software