《建设监理》网站,无锡做网站费用,网站访问统计报告模板,四川网络营销推广在学习Spring完之后简单的了解了MyBatis。然后进行简单的整合#xff0c;遇到MyBatista接口映射的Bean无法自动注入的问题#xff1b; 代码异常#xff1a; 线程“main”org.springframe .bean .factory中的异常。创建名为“UserController”的bean时出错:通过字段“userdao… 在学习Spring完之后简单的了解了MyBatis。然后进行简单的整合遇到MyBatista接口映射的Bean无法自动注入的问题 代码异常 线程“main”org.springframe .bean .factory中的异常。创建名为“UserController”的bean时出错:通过字段“userdao”表示的不满足的依赖关系;嵌套异常是org.springframe .bean .factory。BeanCreationException:在文件[C:\Users\li rui long\eclipse-workspace\MyBatis_Demo\build\classes\com\mybatis\dao\ userdao]中创建名为“userdao”的bean时出错。类]:在设置bean属性“sqlSessionFactory”时无法解析对bean“sqlSessionFactory”的引用;嵌套异常是org.springframe .bean .factory。BeanCreationException:在类路径资源[ApplicationContext]中定义名称为“sqlSessionFactory”的bean创建错误。:设置bean属性“dataSource”时不能解析对bean“dataSource”的引用;嵌套异常是org.springframe .bean .factory。BeanCreationException:创建名为“dataSource”的bean时出错:查找方法解析失败;嵌套异常是java.lang。IllegalStateException:未能从ClassLoader [jdk.internal.loader.ClassLoader . $AppClassLoader77a567e1]内检类[org.apache.commons.dbcp2.BasicDataSource] 异常提示控制器层的Bean无法创建原因是MyBatis对应的映射接口无法完成映射无法生成DAO层的Bean 所以问题应该出现在XML文件里 测试类13行报错 1 package com.mybatis.test;2 3 import org.springframework.context.ApplicationContext;4 import org.springframework.context.support.ClassPathXmlApplicationContext;5 6 import com.mybatis.controller.UserController;7 8 9 public class Test_Controller {
10
11 public static void main(String[] args) {
12 // TODO Auto-generated method stub
13 ApplicationContext app new ClassPathXmlApplicationContext(ApplicationContext.xml);
14 UserController coll (UserController) app.getBean(UserController);
15 coll.test();
16 }
17
18 } 对ApplicationContext.xml配置文件进行一步步排查 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd!-- 指定需要扫描的包使注解生效 --context:component-scan base-packagecom.mybatis.po/context:component-scan base-packagecom.mybatis.dao/context:component-scan base-packagecom.mybatis.Controller/!-- 配置数据源 --
bean iddataSource classorg.apache.commons.dbcp2.BasicDataSource destroy-methodcloseproperty namedriverClassName value com.mysql.jdbc.Driver/property nameurl value jdbc:mysql://localhost:3306/Springtest?characterEncodingutf8/property nameusername value root/property namepassword value mysql /!-- 可同时连接的最大的连接数 --property namemaxActive value60 /!-- 最大的空闲的连接数 --property namemaxTotal value60 /!-- 最小的空闲的连接数低于这个数量会被创建新的连接默认为0 --property namemaxIdle value5 / !-- 连接池启动时创建的初始化连接数量默认值为0 -- property nameinitialSize value5 / /bean !-- 添加事务支持 --bean id txManager class org.springframework.jdbc.datasource.DataSourceTransactionManagerproperty name dataSource ref dataSource//bean!-- 开启事务注解 --tx:annotation-driven transaction-manager txManager/!-- 配置Mybatis工厂同时指定数据源并与MyBatista完美结合 --bean idsqlSessionFactory class org.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource ref dataSource/!-- configLocation 的属性为Mybatis 的核心配置文件 --property name configLocation value classpath:mybatis-config.xml/property/bean!-- Mapper 代理开发使用Spring自动扫描MyBatista的接口并装配 --!-- Spring 将指定包中所有的被Mapper注解标注的接口自动装配为MyBatatis的映射接口 --bean class org.mybatis.spring.mapper.MapperScannerConfigurer!-- MyBatis-spring组件的扫描器 --property namebasePackage value com.mybatis.dao/property namesqlSessionFactoryBeanName value sqlSessionFactory/ /bean/beans 检查扫描的包名,是否有写错或者少写的。确定数据源的配置正常我的问题就出在这里修改数据库配置信息(密码等)看是否会报不一样的错当还是原来的错说明配置文件没有加载或者数据源错误。我用的DBCP数据源(需要导入两个包DBCP连接池包)修改密码后还是报同样的错误所以我尝试着用Spring自带的数据源解决了问题正确代码 1 ?xml version1.0 encodingUTF-8?2 beans xmlnshttp://www.springframework.org/schema/beans3 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance4 xmlns:contexthttp://www.springframework.org/schema/context5 xmlns:txhttp://www.springframework.org/schema/tx6 xsi:schemaLocation7 http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans.xsd9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/tx
12 http://www.springframework.org/schema/tx/spring-tx.xsd
13 !-- 指定需要扫描的包使注解生效 --
14
15 context:component-scan base-packagecom.mybatis.po/
16 context:component-scan base-packagecom.mybatis.dao/
17 context:component-scan base-packagecom.mybatis.Controller/
18 !-- 配置数据源 --
19 bean id dataSource class org.springframework.jdbc.datasource.DriverManagerDataSource
20 property namedriverClassName value com.mysql.jdbc.Driver/
21 property nameurl value jdbc:mysql://localhost:3306/Springtest?characterEncodingutf8/
22 property nameusername value root/
23 property namepassword value mysql /
24 /bean
25
26 !-- 添加事务支持 --
27 bean id txManager class org.springframework.jdbc.datasource.DataSourceTransactionManager
28 property name dataSource ref dataSource/
29 /bean
30 !-- 开启事务注解 --
31 tx:annotation-driven transaction-manager txManager/
32 !-- 配置Mybatis工厂同时指定数据源并与MyBatista完美结合 --
33 bean idsqlSessionFactory class org.mybatis.spring.SqlSessionFactoryBean
34 property namedataSource ref dataSource/
35 !-- configLocation 的属性为Mybatis 的核心配置文件 --
36 property name configLocation value classpath:mybatis-config.xml/property
37 /bean
38 !-- Mapper 代理开发使用Spring自动扫描MyBatista的接口并装配 --
39 !-- Spring 将指定包中所有的被Mapper注解标注的接口自动装配为MyBatatis的映射接口 --
40 bean class org.mybatis.spring.mapper.MapperScannerConfigurer
41 !-- MyBatis-spring组件的扫描器 --
42 property namebasePackage value com.mybatis.dao/
43 property namesqlSessionFactoryBeanName value sqlSessionFactory/
44 /bean
45
46 /beans 检查对应的依赖类配置文件路径能否Ctrl进去。MyBatis的核心文件和映射文件路径是否正确。以下是我的代码 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationmappers!-- 映射文件--mapper resource com/mybatis/dao/UserMapper.xml//mappers
/configuration ?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespace com.mybatis.dao.UserDao!-- 根据ID查询用户信息 --select idselectUserById parameterType Integer resultType com.mybatis.po.MyuserSELECT * FROM user WHERE uid #{uid}/select!-- select idselectAllUser resultType com.mybatis.po.MyuserSELECT * FROM user/select添加一个用户#{uname}为com.mybatis.po.MyUser属性值insert id addUser parameterType com.mybatis.po.MyuserINSERT INTO user (uname,usex) VALUES (#{uname},#{usex})/insert修改一个用户update idupdateUser parameterType com.mybatis.po.MyuserUPDATE user SET uname #{unmae},usex #{user} where uid #{uid}/update删除一个用户delete id delectUser parameterType IntegerDELECT from user WHERE uid #{uid}/delete --
/mapper 看Dao层的接口和Mapped的映射文件是否是在同一包下。类似问题的博客 https://blog.csdn.net/h363659487/article/details/74275972 https://blog.csdn.net/u012385190/article/details/53186552 嗯嗯第一次写这样的博客希望会对大家有帮助愿我们都被温柔以待2019.4.21。转载于:https://www.cnblogs.com/liruilong/p/10744962.html