hibernate4通过注解方式整合sessionFactory策略

来源:互联网 发布:蜗牛移动 网络 编辑:程序博客网 时间:2024/06/05 23:58

1.首先是在配置文件里面定义sessionFactory,具体配置如下:

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd          http://www.springframework.org/schema/mvc          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">            <!-- 加载common-config.properties文件 -->     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:config/common-config.properties" />     </bean>          <!-- 配置数据源 -->      <bean id="dataSource"          class="org.springframework.jdbc.datasource.DriverManagerDataSource">          <property name="driverClassName" value="com.mysql.jdbc.Driver" />          <property name="url" value="jdbc:mysql://127.0.0.1:3306/appeval" />          <property name="username" value="root" />          <property name="password" value="root" />      </bean>          <!-- 配置jdbc -->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">    <property name="dataSource" ref="dataSource"/>    </bean>        <!--  配置hibernate SessionFactory-->      <bean id="sessionFactory"          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">          <property name="dataSource" ref="dataSource" />          <property name="hibernateProperties">              <props>                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                  <prop key="hibernate.hbm2ddl.auto">update</prop>                  <prop key="hibernate.show_sql">true</prop>                  <prop key="hiberante.format_sql">true</prop>              </props>          </property>          <property name="configLocations">              <list>                  <value>                      classpath*:config/hibernate/hibernate.cfg.xml                  </value>              </list>          </property>      </bean>        <!-- 事务管理器 -->      <bean id="transactionManager"          class="org.springframework.orm.hibernate4.HibernateTransactionManager">          <property name="sessionFactory" ref="sessionFactory"></property>      </bean>            <!-- 事务代理类 -->      <bean id="transactionBese"          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"          lazy-init="true" abstract="true">          <property name="transactionManager" ref="transactionManager"></property>          <property name="transactionAttributes">              <props>                  <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>                  <prop key="get*">PROPAGATION_NEVER</prop>              </props>          </property>      </bean>    </beans>    
2.然后是重点,通过注解将sessionFactory注入到属性中,代码如下:

@Repository("entityDao")public class HbmEntityDaoImpl implements EntityDao {private final Logger logger = Logger.getLogger(HbmEntityDaoImpl.class);    @Autowiredprivate SessionFactory sessionFactory;
其他的就可以通过注释进行具体操作了。


0 0