mybatis配置的一个错误

Error creating bean with name 'xxxController’错误的一个解决思路

SpringBoot整合MyBatis时发生的一个错误,百度搜索这个错误地时候错误原因大多是使用mybatis-generator时导致Mapper.xml中出现相同的id,但是我的几条sql语句都是手打的,也检查确认不存在id相同的情况。

虽然这个错误原因跟我不相符,但是基本能确定就是Mapper文件中出现问题。某一次调试时出错信息出现了类似**“不允许有匹配 “[xX][mM][lL]” 的处理指令目标。”**这个错,搜了一下结果是配置文件中

<?xml version="1.0" encoding="UTF-8" ?>

必须是xml文件中的第一个元素且前面不能空格!

改完之后果然不报错了,配置文件复制粘贴的时候要注意这些细节啊!

报错参考:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\IDEAtest\springboot-data-mybatis\target\classes\com\joanna\springbootdatamybatis\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\IDEAtest\springboot-data-mybatis\target\classes\mybatis\mapper\UserMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.7.jar:5.3.7]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.7.jar:5.3.7]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MyBatis一个优秀的 ORM 框架,提供了许多方便的配置和使用方式。下面是一个简单的 MyBatis 配置文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/UserMapper.xml"/> </mappers> </configuration> ``` 其中,`environments` 元素定义了环境,可以包含多个环境,通过 `default` 属性指定默认环境。`environment` 元素定义了环境的具体配置,包括事务管理器和数据源。在上面的示例中,使用了 `JDBC` 类型的事务管理器和 `POOLED` 类型的数据源。 `mappers` 元素定义了映射器,可以包含多个映射器,通过 `resource` 属性指定映射器文件的路径。在上面的示例中,使用了 `com.example.mapper.UserMapper.xml` 文件作为映射器。 如果在配置文件中出现错误MyBatis 会在启动时抛出异常。常见的错误包括: - `org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration`:XML 文件格式错误或者 MyBatis 版本不兼容。 - `org.apache.ibatis.exceptions.PersistenceException: Error getting driver instance for jdbc driver`:数据库驱动类名错误或者数据库驱动未加载。 - `org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)`:映射器文件的路径错误或者映射器文件中的语句 ID 错误。 这些错误都可以通过仔细检查配置文件和相关代码来解决。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值