Tích hợp Spring với MyBatis và C3P0

Để tích hợp Spring Framework với MyBatis và sử dụng C3P0 làm connection pool, chúng ta cần cấu hình các tệp XML sau:

1. Tệp cấu hình Spring cho Mapper (spring-mapper.xml)

Tệp này chịu trách nhiệm cấu hình kết nối cơ sở dữ liệu, connection pool và cách Spring quản lý các Mapper của MyBatis.


<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 1. Tải cấu hình từ tệp properties -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- 2. Cấu hình Connection Pool sử dụng C3P0 -->
    <bean id="poolingDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.databaseUrl}"/>
        <property name="user" value="${jdbc.dbUser}"/>
        <property name="password" value="${jdbc.dbPassword}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="initialPoolSize" value="10"/>
    </bean>

    <!-- 3. Cấu hình SqlSessionFactoryBean -->
    <bean id="myBatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="poolingDataSource"/>
        <!-- Liên kết tệp cấu hình MyBatis -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!-- 4. Quét các Interface Mapper (DAO) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="myBatisSqlSessionFactory"/>
        <!-- Thư mục chứa các Interface Mapper -->
        <property name="basePackage" value="com.example.repository"/>
    </bean>

</beans>

Tệp cấu hình cơ sở dữ liệu (database.properties)

Đảm bảo tệp này chứa các thuộc tính JDBC theo định dạng sau:


jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.databaseUrl=jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
jdbc.dbUser=root
jdbc.dbPassword=your_password

2. Tệp cấu hình Spring cho Service (spring-service.xml)

Tệp này tập trung vào việc cấu hình các lớp Service và quản lý giao dịch.


<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Quét các lớp Service -->
    <context:component-scan base-package="com.example.service"/>

    <!-- Tự động inject các dependency cho Service -->
    <bean id="userService" class="com.example.service.UserServiceImpl">
        <property name="userRepository" ref="userRepository"/>
    </bean>

    <!-- Cấu hình Transaction Manager -->
    <bean id="transactionalManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- Inject DataSource -->
        <property name="dataSource" ref="poolingDataSource"/>
    </bean>

</beans>

3. Tệp cấu hình chính (applicationContext.xml)

Tệp này sẽ import các tệp cấu hình con lại với nhau.


<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Import tệp cấu hình Mapper -->
    <import resource="classpath:spring-mapper.xml"/>

    <!-- Import tệp cấu hình Service -->
    <import resource="classpath:spring-service.xml"/>

</beans>

Đảm bảo rằng tất cả các tệp cấu hình XML này nằm trong thư mục tài nguyên (src/main/resources) hoặc một thư mục được chỉ định rõ ràng trong cấu trúc dự án của bạn.

Thẻ: Spring mybatis c3p0 jdbc Configuration

Đăng vào ngày 26 tháng 10 lúc 05:31