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

Jun 25, 2017 am 10:34 AM
hibernate build frame 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<?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

2

3

4

 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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to evaluate the cost-effectiveness of commercial support for Java frameworks How to evaluate the cost-effectiveness of commercial support for Java frameworks Jun 05, 2024 pm 05:25 PM

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

Golang framework documentation best practices Golang framework documentation best practices Jun 04, 2024 pm 05:00 PM

Writing clear and comprehensive documentation is crucial for the Golang framework. Best practices include following an established documentation style, such as Google's Go Coding Style Guide. Use a clear organizational structure, including headings, subheadings, and lists, and provide navigation. Provides comprehensive and accurate information, including getting started guides, API references, and concepts. Use code examples to illustrate concepts and usage. Keep documentation updated, track changes and document new features. Provide support and community resources such as GitHub issues and forums. Create practical examples, such as API documentation.

How to choose the best golang framework for different application scenarios How to choose the best golang framework for different application scenarios Jun 05, 2024 pm 04:05 PM

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

What are the common misunderstandings in the learning process of Golang framework? What are the common misunderstandings in the learning process of Golang framework? Jun 05, 2024 pm 09:59 PM

There are five misunderstandings in Go framework learning: over-reliance on the framework and limited flexibility. If you don’t follow the framework conventions, the code will be difficult to maintain. Using outdated libraries can cause security and compatibility issues. Excessive use of packages obfuscates code structure. Ignoring error handling leads to unexpected behavior and crashes.

Detailed practical explanation of golang framework development: Questions and Answers Detailed practical explanation of golang framework development: Questions and Answers Jun 06, 2024 am 10:57 AM

In Go framework development, common challenges and their solutions are: Error handling: Use the errors package for management, and use middleware to centrally handle errors. Authentication and authorization: Integrate third-party libraries and create custom middleware to check credentials. Concurrency processing: Use goroutines, mutexes, and channels to control resource access. Unit testing: Use gotest packages, mocks, and stubs for isolation, and code coverage tools to ensure sufficiency. Deployment and monitoring: Use Docker containers to package deployments, set up data backups, and track performance and errors with logging and monitoring tools.

See all articles