- 导包
- 创建数据库
- 书写对象与表的映射文件;一般命名为
xxx.hbm.xml
导入xml约束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
书写对象与表映射文件,其中class
标签的name属性是实体类的完整类名,table
属性为数据库表名,dynamic-update
属性是每次更新自动生成的sql语句是否为表的全部列,dynamic-insert
同理,每个实体类对象映射文件都要有一个ID
标签来唯一标识记录是否存在,id
不可省略,
id
标签与property
标签中的name
属性对应实体类中的属性名,严格区分大小写,column
属性对应表中的列名,不区分大小写,type
属性为属性类型;id
标签下的generator
标签标识id
的增长类型:其中native
是数据库主键自动增长,increment
是框架主键自动增长,assigned
是手动指定id值
<hibernate-mapping >
<class name="cn.entity.Xuesheng" table="xueshengbiao" dynamic-update="true" dynamic-insert="true">
<cache usage="read-write"/>
<id name="xuehao" column="xuehao" type="int">
<generator class="native"></generator>
</id>
<property name="xingming" column="xingming" type="java.lang.String"></property>
<property name="xingbie" column="xingbie" type="int"></property>
<property name="nianling" column="nianling" type="int"></property>
<set name="chengji" cascade="save-update">
<key column="xuehao"></key>
<one-to-many class="cn.entity.Chengji"/>
</set>
<set name="kecheng" inverse="true" table="chengjibiao" lazy="false" fetch="subselect">
<key column="xuehao"></key>
<many-to-many class="cn.entity.Kecheng" column="kechenghao">
</many-to-many>
</set>
</class>
</hibernate-mapping>
- 书写主配置文件
主配置文件放在src
下,名为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>
<!-- 数据库设置-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/xuesheng?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- 数据库类型 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 启用Hibernate的自动会话上下文管理-->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- 禁用二级缓存 -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->
<!-- 查看自动生成的sql语句 -->
<property name="show_sql">true</property>
<!-- 格式化自动生成的sql语句 -->
<property name="format_sql">true</property>
<!-- 二级缓存配置 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 查询缓存配置 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 自动建表 -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<!-- 注入实体类与与表的映射文件-->
<mapping resource="cn/entity/xuesheng.hbm.xml" />
<mapping resource="cn/entity/chengji.hbm.xml" />
<mapping resource="cn/entity/kecheng.hbm.xml" />
</session-factory>
</hibernate-configuration>