1 添加hibernate相关依赖5.2.12.final
<!-- hibernate核心依赖-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> </dependency>2 在resource目录下添加hibernate.cfg.xml(核心配置文件)
2.1 添加DTD支持 dtd文件在你导入的jar包中 2.2 添加Hibernate的配置 2.2.1 数据库相关(property)(connection.username|connection.password|connection.url|connection.driver_class|dialect) 2.2.2 调试相关(show_sql|format_sql) 注1:hibernate配置到spring中后,必须加前缀hibernate.show_sql`在这里插入代码片 ` <?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数据库连接配置 --> <property name="connection.username">账号</property> <property name="connection.password">密码</property> <property name="connection.url">jdbc:mysql://localhost:3306/t248?useUnicode=true&characterEncoding=UTF-8&useSSL=false</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property> <!-- 调试相关 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 开启二级缓存 --> //<property name="hibernate.cache.use_second_level_cache">true</property> //<!-- 开启查询缓存 --> //<property name="hibernate.cache.use_query_cache">true</property> <!-- EhCache驱动 --> //<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 所有实体类映射文件需要在核心配置中进行映射配置 --> </session-factory> </hibernate-configuration>2.3 再创建实体类和实体映射文件(*.hbm.xml) 将实体映射文件加入到核心配置文件中
<mapping resource="映射文件所在的位置"/> <mapping resource="mapping/TreeNode.hbm.xml"/> <mapping resource="mapping/Book.hbm.xml"/> <mapping resource="mapping/category.hbm.xml"/> <mapping resource="mapping/Order.hbm.xml"/> <mapping resource="mapping/OrderItem.hbm.xml"/> <mapping resource="mapping/BookCategory.hbm.xml"/> 实体必须实现Serializable接口