美文网首页
Spring4.3.4和Hibernate 5.2.3整合

Spring4.3.4和Hibernate 5.2.3整合

作者: 呆呆_ | 来源:发表于2017-01-20 01:26 被阅读0次

    开始前先阅读下文:
    http://blog.csdn.net/frankcheng5143/article/details/50634487

    1. hibernate.cfg.xml不在需要数据库连接的配置,删掉

    删除前:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <!-- 配置Hibernate的基本属性 -->
            <!-- 1.数据源配置到IOC容器中 -->
            <!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
            <!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
            <property name="connection.url">jdbc:mysql://xxx.xxx.xxx.xxxx:3306/xxxxx</property>
            <property name="connection.username">root</property>
            <property name="connection.password">xxxxxxxx</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    
            <!-- 数据库连接池的大小 -->
            <property name="connection.pool_size">5</property>
            <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
            <property name="jdbc.fetch_size">50 </property>
            <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
            <property name="jdbc.batch_size">23 </property>
            <!-- SQL 方言 -->
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
            <!-- 在控制台输出sql语句 -->
            <property name="show_sql">true</property>
            <!-- 在启动时根据配置更新数据库 -->
            <property name="hbm2ddl.auto">update</property>
    
            <mapping class="com.xxxx.fabu.model.CheckItem"/>
            <mapping class="com.xxxx.fabu.model.User"/>
            <mapping class="com.xxxx.fabu.model.CheckInfo"/>
        </session-factory>
    </hibernate-configuration>
    
    

    修改后:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
    
            <!-- 数据库连接池的大小 -->
            <property name="connection.pool_size">5</property>
            <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
            <property name="jdbc.fetch_size">50 </property>
            <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
            <property name="jdbc.batch_size">23 </property>
            <!-- SQL 方言 -->
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
            <!-- 在控制台输出sql语句 -->
            <property name="show_sql">true</property>
            <!-- 在启动时根据配置更新数据库 -->
            <property name="hbm2ddl.auto">update</property>
    
            <mapping class="com.xxxx.fabu.model.CheckItem"/>
            <mapping class="com.xxxx.fabu.model.User"/>
            <mapping class="com.xxxx.fabu.model.CheckInfo"/>
        </session-factory>
    </hibernate-configuration>
    
    

    2. 创建database.properties文件

    ##在applicationContext.xml文件之中引入此配置的资源文件
    db.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx
    db.user=root
    db.password=xxxxxx
    pool.max=100
    pool.min=20
    pool.init=10
    pool.idle=100
    

    3. 在继续配置前先添加必要的命名空间

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-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.xsd
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--在当前的配置之中采用Annotation的注解方式进行定义-->
        <context:annotation-config/>
        <!--定义扫描包,此包中定义的所有的程序类支持Annotation-->
        <context:component-scan base-package="com.xxxx"/>
    
    
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        </bean>
    
    </beans>
    
    

    4.配置数据库连接池

    在applicationContext.xml文件中添加上文的database.properties配置文件

    <context:property-placeholder location="classpath:database.properties" />
    
    

    配置dataSource、sessionFactory,最后applicationContext.xml如下:

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-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.xsd
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--在当前的配置之中采用Annotation的注解方式进行定义-->
        <context:annotation-config/>
        <!--定义扫描包,此包中定义的所有的程序类支持Annotation-->
        <context:component-scan base-package="com.xxxx"/>
    
        <!--设置要导入的资源文件路径,直接通过classpath加载-->
        <context:property-placeholder location="classpath:database.properties"/>
    
        <!--配置数据库连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${db.driver}"/>
            <property name="jdbcUrl" value="${db.url}"/>
            <property name="user" value="${db.user}"/>
            <property name="password" value="${db.password}"/>
            <property name="maxPoolSize" value="${pool.max}"/>
            <property name="minPoolSize" value="${pool.min}"/>
            <property name="initialPoolSize" value="${pool.init}"/>
            <property name="maxIdleTime" value="${pool.idle}"/>
        </bean>
    
        <!--配置hibernate中的SessionFactory程序类,此时将通过spring负责管理-->
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!--配置hibernate.cfg.xml文件的引用路径,通过classpath引用-->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
            <!--SessionFactory的执行需要dataSource支持,将之前的c3p0的数据源配置进来-->
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    </beans>
    

    5.事务

    <!--配置Hibernate的事务控制,同时可以实现数据库的关闭处理-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    

    相关文章

      网友评论

          本文标题:Spring4.3.4和Hibernate 5.2.3整合

          本文链接:https://www.haomeiwen.com/subject/pbfwbttx.html