1. ORM :ORM ( Object / Relation Mapping ): 对象 / 关系映射(理解) 1) ORM 主要解决对象 - 关系的映射 2) .ORM的思想:将关系数据库中表中的记录映射成为对象,以对象的形式展现,程序员可以把对数据库的操作转化为对对象的操作。 2. Hibernate 的 Hel
1. ORM :ORM (Object /Relation Mapping ): 对象/关系映射(理解) 1) ORM 主要解决对象 -关系的映射 2) .ORM的思想:将关系数据库中表中的记录映射成为对象,以对象的形式展现,程序员可以把对数据库的操作转化为对对象的操作。2. Hibernate 的 HelloWord 1) . 加入 jar 包:加入到当前项目的 classpath 下 hibernate-release-4.2.4.F inal\lib\required\*.jar(所有的包) MySQL 的驱动mysql -connector -java -5.1.29 -bin.jar
2) . 配置 hibernate 的配置文件: hibernate.cfg.xml
①. 利用 hibernate 插件生成 hibernate.cfg.xml
?xml version ="1.0" encoding="UTF-8"?> !DOCTYPE hibernate -configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "> hibernate-configuration> session -factory > session -factory > hibernate-configuration>
②. 编辑 hibernate.cfg.xml 文件
I. 加入链接数据库的基本信息:
! -- 配置连接数据库的基本信息 --> property name ="connection.driver_class" >com.mysql.jdbc.Driver property > property name ="connection.username" >root property > property name ="connection.password" >123456 property > property name ="connection.url" >jdbc :mysql:///hibernate4property>
II. 配置 Hibernate 使用的数据库方言: 每一种数据库使用的基本语法会有细微的区别,例如分页 MySQL 使用 limit,而 Oracle 使用 rownum。这就需要告诉 Hibernate 底层使用的是哪一种数据库
! -- 配置 Hibernate 的数据库方言 --> property name ="dialect" >org.hibernate.dialect.MySQLInnoDBDialect property > 注意:方言对应的类来自于 hibernate-release- 4.2.4.Final\project\etc\hibernate.properties 中 III. 编辑 Hibernate 的一般属性
! -- 创建数据表的策略(了解,最多使用的值是 update) --> property name ="hbm2ddl.auto" >update property >
> create : 每次运行都会删除上一次的表 ,重新生成表 , 哪怕二次没有任何改变 > create -drop :会根据 .hbm.xml 文件生成表, 但是SessionFactory 一关闭, 表就自动删除 > update :最常用的属性值,也会根据 .hbm.xml 文件生成表 , 但若 .hbm.xml 文件和数据库中对应的数据表的表结构不同 , Hiberante 将更新数据表结构,但不会删除已有的行和列 > validate : 会和数据库中的表进行比较 , 若 .hbm.xml 文件中的列在数据表中不存在,则抛出异常
! -- 是否打印 SQL --> property name ="show_sql" >trueproperty>
! -- 是否格式化 SQL --> property name ="format_sql" >trueproperty>
3) . 编写实体类( POJO)及 Hibernate 映射文件: xxx.hbm.xml
I. 编写一个 POJO :必须包含一个 OID 字段和数据表主键对应;必须有一个无参数的构造器;为字段定义 getter、 setter;非 final 类
II. 由 hibernate 插件生成 xxx.hbm.xml 文件
注意:需要对文件进行简单的修改:修改主键生成方式(使用 id 的 generator 子节点的 class 属性配置主键的生成方式, native 表示使用数据库本地的方式来 生成主键, MySQL 会自动的选用 auto_increment,而 Oracle 则使用序列的方式)
generator class ="assigned" /> 修改为 generator class="native" />
III. 在 hibernate 配置文件(hiberante.cfg.xml )中关联 hibernate 持久化类的映射文件 mapping resource ="com/atguigu/hibernate/entities/News.hbm.xml" />
4) . 通过 Hibernate API 完成持久化操作
1. 创建 SessionFactory : Session 的工厂类。SessionFactory 是线程安全的,一般地,在一个 Java 应用中只有一个 SessionFactory 实例 Configuration configuration = new Configuration ().configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()) .buildServiceRegistry (); SessionFactory sessionFactory = configuration.buildSessionFactory (serviceRegistry );
2. 创建 Session : 表示 Hibernate 应用程序和数据库的一次会话 Session session = sessionFactory.openSession ();
3. 开启事务 Transaction transaction = session.beginTransaction ();
4. 执行持久化操作 //save //session.save (news );
//利用 OID 加载对象 News news2 = (News ) session.get (News.class , 1); System.out.println (news2 ); news2.setContent ("myBatis" );
5. 提交事务 transaction.commit ();
6. 关闭 Session session.close ();
7. 关闭 SessionFactory sessionFactory.close ();
★写测试类的时候一般采用注解更方便些: public class Testing { private SessionFactory sessionFactory ; private Session session ; private Transaction transaction = null ; @Before public void init (){ Configuration configuration = new Configuration ().configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory (serviceRegistry ); session = sessionFactory.openSession (); transaction = session.beginTransaction (); } @After public void destroy (){ transaction.commit (); session.close (); sessionFactory.close (); } @Test public void test (){ //测试部分 } }

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools