在说Hibernate映射前,我们先来了解下对象关系映射 ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对这些对象的操作。我们来看一张图 vcHLudjPtcr9vt26zbbUz/PK/b7d1q685LXE07PJ5KOs
在说Hibernate映射前,我们先来了解下对象关系映射 ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对这些对象的操作。我们来看一张图
vcHLudjPtcr9vt26zbbUz/PK/b7d1q685LXE07PJ5KOsv8nS1M2ouf3Ts8nkudjPtdfUtq+y+sn6U1FM0+++5KOs1NrStc7xwt+8rbLjus3K/b7dsuPWrrzks+S1scfFwbqhozwvcD4KPGgyPkhpYmVybmF0ZdOzyeQ8L2gyPgo8cD4gPGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20141110/20141110091545138.jpg" alt="\">
Hibernate文件
- 映射类(*.java):它是描述数据库表的结构,表中的字段在类中被描述成属性,将来就可以实现把表中的记录映射成为该类的对象了。
- 映射文件(*.hbm.xml):它是指定数据库表和映射类之间的关系,包括映射类和数据库表的对应关系、表字段和类属性类型的对应关系以及表字段和类属性名称的对应关系等。
- 数据库配置文件(*.properties/*.cfg.xml):它是指定与数据库连接时需要的连接信息,比如连接哪种数据库、登录数据库的用户名、登录密码以及连接字符串等。当然还可以把映射类的地址映射信息放在这里。
基本映射:
具体看操作
1映射实体类
//默认空构造函数的重要性 public class User { public User() { // TODO Auto-generated constructor stub } public User(String id,String name){ this.id=id; this.name=name; } private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExprieTime() { return exprieTime; } public void setExprieTime(Date exprieTime) { this.exprieTime = exprieTime; } private String name; private String password; private Date createTime; private Date exprieTime; }
实体类的设计原则:
* 实现无参的默认的构造函数
* 提供一个标识
*建议不要使用final修饰实体类(因为采用load延时加载数据的时候会继承实体类生成代理对象)
*建议为实体类生成getter和setter方法(如果不使用,需要用属性field标识)
2映射文件User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!--1、class和table的映射,name属性是实体名,table属性是表名(table可省略,则name即是映射的表名)--> <class name="com.bjpowernode.hibernate.User"> <!-2、主键映射,name属性是实体类的标识符属性,对应table的主键,即用column表示(column同样可省略)--> <id name="id" access="field"> <!--主键生成器,class属性表示生成策略,根据不同的需求选择--> <generator class="uuid"/> </id> <!--3、其他属性的映射 property--> <property name="name" length="40" unique="true" /> <property name="password"/> <property name="createTime"/> <property name="exprieTime"/> <filter name="testFiltere" condition="id < :myid"></filter> </class> </hibernate-mapping>
3hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernast_test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">hanhan</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!--打印sql--> <property name="hibernate.show_sql">true</property> <!--在没有表的时候,创建sessionfactroy 时,就会去创建表(update的方式,不删除原有数据)--> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
Hibernate的主键生成策略种类
手动:Assigned
Hibernate主动:uuid
数据库交互:
需要和数据库交互以生成id的:guid、identity、sequence、native、foreign
说明:需要和数据库交互生成,需要经过一次查询才能生成
Guid,identity:MySQL,SQLserver的生成方式
sequence:Oracle,db2的生成方式,自增序列
native:identity+sequence,跨平台
foreign:只适用基于共享主键的一对一关联映射的时候使用。即一个对象的主键是参照的另一张表的主键生成的。
总结:
Hibernate的基本映射:重点是对主键生成策略的认识,根据不同数据库选择不同的方式,重要理解。

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;

はい、それはssafetostoreblobdatainmysql、butonsiderheSeCactors:1)Storagespace:blobscanconsumesificantspace.2)パフォーマンス:パフォーマンス:大規模なドゥエットブロブスメイズ階下3)backupandrecized recized recized recize

PHP Webインターフェイスを介してMySQLユーザーを追加すると、MySQLI拡張機能を使用できます。手順は次のとおりです。1。MySQLデータベースに接続し、MySQLI拡張機能を使用します。 2。ユーザーを作成し、CreateUserステートメントを使用し、パスワード()関数を使用してパスワードを暗号化します。 3. SQLインジェクションを防ぎ、MySQLI_REAL_ESCAPE_STRING()関数を使用してユーザー入力を処理します。 4.新しいユーザーに権限を割り当て、助成金ステートメントを使用します。

mysql'sblobissuitable forstoringbinarydatawithinarationaldatabase、whileenosqloptionslikemongodb、redis、andcassandraofferferulesions forunstructureddata.blobissimplerbutcanslowdowdowd withwithdata

toaddauserinmysql、使用:createuser'username '@' host'identifidedby'password '; here'showtodoitsely:1)chosehostcarefilytoconを選択しますTrolaccess.2)setResourcelimitslikemax_queries_per_hour.3)usestrong、uniquasswords.4)endforcessl/tlsconnectionswith

toavoidcommonMonmistakeswithStringDatatypesinmysql、undultingStringTypenuste、choosetherightType、andManageEncodingandCollationsEttingtingive.1)Usecharforfixed-LengthStrings、Varcharforaible Length、AndText/Blobforlardata.2)setCurrectCherts


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

WebStorm Mac版
便利なJavaScript開発ツール
