spring整合mybatis 配置xml文件

这是一个关于Spring与MyBatis集成配置的详细示例。配置文件包括`spring.xml`和`mybatis.xml`,其中`jdbc.properties`用于存储数据库连接信息。`spring.xml`扫描`com.service`包下的服务类,而`mybatis.xml`设置了数据源、SqlSessionFactory以及MapperScannerConfigurer来自动扫描`com.dao`包下的DAO接口。整个配置实现了Spring和MyBatis的无缝整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--mybatis主配置文件-->
    <!--    引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="jdbc.properties"></property>
    </bean>
    <!--    配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <!--        初始化连接大小-->
        <property name="initialSize" value="${initialSize}"></property>
        <!--        连接池最大数量-->
        <property name="maxActive" value="${maxActive}"></property>
        <!--        连接池最大空闲-->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!--        连接池最小空闲-->
        <property name="minIdle" value="${minIdle}"></property>
        <!--        获取链接最大等待时间-->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>
    <!--    spring和Mybatis完美整合,不需要mybatis的映射文件-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <!--        自动扫描mapperxml文件夹  类似于mybatis中主配置文件Mapper.xml文件 中的mappers-->
        <property name="mapperLocations" value="classpath:mapperxml/*xml"></property>
    </bean>
    <!--    DAO接口所在包名,Spring会自动查找其下的接口类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>
    </bean>
</beans>

spring.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:context="http://www.springframework.org/schema/context"
       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.xsd">
<!--spring的核心配置文件-->
    <context:component-scan base-package="com.service"></context:component-scan>
<!--<bean class="com.service.impl.StudentServiceImpl" id="studentService">
    <property name="studentMapper" ref="studentMapper"></property>
</bean>-->
<!--    包含mybatis的核心配置文件-->
    <import resource="springMybatis.xml"></import>
</beans>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值