学习Spring整合Hibernate的知识,新建一个工程,代码结构如下:
按如下步骤整合:
代码如下:
hibernate.cfg.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 配置hibernate的基本属性 --> 8 <!-- 1.数据源需配置到IOC容器中,所以在此处无需配置C3P0数据源 --> 9 <!-- 2.关联的.hbm.xml也在IOC容器中配置SessionFactory实例时进行配置 --> 10 <!-- 3.配置hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略及二级缓存等 --> 11 12 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 13 14 <property name="hibernate.show_sql">true</property> 15 16 <property name="hibernate.format_sql">true</property> 17 18 <property name="hibernate.hbm2ddl.auto">update</property> 19 20 <!-- 配置hibernate二级缓存相关的属性 --> 21 22 </session-factory> 23 </hibernate-configuration>
db.properties:
1 jdbc.user=root 2 jdbc.password=1234 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_hibernate 5 6 jdbc.initPoolSize=5 7 jdbc.maxPoolSize=10