spring和hibernate整合方案
步骤:
1.加入spring能力,此步骤需要选择一下jar包
spring 3.0 core libraries (ioc)
spring 3.0 Persistence core libraries (提供了对orm的简化支持)
spring 3.0 aop libraries (Persistence包的实现依赖于aop包)
spring 3.0 Persistence jdbc libraries (提供了数据源)
2.加入hibernate能力
主意:
选择spring配置文件来配置hibernate相关设置
生成配置文件如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
<property name="username" value="moluo"/>
<property name="password" value="moluo"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
</bean>
在目前的配置文件中体现了原来hibernate.cfg.xml中的具体内容
1.配置数据库连接
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:moluo"/>
<property name="username" value="moluo"/>
<property name="password" value="moluo"/>
</bean>
2.配置hibernate特色配置
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
常规做法:加入hibernate.show_sql=true
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
3.把需要映射的bean进行映射
1.创建一个bean包
2.选择数据库试图,加入需要映射的表的hibernate映射
结果:spring配置文件中的sessionFactory 的 bean中新增加mappingResources属性
<property name="mappingResources">
<list>
<value>bean/Test.hbm.xml</value>
</list>
</property>