本文转自http://blog.csdn.net/zht666/article/details/38706083

Spring整合MyBatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般会使用MapperScannerConfigurer,MapperScannerConfigurer会自动扫描basePackage指定的包,找到映射接口类和映射XML文件,并进行注入。配置如下:

  1. <!-- 数据源 -->
  2. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  3. <property name="driverClass" value="${${database.type}.jdbc.driverClassName}"/>
  4. <property name="jdbcUrl" value="${${database.type}.jdbc.url}"/>
  5. <property name="properties" ref="dataSourceProperties"/>
  6. <property name="autoCommitOnClose" value="true"/>
  7. <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
  8. <property name="initialPoolSize" value="${cpool.minPoolSize}"/>
  9. <property name="minPoolSize" value="${cpool.minPoolSize}"/>
  10. <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
  11. <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
  12. <property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
  13. <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
  14. </bean>
  15. <!--基于注解的事务管理-->
  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  17. <property name="dataSource" ref="dataSource"/>
  18. </bean>
  19. <tx:annotation-driven transaction-manager="transactionManager"/>
  20. <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  21. <property name="dataSource" ref="dataSource"/>
  22. <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
  23. </bean>
  24. <!-- 扫描mybatis映射接口类 -->
  25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <property name="basePackage" value="com.test.dsm"/>
  27. <property name="sqlSessionFactoryBeanName" value="lazySqlSessionFactory"/>
  28. </bean>

这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.test.dsm)

如果myBatis映射XML文件和映射接口文件不放在同一个包下怎么办呢?

如果在不同的包下,那就需要手动配置XML文件的路径了,只需要修改SqlSessionFactoryBean配置即可:

  1. <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource"/>
  3. <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
  4. <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
  5. *是个通配符,代表所有的文件,**代表所有目录下 -->
  6. <property name="mapperLocations" value="classpath:com/test/mapper/mysql/**/*.xml" />
  7. </bean>

添加一个mapperLocations属性,指定加载xml文件的路径。

classpath:表示在classes目录中查找;

*:通配符表示所有文件;

**:表示所有目录下;

MyBatis官网说明如下:http://mybatis.github.io/spring/factorybean.html

Properties

SqlSessionFactory has a single required property, the JDBC DataSource . This can be any DataSource and should be configured just like any other Spring database connection.

One common property is configLocation which is used to specify the location of the MyBatis XML configuration file. One case where this is needed is if the base MyBatis configuration needs to be changed. Usually this will be <settings> or <typeAliases> sections.

Note that this config file does not need to be a complete MyBatis config. Specifically, any environments, data sources and MyBatis transaction managers will beignored . SqlSessionFactoryBean creates its own, custom MyBatis Environment with these values set as required.

Another reason to require a config file is if the MyBatis mapper XML files are not in the same classpath location as the mapper classes. With this configuration, there are two options. This first is to manually specify the classpath of the XML files using a <mappers> section in the MyBatis config file. A second option is to use themapperLocations property of the factory bean.

The mapperLocations property takes a list of resource locations. This property can be used to specify the location of MyBatis XML mapper files. The value can contain Ant-style patterns to load all files in a directory or to recursively search all paths from a base location. For example:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>

This will load all the MyBatis mapper XML files in the sample.config.mappers package and its sub-packages from the classpath.

MyBatis Spring整合配置映射接口类与映射xml文件的更多相关文章

  1. Mybatis+Spring整合后Mapper测试类编写

    public class UserMapperTest { private ApplicationContext applicationContext; @Before public void ini ...

  2. 【插件】【idea】的Mybatis Plugin插件方便mapper接口方法和mapper XML文件之间来回切换

    效果 安装 这是2019.2版本的,旧版的有点不一样

  3. springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题

    解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...

  4. mybatis与spring整合配置

    mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...

  5. Spring+Mybatis+Maven 整合配置

    <?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="by ...

  6. java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件

    APT的介绍: APT(Annotation Processing Tool)是一种注解处理工具,它对源代码文件进行检测,并找出源文件所包含的注解信息,然后针对注解信息进行额外的处理. 使用APT工具 ...

  7. Power Designer导出实体类和NHibernate xml文件

    Power Designer导出实体类和NHibernate xml文件 今天研究了一下通过PowerDesigner生成实体类和NHibernate所需要的xml文件,方法是通过Power Desi ...

  8. mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口

    搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里

  9. spring和mybatis的整合配置

    参考自: http://www.cnblogs.com/wangmingshun/p/5674633.html 链接中的文章里一共有三种整合方式,太多了怕记混了. 我这里只保留第二种. spring中 ...

随机推荐

  1. HNOI2017 游记

    如果你要问我为什么现在才发出来,那是因为我太懒了 Day0: 日常看板子……不想写题,嘴巴了几道题之后也不想写…… 到了晚上颓起来了……回想了一下似乎也没有立什么flag,那就愉快地颓吧……深感技术下 ...

  2. HDU3377 Plan

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3377 简单路径要求权值最大,那么为了回避括号序列单独插头的情况特判多,考虑使用最小表示法. #incl ...

  3. Oracel中的NVL函数

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  4. Python安装第三方库的安装技巧

    电脑:Windows10 64位. Python IDE 软件:JetBrains PyCharm Community Edition 2018.1.3 x64 Python version : Py ...

  5. The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet- mapping*,session-config?,mime-map

    修改了一下web.xml,加入了一个<filter>,然后就报这样的错??? The content of element type "web-app" must ma ...

  6. npm升级package.json依赖包到最新版本号

    转载自:https://blog.csdn.net/syaivin/article/details/79388244?utm_source=blogxgwz1 1.安装: npm install -g ...

  7. WebStorm Error : program path not specified

    1.出现这个错误是由于没有设置Node.js路径引起的. 2.下载安装Node.js. 3.设置对应的路径,设置后点一下Enable按钮即可. 以上,完.

  8. AtCoder Regular Contest 094 D Worst Case

    Worst Case 思路: 使 a <= b 当 a == b 时 或者 a == b - 1 时,答案显然为 2 * (a - 1) 否则找到最大的 c ,使得 c * c < a * ...

  9. lua --- Module

    首先需要明白,一般情况下,我们的定义的lua模块的文件与模块名(其实就是table的名字)是一致的,当然,不一致代码也是可以编译的(亲测),之所以这样,本人认为是为了实际项目中管理的方便.以下是定义模 ...

  10. Arduino 开关控制小灯持续亮之具体思路

    Arduino 开关控制小灯持续亮之具体思路 为什么写这篇文章: 我们用开关控制灯的亮灭的时候,希望只需要按一下按键就可以做到灯一直亮着.而在<Arduino魔法书>中——有弹性的按键这一 ...