排坑记录

今天使用SpringBoot整合MyBatis-plus时遇到了报错。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'indexController': Unsatisfied dependency expressed through
field 'personService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'personService': Unsatisfied dependency expressed through field
'personMapper'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'personMapper' defined in file [F:\IDEA\work\SpringBoot\boot-
webdemo\target\classes\com\tang\dao\PersonMapper.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
[com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.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 [F:\IDEA\work\SpringBoot\boot-
webdemo\target\classes\mapper\PersonMapper.xml]'; nested exception is
org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location
is 'file [F:\IDEA\work\SpringBoot\boot-
webdemo\target\classes\mapper\PersonMapper.xml]'. Cause:
org.apache.ibatis.builder.BuilderException: Error resolving class. Cause:
org.apache.ibatis.type.TypeException: Could not resolve type alias 'Person'. Cause:
java.lang.ClassNotFoundException: Cannot find class: Person

我看了下说是Mapper没有注入,然后我就奇了怪了,这注解不都标明了吗,然后我就找了一圈发现了问题,MyBatis-Plus不会识别我们的MyBatis别名配置,也就是Mapper.xml的返回类型包别名配置失效了,所以我们sql查询的返回类型不明,导致注入失败,这里我们只能使用全路径标明pojo类的返回类型,以前的写法要改一改。

注意事项

  • 我们创建的实体类要对应数据库,名称最好一致,如果表名发生更改,我们可以使用**@TableName(“···”)**注解,声明正确表名

  • 我们pojo类的字段要和数据库的元素一一对应,因为MyBatis-Plus都是默认全部扫描的,那么我们要是定义了非数据库字段,则要使用注解**@TableField(exist = false)**,声明当前字段并不存在于数据库中

  • dao:BaseMapper

  • service接口:IService

  • service实现类:ServiceImpl<M, T>

    M代表对应的Mapper,T就是数据库对应的pojo