美文网首页
Spring与MyBatis整合

Spring与MyBatis整合

作者: 青年心路 | 来源:发表于2019-05-24 16:28 被阅读0次

1.基本用法
1.1添加依赖

    <!--Spring整合MyBatis-->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>

      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
      </dependency>

1.2创建Dao实现类
对于MyBatis而言,可以创建Dao的实现类,也可以不创建,一般都不创建
DataSource——>SqlSessionFactory——>Dao——>Service——>Action

1.3创建映射文件

    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--也可以指定配置文件,但是一般情况下不使用-->
        <!--<property name="configLocation" value="classpath:"-->

        <property name="dataSource" ref="dataSource"/>
        <!--指定mybatis配置文件的映射路径-->
        <property name="mapperLocations" value="classpath:com/hxx/mapper/*Mapper.xml"/>
        <!--为映射类指定别名   通过包名指定-->
        <property name="typeAliasesPackage" value="com.hxx.entity"/>
    </bean>

2.不创建实现类

    <!--通过反射动态的创建dao实现类,并添加到IoC容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定接口的位置-->
        <property name="basePackage" value="com.hxx.dao"/>
    </bean>

相关文章

网友评论

      本文标题:Spring与MyBatis整合

      本文链接:https://www.haomeiwen.com/subject/xcfizqtx.html