Dfly1040 2016-10-15 10:23 采纳率: 0%
浏览 4579

spring-boot和mybatis配置多数据源,service找不到bean

在配置多数据源时候在service层的时候如果只用@Autowired的时候会因为有多个数据源而报错,但当我加上@Qualifier("sqlSessionFactory")的时候又会查不到对应的bean是我的注解写的有问题?

配置文件如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">

<!-- 读取数据库连接信息配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:db.properties" />
</bean>

<!-- 配置数据源连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="maxActive" value="100"/>
    <property name="initialSize" value="5"/>
    <property name="minIdle" value="5"/>
    <property name="maxWait" value="60"/>
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testWhileIdle" value="true"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="false"/>
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- 配置数据源管理对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> -->
    <property name="mapperLocations" value="classpath:com/ocss/sqlmap/*.xml" />
    <property name="typeAliasesPackage" value="com.ocss.dao"/>
</bean>
<!-- 配置数据源自动实装 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.ocss.dao" />
    <property name="processPropertyPlaceHolders" value="true" />
    <!-- <property name="sqlSessionTemplateBeanName" value="sqlSessionFactory" /> -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 配置数据源事务控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

 <!-- 配置数据源连接池2 -->
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName2}"/>
    <property name="url" value="${jdbc.url2}"/>
    <property name="username" value="${jdbc.username2}"/>
    <property name="password" value="${jdbc.password2}"/>
    <property name="maxActive" value="100"/>
    <property name="initialSize" value="5"/>
    <property name="minIdle" value="5"/>
    <property name="maxWait" value="60"/>
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testWhileIdle" value="true"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="false"/>
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- 配置数据源管理对象 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> -->
    <property name="mapperLocations" value="classpath:com/his/sqlmap/*.xml" />
    <property name="typeAliasesPackage" value="com.his.dao"/>
</bean>
<!-- 配置数据源自动实装 -->
<bean id="mapperScanner2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.his.dao" />
    <property name="processPropertyPlaceHolders" value="true" />
    <property name="sqlSessionTemplateBeanName" value="sqlSessionFactory2" />
    <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" /> -->
</bean>
<bean id="sqlSession2" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory2" />
</bean>
<!-- 配置数据源事务控制 -->
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource2" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager2" />

service如下

@Service
public class InfoContentServiceImpl implements InfoContentService {

@Autowired
//@Qualifier("sqlSessionFactory")
private InfoContentDao infoContentDao;
  • 写回答

4条回答 默认 最新

  • qi_gaolei 2019-11-08 14:14
    关注

    我配置好俩个数据源 用注解可以查询成功 可是用mapper就报找不到 我用的mabatic 知道为什么吗

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧