多个mapper location时, mybatis spring的自动扫描配置

时间:2023-02-09 11:52:44

1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔

2. SqlSessionFactoryBean里面的mapperLocations, 用<list><value></value>...</list>设置

例如:

<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="poolPreparedStatements" value="true" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="5" />
<property name="validationQuery" value="select 1" /><!-- This one is for MySQL -->
<!--<property name="validationQuery" value="select 1 from dual" /> This one is for Oracle -->
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
</bean> <bean id="dataSourceLocal" parent="dataSource">
<property name="driverClassName" value="${db.mysql.local.driver}" />
<property name="url" value="${db.mysql.local.url}" />
<property name="username" value="${db.mysql.local.user}" />
<property name="password" value="${db.mysql.local.password}" />
</bean>

<!-- 当有多个数据源时, 自sqlSessionFactory, mapperScannerConfigurer 到 transactionmanager, 都要设置两套id -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rockbb.facility.common.dao.mapper, com.rockbb.facility.weixin.dao.mapper" />
<!-- 当只有一个数据源时,这行可以不写. -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceLocal" />
<property name="mapperLocations">
<list>
<value>classpath:com/rockbb/facility/common/dao/mapper/*.xml</value>
<value>classpath:com/rockbb/facility/weixin/dao/mapper/*.xml</value>
</list>
</property>
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceLocal" />
</bean> <tx:annotation-driven transaction-manager="transactionManager" />

关于详细的配置项说明, 参考 http://haohaoxuexi.iteye.com/blog/1843309

当多个数据源都要使用事务时, 要这样定义

<tx:annotation-driven transaction-manager="transactionManagerA" />
<tx:annotation-driven transaction-manager="transactionManagerB" />

在代码中, 如果不显式指明使用哪个事务, 则默认使用先声明的那个, 本例中就是 transactionManagerA
显式指明使用哪个事务管理, 需要用这样的注解

@Transactional("transactionManagerB")
public void doSomething() {
//....
}

多个mapper location时, mybatis spring的自动扫描配置的更多相关文章

  1. Spring集成mybatis时mybatis的映射XML配置

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  2. spring-第N篇整合SSM,即Mybatis&plus;Spring&plus;Spring MVC

    1.Mybatis的配置使用 1>Jar包:mybatis-3.4.5.jar.mysql-connector-6.0.2或者ojdbc6-11.2.0.4.jar. 2>编写conf.x ...

  3. spring &plus; springMVC &plus;hibernate 配置2

    这种方式比较精简 配置项不多 spring采用自动扫描配置 ,分spring_springMVC.xml  . hibernate_config.xml 两个文件 web.xml配置如下 <?x ...

  4. spring和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    软件简介 Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛.http://projects.spring.io/spring-frame ...

  5. Mybatis&plus;Spring整合后Mapper测试类编写

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

  6. org&period;springframework&period;beans&period;factory&period;BeanCreationException&colon; Error creating bean with name &&num;39&semi;org&period;mybatis&period;spring&period;mapper&period;MapperScannerConfigurer&num;0&&num;39&semi;

    七月 05, 2018 10:26:54 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRul ...

  7. Spring和mybatis整合 org&period;mybatis&period;spring&period;mapper&period;MapperScannerConfigurer

    在springmvc与mybatis整合时,需要对每一个mapper定义对应的一个MapperFactoryBean,可以使用MapperScannerConfigurer自动扫描mapper,然后自 ...

  8. mybatis整合spring,使用org&period;mybatis&period;spring&period;mapper&period;MapperScannerConfigurer扫描出现问题

    <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...

  9. mybatis&plus;spring boot&comma; mapper 提示Could not autowire&period; No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

随机推荐

  1. Ruby Gem命令详解

    转自:http://www.jianshu.com/p/728184da1699 Gem介绍: Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.o ...

  2. SQL Server对比两字段的相似度(函数算法)

    相似度函数 概述    比较两个字段的相似度    最近有人问到关于两个字段求相似度的函数,所以就写了一篇关于相似度的函数,分别是“简单的模糊匹配”,“顺序匹配”,“一对一位置匹配”.在平时的这种函数 ...

  3. 基于小脚丫的ADC081S101 电压采集595数码管显示

    RTL结构图 采集模块运用SPI 通讯 MISO方式收集数据 module ad_collect(input sddata,input rst_n,output reg cs,output reg s ...

  4. C之按位运算符

    http://www.cnblogs.com/Kazaf/archive/2012/03/19/2406006.html

  5. SQL Server的小数数值类型(float 和 decimal)用法

    在SQL Server中,小数数值实际上只有两种数据类型:float 和 decimal.double precision 整体是数据类型,等价于 float(53),real等价于float(24) ...

  6. win7 64系统安装oracle客户端使用PL&sol;SQL Developer工具

    1)安装Oracle 11g 64位,我用的版本下载地址: http://www.oracle.com/technetwork/database/enterprise-edition/download ...

  7. nova availability zone

    find a bug: at first there is only one zone. create aggregate host1 in zone1 create aggregate host1 ...

  8. python - Django&colon; Converting an entire set of a Model's objects into a single dictionary - Stack Overflow

    python - Django: Converting an entire set of a Model's objects into a single dictionary - Stack Over ...

  9. VS怎样创建和使用lib文件

    假设你当前正在使用vs写了一个project,那么你想如今就生成一个Lib文件,那么能够直接在"项目属性"上进行改动i 项目属性->配置属性->常规->配置类型中 ...

  10. JSTL(JSP Standard Tag Library ,JSP标准标签库&rpar;

    JSTL标签之核心标签   JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管 ...