spring、springmvc、hibernate的整合

spring与hibernate的整合,因为现在都是使用maven来管理那些依赖jar包的,所以我们就不会去关注哪些包是必须的,哪些必须的包是依赖于其他的包,这些maven都能帮我们搞定,剩下的就是spring和hibernate的整合,以前在hibernate没有注解的时候,配置是很麻烦的,但是现在有了注解,就不用一一去配置数据库xml了,直接在实体里面注解,好了,废话不多说了,直接上spring_hibernate 的配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
           <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
           <context:annotation-config></context:annotation-config>
<context:component-scan base-package="com">
        <context:exclude-filter type="annotation" 
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>
<!--                jdbc:mysql://localhost:3306/a?useUnicode=true&amp;characterEncoding=UTF-8 -->
                jdbc:mysql://localhost:3306/a?useUnicode=true&amp;characterEncoding=UTF-8 

            </value>
        </property>
        <property name="username">
            <value>admin</value>
        </property>
        <property name="password">
            <value>admin</value>
        </property>

 </bean>

 <最主要的就是这段,因为class使用错了,很容易报错的,然后下面的packagesToScan也是重中之重,因为他是去扫描hibernate下面注解的实体类>
<bean id="sessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
            <property name="dataSource" ref="dataSource" /> 
             <property name="hibernateProperties">  
            <props>  
                <!-- 设置Hibernate的数据库方言 -->  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->  
                <prop key="show_sql">true</prop>  
                <!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->  
                <prop key="hibernate.jdbc.batch_size">50</prop>  
                <prop key="show_sql">50</prop>  
                    <prop key="hibernate.hbm2ddl.auto">update</prop>

            </props>  
        </property>  

            <property name="packagesToScan">  
            <list>     
                <value>包名</value>  
            </list>  
        </property>  

</bean>
<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"  />
</beans>

其实这个配置文件最主要的是将头部引进去,还有就是哪些需要的jar包,我这里列举了一个可以运行的spring+springmvc+hibernate的项目的所有jar包。
这里写图片描述
可能运行tomcat时候会有问题,剩下的就直接百度解决这个问题吧,因为在我这是可以运行的。
还有就是springmvc的配置就很简单了,因为他是单独的,所以就不说了,自己百度就一大把。
记得webxml里面要配置`
contextConfigLocation
classpath:xml的路径

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>`
以上都是自己最近几天慢慢整合出来的,还有借鉴了网上的好多知识。如果有不对的地方,欢迎大家指点出来
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值