hibernate基础配置文件
主配置文件
<?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>