前言
声明:这个错误只是作者在学习时候的一种情况,并不一定能够解决所有情况。还望各位审视出错具体环境!谢谢。
今天使用mybatis逆向工程 springboot本想着少写点代码,但被个bug纠缠到死。bug主要信息为:
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
。。。
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]:xxxxxx
。。。
- 首先有一个其他原因就是版本不一致:mysql的jar包版本和mysql版本要一致。用的jar包不同出来的东西不同也会出错(亲测)
- 其次逆向每次生成要把上一次剩余的删除干净再生成,我每次剩部分就会报错
- 我出现这个问题的主要原因是注入失败,失败的原因是mapper.java和mapper.xml不一致,确切的来说是我没配置好使得mapper.xml多了mapper.java没有的内容。思路如下:
首先一点是mybatis逆向工程生成的Mapper.java要加@Mapper注解才行,但是加了注解还是会报类似的错,那么我根据我的节奏剖析原因。
- 我想使用逆向工程生成一些代码普通最后一项配置是这样的
<table tableName="campus" domainObjectName="campus"></table>
- 但是逆向工程生成example类我不想要。于是就改了一下
<!---->
<table tableName="campus"
domainObjectName="campus"
enableUpdateByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false"
enableCountByExample="false"></table>
本来以为完好,打开mapper.xml发现两者数量根本不匹配,在xml文件中重复了。
然后我再逆向的xml中添加一行selectByExampleQueryId="false"
这个怪我当时参数没写全
<table tableName="campus" domainObjectName="campus" selectByExampleQueryId="false"
enableUpdateByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false"
enableCountByExample="false"></table>
启动就没问题了
这个可能只是这种错误的一小部分,可能帮不到很多人!还有其他情况解决欢迎留言补充!
附带maven版本
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>volunteer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>volunteer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 如果对
后端、爬虫、数据结构算法
等感性趣欢迎关注我的个人公众号交流:bigsai