/* 1.Hibernate的体系结构 2.Hibernate API体系 *********************************************************************************************************************************************************************************************** Hib
/*1.Hibernate的体系结构
2.Hibernate API体系
***********************************************************************************************************************************************************************************************
Hibernate API总结
名称 描述
Configuration类 负责配置和启动Hibernate,创建SessionFactory实例
SessionFactory接口 负责初始化Hibernate,创建Session实例,充当数据源代理,一个SessionFactory实例对应一个数据源,由于SessionFactory需要自己缓存
消耗的资源比较大,因此,当应用中只有一个数据源时,最好只创建一个SessionFactory对象实例,除非有多个数据源,才分别为每个数据源创建一个SessionFactory对象实例
Session接口 负责保存,更新,删除,加载和查询持久化对象,充当持久化管理器
Transaction接口 对底层的事务进行了封装,充当了事务管理器
Query接口,Criteria接口 执行数据库查询,充当Hibernate的查询器
***************************************************************************************************************************************************************************************************
Hibernate Web应用的开发步骤:
(1)创建数据源
(2)将Hibernate所需的JAR包复制到WEB-INF/lib下
(3)创建Hibernate配置文件
(4)利用Hibernate的第三方工具或Eclipse插件从数据库中创建出相应的实体对象其ORM映射文件
(5)创建Hibernate的SessionFactory类型
(6)通过SessionFactory对象创建Session实例
(7)通过创建Session实例进行持久化对象的管理
(8)通过创建Transaction实例进行事务管理
(9)通过创建Query或者Criteria实例实现数据库的查询
3.配置Hibernate
配置Hibernate主要就是创建Hibernate配置文件和SessionFactory类,Hibernate的配置文件可以是hibernate.properties或者
hibernate.cfg.xml(两者取其一),Hibernate.cfg.xml配置首选
配置好hibernate.cfg.xml后,推荐保存在WEB-INF/classes下,接下来就可以创建SessionFactory了
package com.hephec.orm;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class MySessionFactory{
//定义一个静态字符串变量存放Hibernate的配置文件名
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
//创建一个线程局部变量对象
private static final ThreadLocal threadLocal=newThreadLocal();
//创建一个静态的Configuration对象
private static final Configuration cfg=new Configuration();
//定义一个静态的SessionFactory对象
private static org.hibernate.SessionFactory sessionFactory;
//取得一个当前的Session对象
private static Session currentSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if(session==null){
if(sessionFactory==null){
try{
//根据配置文件,配置Hibernate
cfg.config(CONFIG_FILE_LOCATION);
//通过Configuration对象创建SessionFactory对象
SessionFactory=cfg.buildSessionFactory();
}
catch(Exeption e){
System.out.println("系统错误创建SessionFactory对象出错!");
e.printStackTrace();
}
}
//通过SessionFactory对象创建Session对象
session=sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
//关闭一个Session对象
public static void closeSession()throws HibernateException(){
Session session=(Session)threadLoal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
//构造方法
public MySessionFactory(){
}
}
调用Hibernate API 进行持久化操作
package com.hephec.service;
import org.hibernate.*;
import java.util.*;
import com.hephec.orm;
public class SystemPart{
//用户验证
public boolean userCheck(String loginName,String loginPass) throws Exception{
//创建一个Session对象
Session session=MySessionFactory.currentSession();
//定义一个Transaction对象
Transaction tx=null;
try{
List result=null;
//创建一个Query查询对象
Query query=session.createQuery("select a from Admin as a where a.username=:loginName and a.loginPass=:loginPass");
//设置查询参数值
query.setString("loginName",loginName);
query.setString("loginPass",loginPass);
//创建一个Transaction对象
Transaction tx=session.beginTransaction();
//执行查询,得到查询结果
result=query.list();
tx.commit();
if(result.size()>0)return true;
else return false;
catch(Exception e){
//事务回滚
if(tx!=null){
tx.rollback();
}
System.out.println("系统错误!");
e.printStackTrace();
return false;
}finally{
//关闭Session对象
session.close();
}
}
}
4.Hibernate映射配置文件
5.Hibernate会话管理
*/
/*
Hibernate的映射机制
1.Hibernate基本映射数据类型
2.Hibernate的主键映射
3.Hibernate的实体映射
4.映射一对一关联关系
5.映射多对一的单向关联关系
6.映射一对多的双向关联关系
7.映射一对多双向自身关联关系
8.映射多对多单向关联关系
9.映射多对多双向关联关系
10.映射组成关系
11.映射继承关系
12.Hibernate映射集合
*/
/*
使用Session的beginTransction()方法
使用Session的close()方法
使用Session的connection()方法
使用Session的delete()方法
使用Session的get()方法
使用Session的load()方法
使用Session的update()方法
使用Session的saveOrUpdate()方法
使用Hibernate的isInitalized()与initialize()方法
持久化对象的级联操作
*/
/*
Hibernate的检索策略
1.立即检索
2.延迟检索
3.迫切左外连接检索
*/
/*
HQL查询方法
1.基本查询
2.条件查询
3.分页查询
4.连接查询
5.子查询
6.动态实例化查询结果
*/
/*
QBC查询方式
1.基本查询
2.QBE查询
3.分页查询
4.复合查询
5.离线查询
*/
/*
本地SQL查询
*/
/*
Hibernate批量操作
1.批量插入
2.批量更新
3.批量删除
*/
/*
Hibernate的事务管理
1.事务边界声明
2.并发控制
3.悲观锁
4.乐观锁
*/
/*
Hibernate缓存机制
1.Hibernate的缓存分类
2.Hibernate的缓存范围
3.Hibernate的缓存管理
4.Hibernate二级缓存的并发访问策略
5.Hibernate的二级缓存配置
*/
/*
Hibernate应用的性能优化
*/
/*
多数据源的应用
*/
/*
JDBC应用
*/
/*
Hibernate调用存储过程
*/
/*
XML数据持久化
*/

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


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

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.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
