java - 使用 Hibernate 以及 IBatis 和 jdbc 连接池需要进行哪些更改

标签 java spring hibernate spring-mvc

我们希望将 Hibernate 5 集成到我们的项目中,复杂性在于我们将 IBatis 与 Spring 4 一起使用 我的 persistence.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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/MYJNDI" />

    <!-- SqlMap setup for iBATIS Database Layer -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="classpath:/sql-map-mysql.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven proxy-target-class="true" />

    <!-- Generic Dao - can be used when doing standard CRUD -->
    <bean id="baseDaoiBATIS" class="com.test.database.BaseDaoiBATIS">
        <property name="dataSource" ref="dataSource" />
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:mybatis-resources/*.xml" />
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sessionFactory" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.database.mapper" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostname}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="usePool" value="true"></property>
    </bean>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <property name="keySerializer" ref="stringRedisSerializer"></property>
        <property name="hashKeySerializer" ref="stringRedisSerializer"></property>
    </bean>

</beans>

我们应该做哪些改变才能让 Hibernate5 和 IBatis 一起使用 如果有任何建议,我们将不胜感激
注意:我们使用的是spring4,Ibatis,连接池

最佳答案

我们根据 @M.Deinum 的建议进行了更改,并且正在运行:
我们遵循的方法是:
[1] 使用 Hibernate-3 和 spring 3
[2] 使用 Hibernate Transaction Manager 而不是 DataSourceTransactionManager :

<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> -->

    <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="com.test.database.domain" />
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
         </props>
      </property>
   </bean>  
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="hibernateSessionFactory" />
   </bean> 
   <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

关于java - 使用 Hibernate 以及 IBatis 和 jdbc 连接池需要进行哪些更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320480/

相关文章:

java - 无法从字符串转换为时间戳;格式错误

java - 将 URL 重定向到另一个应用程序

java - JPA/HQL - 查找具有一对多关系且包含一个或多个特定元素的行

java - jdbc 模板故障安全 IN 语句

java - 自动提交关闭时 SQL JDBC java 提交

java - 如何安全释放netty缓冲区?

java - 方法实现的切入点

java - 更新 gwt CellList 中的图像

Spring session 启动错误 - 无法自动配置 session 存储库,请检查您的配置( session 存储类型为 'jdbc' )

hibernate - Hibernate 3.5 中的 @OrderColumn 注释