欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis初始配置及错误说明

程序员文章站 2022-04-30 22:25:32
1. mybatis使用现在进行了简化,可以直接只引入如下pom就可以了 2. springboot配置时进行如下配置 启动的application添加注解,如下 启动的application.properties添加配置,如下 3. 启动的报错记录说明 如果没有配置这个或者配置错误,@Mapper ......

1. mybatis使用现在进行了简化,可以直接只引入如下pom就可以了

<dependency>
    <groupid>tk.mybatis</groupid>
    <artifactid>mapper-spring-boot-starter</artifactid>
    <version>1.1.0</version>
</dependency>

2. springboot配置时进行如下配置

启动的application添加注解,如下

@springbootapplication
@mapperscan(basepackages = "com.example.demo.dao")
public class demoapplication {

    public static void main(string[] args) {
        springapplication.run(demoapplication.class, args);
    }
}

启动的application.properties添加配置,如下

mybatis.mapper-locations=classpath:com/example/demo/mapper/xml/*mapper.xml

3. 启动的报错记录说明

如果没有配置这个或者配置错误,@mapperscan(basepackages = "com.example.demo.dao"),会有如下报错,因为没有创建usermapper的实例导致注入失败

description:

field usermapper in com.example.demo.web.usercontroller required a bean of type 'com.example.demo.mapper.usermapper' that could not be found.

the injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.autowired(required=true)

如果application.properties没有正确配置mapper文件的位置,在访问到具体的mapper实现时会有如下报错

org.apache.ibatis.binding.bindingexception: invalid bound statement (not found): com.example.demo.mapper.usermapper.selectbyprimarykey
    at org.apache.ibatis.binding.mappermethod$sqlcommand.<init>(mappermethod.java:214) ~[mybatis-3.4.0.jar:3.4.0]
    at org.apache.ibatis.binding.mappermethod.<init>(mappermethod.java:48) ~[mybatis-3.4.0.jar:3.4.0]
    at org.apache.ibatis.binding.mapperproxy.cachedmappermethod(mapperproxy.java:59) ~[mybatis-3.4.0.jar:3.4.0]
    at org.apache.ibatis.binding.mapperproxy.invoke(mapperproxy.java:52) ~[mybatis-3.4.0.jar:3.4.0]
    at com.sun.proxy.$proxy56.selectbyprimarykey(unknown source) ~[na:na]
    at com.example.demo.web.usercontroller.selectuserbyid(usercontroller.java:23) ~[classes/:na]
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_121]
    at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.method.invoke(method.java:498) ~[na:1.8.0_121]