zoukankan      html  css  js  c++  java
  • spring mvc 集成hibernate步骤

    今天从头把hibernate集成进入springMVC框架中,把过程记录下来。

      1.首先要在监听器配置文件中加入hibernate支持,如下:

    <?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"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
        <!-- 配置数据源 -->
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/tobepro"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
    
        <!-- 配置sessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <!-- 数据源指向上边的dataSource -->
            <property name="dataSource" ref="dataSource" />
            <!-- 配置包扫描,这样在启动的时候hibernate会自动搜索指定包下边有@Entity实体类文件 -->
            <property name="packagesToScan" value="com.test.maven.model"/>
            <!-- hibernate属性配置 -->
            <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="hibernate.format_sql">true</prop>
                </props>
            </property>
        </bean>
    
        <!-- 配置事物管理器 -->
        <bean id="transactionmManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <!-- 配置sessionFactory为上边的 -->
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <!-- 配置事务,使用代理方式提供(abstract可能是以接口形式配置,然后 在其他bean中添加接口,让具体的POJO去实现) -->
        <bean id="transactionProxy"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
            abstract="true">
            <property name="transactionManager" ref="transactionmManager" />
            <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="get*">PROPAGATION_NEVER</prop>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    </beans>

    配置文件中,首先配置了spring数据源,然后建立hibernate的session工厂(我使用的是hibernate注解方式的POJO类文件,所以采用packagesToScan的方法,若采用hbm配置文件的方法,无需添加此配置,但是需要增加hibernate.cfg.xml配置文件做配置),建立事物管理器,管理session工厂,然后配置代理。具体配置用法即原因在此就不做说明了,请自行百度。

    在最后的代理配置中,只是配置了代理的接口,而具体的代理需要通过实际的代理来实现,如下:

    <?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"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
        <!-- 配置dao包中class里成员赋值 -->
        <bean id="WxUserDao" class="com.test.maven.dao.impl.WxUserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        
        <!-- 配置service包中class里成员赋值 -->
        <bean id="WxUserServiceReal" class="com.test.maven.service.impl.WxUserServiceImpl">
            <property name="wxUserDao" ref="WxUserDao"/>
        </bean>
        
        <!-- 配置事务代理(把service代理过来) -->
        <bean id="WxUserService" parent="transactionProxy">
            <property name="target" ref="WxUserServiceReal"/>
        </bean>
    </beans>

    此文件首先初始化dao层和service层里必要的成员,然后把成员映射到代理。

    hibernate注解方式配置实体类文件的方法 请参考hibernate注解解析

  • 相关阅读:
    什么是惯性释放
    hyperworks2019x中模型简化
    optistruct如何将多个约束置于一个约束集合中
    optistruct对称约束设置
    optistruct非线性分析步子步设置
    optistruct怎么调用多核
    ConcurrentHashMap中节点数目并发统计的实现原理
    K:leetcode 5381.查询带键的排列 这题简单,但我还能优化。精益求精,才是算法的乐趣所在!
    K:缓存相关问题
    K:剑指offer-56 题解 谁说数字电路的知识不能用到算法中?从次数统计到逻辑表达式的推导,一文包你全懂
  • 原文地址:https://www.cnblogs.com/tobeprogramer/p/4162614.html
Copyright © 2011-2022 走看看