龙空技术网

SpringMVC用junit测试mybatis时报错No qualifying bean of type

随机印变 813

前言:

目前兄弟们对“springmvc连接mysql会遇到哪些问题”大约比较重视,同学们都想要了解一些“springmvc连接mysql会遇到哪些问题”的相关资讯。那么小编也在网上汇集了一些有关“springmvc连接mysql会遇到哪些问题””的相关文章,希望你们能喜欢,看官们快快来学习一下吧!

看这篇文章前我先给大家澄清一点撒,昨天不是说下一篇文章发表用Nginx搭载高性能负载均衡项目集群嘛,But。头条不给我机会啊,写好了准备在2017-09-20发布的,(所以只能等到2017-09-21发布了)结果,审核不通过,说是以前有人发布过,是旧闻,这就尴尬了 !还有前天写的功能点,都说是重复了,我想吐槽了。每个人都每个人的想法和技术,别人发过的就不能再发了我觉得很鸡肋,怕的是前面先写的人写的不够完整。然后第二个人想补充都没办法呢!(如下图)

排版,码了半天,给我整一个旧闻,审核不通过。前功尽弃,好了发点牢骚,大家往下看吧!

有兴趣的朋友一起学习java 可以加群---《专注JavaWeb开发》 群号:162582394注意了,以下代码可能会让你感觉到不适,没错,当我看到这些代码的时候,我说了一句,草泥马比哦,写这个项目的程序员刚毕业吧!(处处是坑,我现在在公司就主要是解决以前辞职走的那个人写的代码)一、发生的场景:最近在家休假,无聊看公司以前做的项目的时候,发现,有些问题。然后自己写一个junit测试。就发生错误!二、环境springMVC+mybatis+mysql项目中用junit测试时,总是报下面的错

先看看测试类:

package com.aidongsports.backstagemanagement.test;

import com.aidongsports.backstagemanagement.dao.UserMapper;

import com.aidongsports.backstagemanagement.entity.Users;

import com.aidongsports.backstagemanagement.service.UserService;

import org.apache.log4j.Logger;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/config/spring-mybatis.xml"})

public class TestMyBatis {

private static Logger logger = Logger.getLogger(TestMyBatis.class);

@Resource

private UserMapper usersDao;

@Resource

private UserService userService;

@Test

public void test1() {

Users users = usersDao.getUserById(1001);

System.err.println("第一个测试"+users);

List<Users> user = userService.getAllUser("小明","男");

System.err.println("第二个测试"+user);

List<Users> user1 = usersDao.selectAllUser("小强","男");

System.err.println("第三个测试"+user1.get(0));

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("file:src/main/webapp/WEB-INF/config/spring-mybatis.xml");

System.err.println(applicationContext.toString());

}

}

运行报错如下:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aidongsports.backstagemanagement.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

意思好像是spring没有注入UserMapper这个类,找了一晚上,原因是:spring-mybatis.xml配置中加载了很多mapper文件。 如下图:

但是呢UserMapper.xml中有些查询语句resultType中返回的是一个User实体,我们这里用的是别名,所以直接在resultType中写的别名。 如下图:

再但是呢mybatis类别名是配置在mybatis-config.xml这个配置中,而我的测试类中没有加载这个mybatis-config.xml文件,导致报错。如下图:

第一种解决方法:将所有mapper.xml文件中返回实体的,全部改成类全路径,不使用类别名。如下图:

第二种解决方法:在测试类中加载mybatis-config.xml配置文件。如下图

第三种解决方法:在spring-mybatis.xml配置文件 加载 mybatis-config.xml,去mybatis-config.xml中读取到类别名,这样 UserMapper.xml就可以找到对应的类。(推荐这种解决)如下图

需要注意的是:1.spring-mybatis.xml配置中的<property name="configLocation" value="file:src/main/webapp/WEB-INF/config/mybatis-config.xml"></property>使用junit测试时写的路径和实际使用tomcat跑的时候写的路径是不相同的。注意区分,使用junit测试时:如下写法

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="file:src/main/resources/mybatis-config.xml"></property> //如果文件在resources下,使用这个

<property name="configLocation" value="file:src/main/webapp/WEB-INF/config/mybatis-config.xml"></property>//如果文件在WEB-INF下,使用这个

</bean>

使用tomcat跑的时候:如下写法

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="/WEB-INF/config/mybatis-config.xml"></property>

</bean>

解决以后:贴上配置文件

spring-mybatis.xml

mybatis-config.xml

spring-mvc.xml

贴上测试类。dao层和service层就不贴了,跟平常写的代码一样

junit测试类

packaport com.aidongsports.backstagemanagement.dao.UserMapper;

import com.aidongsports.backstagemanagement.entity.Users;

import com.aidge com.aidongsports.backstagemanagement.test;

imongsports.backstagemanagement.service.UserService;

import org.apache.log4j.Logger;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类

//@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) //spring-mybatis.xml文件需要在class路径下,也就是在src/main/java下面

//@ContextConfiguration(locations = {"file:src/main/resources/spring-mybatis.xml"})//spring-mybatis.xml文件需要在resources路径下

//@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/config/spring-mybatis.xml"})//spring-mybatis.xml文件需要在WEB-INF/config路径下

@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/config/mybatis-config.xml","file:src/main/webapp/WEB-INF/config/spring-mybatis.xml"})

public class TestMyBatis {

private static Logger logger = Logger.getLogger(TestMyBatis.class);

@Resource

private UserMapper usersDao;

@Resource

private UserService userService;

@Test

public void test1() {

Users users = usersDao.getUserById(1001);

System.err.println("第一个测试"+users);

List<Users> user = userService.getAllUser("小明","男");

System.err.println("第二个测试"+user);

List<Users> user1 = usersDao.selectAllUser("小强","男");

System.err.println("第三个测试"+user1.get(0));

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("file:src/main/webapp/WEB-INF/config/spring-mybatis.xml");

System.err.println(applicationContext.toString());

}

}

有兴趣的朋友一起学习java 可以加群---《专注JavaWeb开发》 群号:162582394

码农不容易,小编给大家分享写博客也不容易,请多多支持,喜欢请关注头条号,每天都有功能和bug分享给大家一起学习进步,我们的目标是 ----软件攻城狮

标签: #springmvc连接mysql会遇到哪些问题