之前几篇文章主要是介绍 Hibernate、Hibernate Annotation、Hibernate Spring 集成
这篇文章主要是:Hibernate和Spring集成后都使用注解的方式。
http://git.oschina.net/yaolifei/test/tree/master/test-hibernate-spring-annotation
这篇文章主要是:Hibernate和Spring集成后都使用注解的方式。
Hibernate使用注解进行关系映射,Spring使用注解进行Bean的依赖注入。
Spring的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--
<context:annotation-config/>
-->
<context:component-scan base-package="com.yaolifei.test"/>
<context:property-placeholder location="jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value= "${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.yaolifei.test.user.domain"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
完整代码请见:
http://git.oschina.net/yaolifei/test/tree/master/test-hibernate-spring-annotation
来源:oschina
链接:https://my.oschina.net/u/574527/blog/145673