四 Spring基于注解与mybatis整合

spring基于注解与mybatis整合配置代码例子如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       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.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
    <!--扫描所有注解-->
    <context:component-scan base-package="cn.*"/>
    <!--    引入数据库配置文件-->
    <context:property-placeholder location="database.properties"/>
    <!--    配置数据源-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
<!--    配置数据映射器-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--        引入数据源-->
        <property name="dataSource" ref="dataSource"/>
<property name="plugins">
    <array>
        <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
                <value>param1=value1</value>
            </property>
        </bean>
    </array>
</property>
<!--       设置 别名-->
        <property name="typeAliasesPackage" value="cn.pojo.*"/>
        <!--        mapper映射-->
        <property name="mapperLocations" value="cn/dao/*.xml"/>
<!--        内部配置-->
        <property name="configuration">
           <bean class="org.apache.ibatis.session.Configuration">
               <property name="mapUnderscoreToCamelCase" value="true"/>
               <property name="autoMappingBehavior" value="PARTIAL"/>
           </bean>
        </property>
    </bean>
<!--  自动扫描指定包下的Mapper接口,并将它们直接注册为MapperFactoryBean
MapperScannerConfigurer递归扫描基准包下所有接口,若它们在SQL映射文件中定义过,则动态注册为MapperFactoryBean,如此即可批量产生映射器实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="cn.dao"/>
    </bean>
<!--  注册事务管理-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--    开启事务注解扫描-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值