首页 新闻 会员 周边 捐助

配置Springmvc+mybaits遇见的问题

0
悬赏园豆:140 [已关闭问题] 关闭于 2017-10-11 11:43
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <!-- apache.dbcp连接池的配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/db2"></property>
        <property name="username" value="root"></property>
        <property name="password" value="messi3838"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="30"></property>
        <property name="maxWait" value="500"></property>
        <property name="defaultAutoCommit" value="true"></property>
    </bean>
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:mapping/*.xml"></property>
         <property name="configLocation" value="classpath:/configs/spring-mybaits.xml" />
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao" />
     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

这是spring-mybaits.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" xmlns:jee="http://www.springframework.org/schema/jee"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
  
           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
  
             http://www.springframework.org/schema/context 
  
             http://www.springframework.org/schema/context/spring-context-4.0.xsd 
  
             http://www.springframework.org/schema/jee 
  
              http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 
  
              http://www.springframework.org/schema/mvc 
  
             http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
  
             http://www.springframework.org/schema/util 
  
             http://www.springframework.org/schema/util/spring-util-4.1.xsd">



    <context:component-scan base-package="controller" />
    <context:component-scan base-package="service" />
    <context:component-scan base-package="com.dao" />



    <mvc:resources mapping="/styles/**" location="/styles/" />

    <mvc:resources mapping="/scripts/**" location="/scripts/" />

    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:annotation-driven />
    <context:annotation-config />
    <mvc:default-servlet-handler />

    <!-- json 获得 -->

    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
        <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" 
        /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" 
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" 
        /> -->



    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/views/" />

        <property name="suffix" value=".html" />

    </bean>



</beans> 

这是spring—servlet.xml

package com.dao;

public interface LoginDao {
    public Integer isLogin(String username,String password);
}


这是dao

package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.LoginDao;

import exception.UsersException;
@Service
public class LoginServiceImp implements LoginService{
    @Autowired
    public LoginDao userService;
    
    public void isLogin(String username,String password){
        int count=userService.isLogin(username, password);
        System.out.println(count);
        if(count==0){
            
                throw new UsersException(001);
            
        }
        
        
    }
    
}

service实现类

 布局

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.LoginDao">
    <select id="isLogin" resultType="bean.UserBean">
        select count(*) from admin where username = #{username} and password =#{password}
    </select>
</mapper>

MyMapping.xml

 

 

[ERROR   ] Context initialization failed
Error creating bean with name 'loginController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dao.LoginDao service.LoginServiceImp.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
[ERROR   ] SRVE0271E: Uncaught init() exception created by servlet [Dispatcher] in application [spring2withfront]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dao.LoginDao service.LoginServiceImp.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dao.LoginDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:332)
    at [internal classes]

错误日志

一直报 No qualifying bean of type [com.dao.LoginDao] found for dependency 的错误不知道为什么 麻烦各位了

魔法少女加藤惠的主页 魔法少女加藤惠 | 初学一级 | 园豆:42
提问于:2017-10-10 19:45
< >
分享
所有回答(3)
0

@Repository
public interface LoginDao

Daniel Cai | 园豆:10424 (专家六级) | 2017-10-11 09:35

不是不能用在接口的么

支持(0) 反对(0) 魔法少女加藤惠 | 园豆:42 (初学一级) | 2017-10-11 09:57
0

 <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

        <property name="basePackage" value="com.dao" />  

    </bean>

你试试这个

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-10-11 10:24

还是一样

支持(0) 反对(0) 魔法少女加藤惠 | 园豆:42 (初学一级) | 2017-10-11 10:29

@魔法少女加藤惠: 你MyMapping.xml是不是写的有问题,parameterType="bean.UserBean"吧,

支持(0) 反对(0) 让我发会呆 | 园豆:2929 (老鸟四级) | 2017-10-11 10:40

@让我发会呆: parameterType是insert的吧

支持(0) 反对(0) 魔法少女加藤惠 | 园豆:42 (初学一级) | 2017-10-11 10:47

@魔法少女加藤惠: 感觉你这mybatis用的有点问题

你的那个接口loginDao,我们平时命名都叫xxxxMapper,为什么你这还有DaoImpl,它是干嘛的?
或者你试试这种写法,我下面我项目的例子,直接copy过来的,没有改参数。

@Select("select * from express_loan_info where customer_id=#{customerId} and offline=0 order by create_time desc")

List<ExpressLoanInfo> listLoanInfoByCustomerId(@Param("customerId") String customerId);

这样可以不用写mapper.xml,你再试试能不能把你的那个dao注入进去。

支持(0) 反对(0) 让我发会呆 | 园豆:2929 (老鸟四级) | 2017-10-11 10:55

@让我发会呆: DaoImpl里面没东西 我把他删了

支持(0) 反对(0) 魔法少女加藤惠 | 园豆:42 (初学一级) | 2017-10-11 11:01
0

问题已解决

自己犯二了

    <servlet>

        <servlet-name>Dispatcher</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 自定义spring mvc的配置文件名称和路径 -->

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:configs/spring-servlet.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

<param-value>classpath:configs/spring-servlet.xml</param-value>里边改成

<param-value>classpath:configs/spring-*.xml</param-value>

魔法少女加藤惠 | 园豆:42 (初学一级) | 2017-10-11 11:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册