主配置文件
<?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核心配置 --> <hibernate-configuration> <session-factory> <!-- 數據庫連接配置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- 數據庫連接池配置 --> <property name="connection.pool_size">1</property> <!-- SQL方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- 是否顯示hibernate自動執行的sql語句到控制台 --> <property name="show_sql">true</property> <!-- 指定數據庫表的生成方式: update:如果表存在,直接使用。如果不存在自動創建 create:無論表是否存在,每次啟動都會重新創建表結構 --> <property name="hbm2ddl.auto">update</property> <!-- 引入映射文件 --> <mapping resource="hib/po/User.hbm.xml" /> </session-factory> </hibernate-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> <!-- 配置: 類和表映射 name:類全名 table:表名 schema:數據庫名(可以省略) --> <class name="hib.po.User" table="t_user" schema="hibernatedb"> <!-- 類的唯一標示和表中的主鍵映射 --> <id name="userId" column="user_id"> <!-- 主鍵生成策略:native(mysql自增) --> <generator class="uuid"></generator> </id> <!-- 普通屬性和普通字段映射 --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="sex" column="sex"></property> <property name="birthday" column="birthday" type="date"></property> <property name="salary" column="salary"></property> </class> <!-- 排序條件 --> <query name="findOrder"> <!-- 可以出現特殊字符:> < from User order by salary asc,sex asc < --> <![CDATA[ from User order by salary asc ]]> </query> </hibernate-mapping>