spring+springmvc+mybatis+maven整合报错?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

spring+springmvc+mybatis+maven整合报错?报错

2020-06-08 11:20:33 223 1
package test.dao;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tedu.cloud_note.dao.UserDao;
import cn.tedu.cloud_note.entity.User;

public class UserDaoTest {
  @Test
  public void  testUserDao() {
    ApplicationContext ctx=
        new ClassPathXmlApplicationContext("conf/spring-mybatis.xml");
    UserDao dao=ctx.getBean("userDao",UserDao.class);
    User user=dao.findByName("demo");
    System.out.println("user:"+user);
  }
  
  @Test
  public void testSave(){
    String[] conf={"/conf/spring-mybatis.xml",
          "/conf/spring-mvc.xml"};
    //实例化ctx对象
    ApplicationContext ctx=
        new ClassPathXmlApplicationContext(conf);
    //获取UserDao对象
    UserDao dao=ctx.getBean("userDao",UserDao.class);
    User user=new User();
    user.setCn_user_id("1465778979");
    user.setCn_user_name("烤鸭");
    user.setCn_user_password("123456");
    user.setCn_user_desc("烤鱼");
    
    //通过dao执行sava方法
    dao.save(user);
    System.out.println(user);
  }
  
}










新人小白求问:为什么在ctx的代码中不能传入conf属性,传入的话就会报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
	at test.dao.UserDaoTest.testSave(UserDaoTest.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271)
	at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:191)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
	... 35 more

但是用这个就不报错。。。。

 @Test
  public void testSave(){
    /*String[] conf={"conf/spring-mybatis.xml",
          "conf/spring-mvc.xml"};*/
    //实例化ctx对象
    ApplicationContext ctx=
        new ClassPathXmlApplicationContext("conf/spring-mybatis.xml");

 

 

spring-mvc.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-2.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	
	<!-- 组件扫描 ,扫描controller,service组件-->
	<context:component-scan base-package="cn.tedu.cloud_note"></context:component-scan>
	
	<!-- handlermapping,支持@RequestMapping,@RequestBody 
		@Exceptionhandler.
		启用注解驱动-->
	<mvc:annotation-driven />
	
	
</beans>




spring-mybatis.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:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	<!-- Spring针对数据库主要如下三个 -->	
	<!-- 配置数据库连接参数及连接池 -->
	<bean id="dbcp" 
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="username" value="root"></property>
		<property name="password" value="6225104"></property>
		<property name="driverClassName" 
			value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property>
	</bean>
	<!-- 配置SqlSessionFactory -->
	<bean id="ssf" 
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置dataSource -->
		<property name="dataSource" ref="dbcp"></property>
		<!-- 配置mapper映射文件 -->
		<property name="mapperLocations" 
			value="classpath:mapper/*.xml"></property>
		
	</bean>
	<!-- 配置mapper扫描 -->
	<bean id="mapperScanner" 
		class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tedu.cloud_note.dao"></property>	
	</bean>
	
	
</beans>




第一次发也不知道格式对不对~~求大神解答!!

取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-08 11:20:51
    @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:config/spring-mybatis.xml")

    config前应该加/,你这是要测mapper吧,在测试类上加上这个就可以,不需要用你这种方式,

    你用的这种是什么方法啊???大神,你用的这些标记我都不认识,而且SpringJUnit也找不到;下面的@ContextConfigration也没有;在config前面加/也还是会报错。。。
    0 0
相关问答

20

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 1224383浏览量 回答数 20

170

回答

惊喜翻倍:免费ECS+免费环境配置~!(ECS免费体验6个月活动3月31日结束)

豆妹 2014-10-29 17:52:21 232294浏览量 回答数 170

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 340475浏览量 回答数 8

119

回答

OSS存储服务-客户端工具

newegg11 2012-05-17 15:37:18 301048浏览量 回答数 119

24

回答

阿里云开放端口权限

xcxx 2016-07-20 15:03:33 658039浏览量 回答数 24

39

回答

安全组详解,新手必看教程

我的中国 2017-11-30 15:23:46 262887浏览量 回答数 39

21

回答

请教一下数据量有100万条左右要什么配置?

易网网络 2013-03-27 15:18:02 193544浏览量 回答数 21

251

回答

阿里云LNAMP(Linux + Nginx + Apache + MySQL + PHP)环境一键安装脚本

云代维 2014-02-14 15:26:06 309345浏览量 回答数 251

24

回答

【精品问答】python技术1000问(1)

问问小秘 2019-11-15 13:25:00 483391浏览量 回答数 24

2

回答

区域选择帮助

fanyue88888 2012-12-07 15:54:30 205416浏览量 回答数 2
+关注
爱吃鱼的程序员
https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
2
文章
21568
问答
问答排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载