What is persistence technology
Persistence technology. It is to save data to be permanently stored in the memory device. Persistence is the main application objects in memory are stored in a relational database.
What is ORM
Object-relational mapping.
We can go through ORM class operation by way of a database, rather than native SQL statements. By mapping table into categories, as examples of the row, as the field attribute.
ORM in the implementation of the object corresponding to the operation of the operation will eventually be converted to the native database statement.
New Construction
step
- Import-related packages
- Create a class orm
- Creating Hibernate core profile
- The mapping between
- Create a class execution
Guide package
- Packages must rely hibernate-release-5.3.1.Finallibrequired
- Database package
- c3p0 connection pool c3p0.jar
- Unit testing junit-4.9.jar
Creating ORM class
import lombok.Getter;
import lombok.Setter;
@Getter@Setter
public class Students {
private int id;
private String name;
private int age;
@Override
public String toString() {
return "Students{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
'}';
}
}
Core configuration file
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 连接数据库的基本参数 -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test?serverTimezone=UTC</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Demo1/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The mapping between
Configured via xml configuration.
<?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">
<hibernate-mapping>
<class name="Demo1.Students" table="students" >
<!--建立类属性哪一个是主键 还要跟数据库当中主键进行对象-->
<id name="id" column="id" >
<generator class="native"/>
</id>
<!--建立类中的普通属性与数据库当中字段进行关联-->
<property name="name" column="name" />
大专栏 Hibernate-配置class="token punctuation"><property name="age" column="age"/>
</class>
</hibernate-mapping>
carried out
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@Test
public void save(){
/*1.加载配置文件*/
Configuration configure = new Configuration().configure();
/*2. 创建会话工厂 - JDBC连接 */
SessionFactory sessionFactory = configure.buildSessionFactory();
/*3. 获取session - JDBC连接对象 */
Session session = sessionFactory.openSession();
Students per = new Students();
per.setId(15);
per.setName("张三");
per.setAge(15);
session.save(per);
/*释放资源*/
session.close();
sessionFactory.close();
}
Resolve
Mapping configuration
xxx.hbm.xml
Xxx behind persistent class corresponding to a fixed wording .hbm.xml
hibernate-mapping标签
Note that a mapping file can only have a hibernate-mapping tags
class标签
Role: mapping relationship between classes and tables
Attributes:
- The full path name of the class
- table table name. The class name and the table name has been, table can be omitted, if the table does not automatically create a table
- catalog
- proxy proxy settings to support deferred loading
- lazy whether to use lazy loading
- dynamic-update contains only the fields changed when specifying Update SQL generation
- dynamic-insert contains a non-specified empty fields generated InsertSQL
- Examples of mutable if the class variable
id标签
Primary key corresponding relationship established class attribute table: role
Attributes:
- The name attribute of
- The name of the database primary key column
- length
- type hibernate type
property标签
Action: establishing the corresponding relationship common attribute class table
- name
- type
- column
- access
- Are not-null attribute value can be null
- Whether generated generated by the database
Core Configuration
hibernate.cfg.xml
When the program calls the Configuration object configure () method, Hibernate will automatically load the file. hibernate-configuration is the root element. Root element contains session-factory sub-element sub-elements have the property element, which is an important property attribute information database connection configuration Hibernate
Automatic construction of the tablehibernate.hbm2ddl.auto
Formatting SQLhibernate.format_sql
Show SQL hibernate.show_sql
DTD configuration
File -> Settings -> Sehemas and DTD
Configuring connection pool
<!--C3PO-->
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!--设置事务的隔离级别-->
<property name="hibernate.connection.isolation">4</property>
<!--创建一个session绑定到当前线程-->
<property name="current_session_context_class">thread</property>
Log Viewer
Guide package
log4j-1.2.16.jar
log4j.preperties
Placed under the src directory
level
- error
- warn
- info
- The default debug
- trace