Using Hibernate notes

Hibernate


1. What is Hibernate?
    A. Excellent persistence framework (the interaction between solution and database)
    B. ORM design concept reflects the P306 objects to mappings
    C. will be completely extracted from the service layer, business logic is simpler (the underlying database operations, all packaged together with)
    
the architecture of Hibernate
    A. persistent object (.java persistence *)
    B.Hibernate property files (database connection parameters)
    C.Hibernate mapping file (mapping table to do with Java classes)
    
Hibernate in important classes
    A.configuration configuration classes
    B.session factory session factory (the operator in a session) for generating a session
        factory design pattern
    C.session session class
    
of Hibernate Starter
    1. Download
    2. import JAR
    3. introduced connected driver package
    4. generated attribute file
   

​
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>//驱动路径
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.connection.url">jdbc:mysql:///test</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>//数据库方言(当前使用的是什么数据库)
  <property name="hibernate.show_sql">true</property>//打印对应的sql语句
  <mapping resource="cn/pzhuweb/bean/User.hbm.xml"/>

​


      The mapping file generation table
      

<hibernate-mapping>
    <class name="cn.pzhuweb.bean.User" table="user">//表对应的持久化类的位置 表名
        <id name="username" type="java.lang.String">//持久化类对应主键名 数据类型
            <column name="name" />//字段名
            <generator class="assigned" />//由程序负责字段的生成  生成方式 P311
        </id>
        <property name="password" type="java.lang.String">//非主属性
            <column name="password" />
        </property>
    </class>
</hibernate-mapping>

java.lang.String
String class in the java.lang package, java String class to create a string variable, string variables belong to the object.

<generator class = "assigned" / >
primary key generation strategy

spring property标签
name="name"     type="xsd:string"     use="required"

6. Hibernate is initialized (obtained session)
    A. configured to create a class of Hibernate
    B. Hibernate session factory create (generate session / close session)
    C. create a local thread, if the session already exists, directly from the thread
    
7. create a DAO interface
8. create a user interface persistence of DAO

Hibernate inside
session.getTransaction () commit ();. // Here is the real statement interacts with the database
is instantiated. . .
Hibernate is encapsulated database operations
by: save
deleted: delate refresh
check: get / load
change: get / load set refresh method

Published 42 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_39395805/article/details/103477599
Recommended