【求助】springmvc和mybatis整合时报错了,求个好心人?
java吧
全部回复
仅看楼主
level 10
2014年11月08日 01点11分 1
level 10
2014年11月08日 01点11分 2
level 15
缺包[黑线]
2014年11月08日 01点11分 3
dbcp吗?
2014年11月08日 01点11分
level 10
2014年11月08日 01点11分 4
level 12
有整合好的,
2014年11月08日 01点11分 6
我这个怎么办呢
2014年11月08日 01点11分
回复 西门_吹雪pj :你对照我的
2014年11月08日 01点11分
回复 常无忧2 :你的在哪呢
2014年11月08日 01点11分
回复 西门_吹雪pj :在这个贴吧
2014年11月08日 01点11分
level 10
这是目录
2014年11月08日 01点11分 7
level 10
这是整合的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.test" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></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:com/test/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
2014年11月08日 01点11分 8
level 10
这是实现类:
package com.test.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.dao.StudentMapper;
import com.test.model.Student;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
public StudentMapper studentMapper;
public StudentMapper getStudentMapper() {
return studentMapper;
}
@Autowired
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
public Student getStudentbysno(String sno) {
// TODO Auto-generated method stub
return studentMapper.selectByPrimaryKey(sno);
}
}
2014年11月08日 01点11分 9
level 11
检查一下xml文件,是不是少点东西
2014年11月08日 01点11分 10
你给我看看吧,我都发出来了
2014年11月08日 01点11分
level 10
这是junit测试,保的错在一楼,谁给我看看时那错了
package com.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.test.model.Student;
import com.test.service.StudentService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:mybatis-spring.xml"})
public class Testmybatis {
@Resource
private StudentService studentService = null;
@Test
public void test1(){
Student stu = studentService.getStudentbysno("12046324");
System.out.println(stu);
}
}
2014年11月08日 01点11分 11
level 10
警告: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name *studentService*: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name *studentMapper* defined in file [/home/jezas/Workspaces/MyEclipse Professional 2014/springmvc1 Maven Webapp/target/classes/com/test/dao/StudentMapper.class]: Cannot resolve reference to bean *sqlSessionFactory* while setting bean property *sqlSessionFactory*; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name *sqlSessionFactory* defined in class path resource [mybatis-spring.xml]: Cannot resolve reference to bean *dataSource* while setting bean property *dataSource*; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name *dataSource* defined in class path resource [mybatis-spring.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:260)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:63)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:83)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:74)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:116)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:251)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.ja
2014年11月08日 02点11分 15
level 10
第一个:Error creating bean with name *studentService*: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name *studentMapper* defined in file [/home/jezas/Workspaces/MyEclipse Professional 2014/springmvc1 Maven Webapp/target/classes/com/test/dao/StudentMapper.class]: Cannot resolve reference to bean *sqlSessionFactory* while setting bean property *sqlSessionFactory*; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name *sqlSessionFactory* defined in class path resource [mybatis-spring.xml]: Cannot resolve reference to bean *dataSource* while setting bean property *dataSource*; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name *dataSource* defined in class path resource [mybatis-spring.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name *studentMapper* defined in file [/home/jezas/Workspaces/MyEclipse Professional 2014/springmvc1 Maven Webapp/target/classes/com/test/dao/StudentMapper.class]: Cannot resolve reference to bean *sqlSessionFactory* while setting bean property *sqlSessionFactory*; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name *sqlSessionFactory* defined in class path resource [mybatis-spring.xml]: Cannot resolve reference to bean *dataSource* while setting bean property *dataSource*; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name *dataSource* defined in class path resource [mybatis-spring.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
2014年11月08日 02点11分 16
缺少 spring-jdbc 依赖
2015年03月15日 16点03分
level 10
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name *dataSource* defined in class path resource [mybatis-spring.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
2014年11月08日 02点11分 17
spring.xml没配置,应该是在整合文件里一起配置了吧,还是我在配置一遍呢
2014年11月08日 02点11分
level 10
Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
2014年11月08日 02点11分 18
level 10
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name *dataSource* defined in class path resource [mybatis-spring.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
2014年11月08日 02点11分 19
level 10
找个好心人你给我看看
2014年11月08日 02点11分 21
level 11
dao层的接口和实现xml 要放在一起,名字要一样。
2014年11月08日 02点11分 22
这个我试试
2014年11月08日 02点11分
你看看我配置文件正确吗
2014年11月08日 02点11分
回复 西门_吹雪pj :太长 不看
2014年11月08日 03点11分
level 12
包没倒全?
ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
2014年11月08日 03点11分 23
测试的时候好使了,连上springmvc时就出现连接异常了呢
2014年11月08日 05点11分
level 5
楼主贴栈就够了,缺包,apache.commons.dbcp这个,你看你在mvn依赖里是不是没有引,或者是版本太低
2014年11月08日 03点11分 24
是的,在add里面导错了,很感谢你
2014年11月08日 03点11分
为什么测试的时候好使,连上springmvc就出现数据库不能连接了
2014年11月08日 05点11分
回复 西门_吹雪pj :打的包里面有你的依赖包吗?
2014年11月08日 11点11分
回复 BackSpace_2008 :maven我手动加进去的
2014年11月08日 11点11分
1 2 尾页