spring整合mybatis的核心配置文件 application-mybatis.xml

spring整合mybatis的核心配置文件 application-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"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 1. 在resources 目录下创建一个 druid.properties 配置文件,提供数据库连接相关配置 -->

    <!-- 2.把创建的属性配置文件引入到核心配置文件 application-mybatis.xml 中,这样就可以通过 ${key} 的形式获取对应的配置值 -->
    <context:property-placeholder location="classpath:druid.properties" />

    <!-- 3.创建数据源 DruidDataSource 通过 OGNL 表达式获取对应的值进行属性注入 -->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}"/>
        <property name="password" value="${pwd}"/>

        <property name="initialSize" value="${initSize}" />
        <property name="maxActive" value="${maxSize}" />
        <property name="minIdle" value="${minSize}" />
    </bean>

    <!-- 4.创建SqlSessionFactoryBean工厂 , 把数据源,mapper.xml文件以及分页插件注入给工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="druidDataSource" />
        <!-- mapper 所对应的文件地址-->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
        <!-- 分页插件 -->
        <!-- 这里,plugins 需要的是动态参数 Interceptor 拦截器,由于只会用到一次,则在这里通过属性 array 的形式注入 -->
        <property name="plugins">
            <!-- 这里是一个动态数组 -->
            <array>
                <bean class="com.github.pagehelper.PageInterceptor" />
            </array>
        </property>
    </bean>

    <!-- 5. 创建mapper扫描配置,把SqlSessionFactoryBean 工厂以及mapper 接口所在的包路径注入给配置 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 这个属性需要的是字符串类型,所以使用 value 即可,如果是对象则需要会用 ref 引入,当然也可以使用class -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- mapper 接口所在的包路径注入给配置 -->
        <property name="basePackage" value="org.neuedu.springmvc.mybatis.swagger.mapper"/>
    </bean>

    <!-- 6. 事务管理器配置,使用注解的形式替代 AOP 配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <!-- 获取数据源 -->
        <property name="dataSource" ref="druidDataSource" />
    </bean>

    <!-- 7. 开启 tx 事务注解管理,使用注解的形式替代 AOP 配置, 后续如果有哪个方法要用到事务管理,只需加上一个注解即可 @Transactional -->
    <!-- 这里需要引用 6 的事务管理器配置 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 8. 开启扫描Service 注解 -->
    <context:component-scan base-package="org.neuedu.springmvc.mybatis.swagger.service.impl" />
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值