解决:No qualifying bean of type 'org.apache.ibatis.session.SqlSession' available

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)

    ... 15 more


错误解决:cms-infra.xml配置文件加入

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>

    </bean>

cms-infra.xml配置文件内容示例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://mybatis.org/schema/mybatis-spring
        http://mybatis.org/schema/mybatis-spring.xsd">

    <import resource="classpath:/META-INF/spring/cms-env.xml" />

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/META-INF/mybatis/mybatis-config.xml" />
        <property name="mapperLocations" value="com/sh/cms/*/*Repository.xml"/>
        <property name="typeAliasesPackage" value="com.sh.cms" />
    </bean>

    <!-- scan for Mappers
    <mybatis:scan base-package="com.sh.cms.*.domain.repository" />-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.sh.cms.*.domain.repository" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  
    </bean>

</beans>

cms-env.xml文件内容示例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- CMS数据库连接 -->
    <bean id="dataSource" class="com.sh.xrsite.common.tools.datasource.TomcatEncryptedDataSource"
        destroy-method="close">
        <property name="url" value="${database.url}" />
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="initialSize" value="${tomcat.initialSize}" />
        <property name="maxActive" value="${tomcat.maxActive}" />
        <property name="maxIdle" value="${tomcat.maxIdle}" />
        <property name="minIdle" value="${tomcat.minIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${tomcat.timeBetweenEvictionRunsMillis}" />
        <property name="minEvictableIdleTimeMillis" value="${tomcat.minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${tomcat.validationQuery}" />
        <property name="validationInterval" value="${tomcat.validationInterval}" />
        <property name="testOnBorrow" value="${tomcat.testOnBorrow}" />
        <property name="removeAbandoned" value="${tomcat.removeAbandoned}" />
        <property name="removeAbandonedTimeout" value="${tomcat.removeAbandonedTimeout}" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>






阅读更多
个人分类: mysql
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭