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 知道为什么吗

    评论

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复