hibernate.cfg.xml 配置文件模版:
<?xml version='1.0' encoding='UTF-8'?>
<!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>
<!-- 啓動Hibernate時,重建數據庫 create;更新數據庫 update -->
<property name="hbm2ddl.auto">update</property>
<!--顯示執行的SQL語句-->
<property name="show_sql">true</property>
<!-- 數據庫方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- driver url username password -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF8&useSSL=false</property>
<property name="connection.username">root</property>
<property name="connection.password">Cs123456</property>
<!-- JDBC連接池(使用內置的連接池)-->
<property name="connection.pool_size">5</property>
<!-- 每次從數據庫中取出,並放到JDBC的Statment中的記錄條數,fetch_size設的越大,讀取數據庫的次數會越小,速度越快,但消耗更多內存 -->
<property name="jdbc.fetch_size">50</property>
<!-- 批量插入、刪除和更新時,每次操作的記錄數,batch_size越大,向數據庫發送SQL語句的次數越少,速度越快,但消耗更多內存 -->
<property name="jdbc.batch_size">30</property>
<!-- Hibernate自動管理上下文的策略 -->
<property name="current_session_context_class">thread</property>
<!-- 配置mapping -->
<!-- (1)hbm.xml 映射文件配置 -->
<mapping resource="com/uzipi/entity/Student.hbm.xml" />
<mapping resource="com/uzipi/entity/Course.hbm.xml" />
<!-- (2)持久化類上的註解方式 -->
<mapping class="com.uzipi.entity.TClass" />
</session-factory>
<mapping resource="com/uzipi/entity/Student.hbm.xml" />
</hibernate-configuration>
XXX.hbm.xml 映射文件模版:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="com.uzipi.entity.Student" table="t_student">
<id name="id" column="stu_id" type="int" unique="true"> <!-- 持久化類的唯一標識 -->
<generator class="native" />
<!-- <generator class="increment" /> -->
</id>
<property name="name" column="stu_name" type="string" not-null="true" length="200" />
<property name="sex" type="string" length="10"> <!-- 另一種映射字段的方式 -->
<column name="stu_sex"/>
</property>
<property name="age" column="stu_age" type="int" not-null="true" length="11" />
<property name="classbj" column="class_id" type="string" length="200" />
</class>
</hibernate-mapping>