zoukankan      html  css  js  c++  java
  • mybatis 接口和.xml配置小点

    新公司入职后很久没写博客了,慢慢拾起来吧。

    现象描述:

    项目本地运行没有问题,打包也没有报错,正常打包。但是部署到服务器在调用查库接口时,报错:Invalid bound statement (not found)。。。。。。。

    该错的意思就是调用时没有找到对应的xml文件。

    排错思路:

    mybatis 涉及的配置主要有两个,一是接口要生成bean【用户扫描mapper接口的也就是dao类】,二是xml 要告诉spring能找的到【用于扫描mapper.xml】。

    1 接口生成bean:

    方法一:在每一个接口上添加注解@Mapper,就好比这种:

     1 package com.brave.solid.domain.mapper;
     2 
     3 import com.brave.solid.domain.entity.User;
     4 import org.apache.ibatis.annotations.Mapper;
     5 
     6 import java.util.List;
     7 
     8 @Mapper
     9 public interface UserMapper {
    10     /**
    11      * This method was generated by MyBatis Generator.
    12      * This method corresponds to the database table user
    13      *
    14      * @mbggenerated
    15      */
    16     int deleteByPrimaryKey(Integer id);
    17 }

    方法二:在启动类上添加注解@MapperScan(),就好比这样:

     1 package com.brave.solid;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 @MapperScan("com.brave.solid.domain.mapper")
     9 public class SolidApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(SolidApplication.class, args);
    13     }
    14 
    15 }

    这两个注解有一个就好了,@Mapper需要每个接口上都添加,比较麻烦,所以启动类上添加@MapperScan比较方便,而且@MapperScan参数支持这样子:@MapperScan({"com.kfit.demo","com.kfit.user"})  多个包下的话。

    2 保证 xml需要被扫描到:

    则需要在配置文件里:

     注意的是路径千万不能弄错,我当时的坑就是这个路径写错,导致能打包,不能扫描到。

    总结:

      如果是以上的俩注解没有【有其中一个就好】,应该编译,打包就会报错。

    如果是mybatis的mapper-localtions没有配置对的话,能打包,不能扫描到。但是还有一种解决办法,就是mapper的文件路径和接口的包路径改成一致,这样生成的target里可以看到Mapper接口和XML文件在同一个文件夹下,这样子也是可以扫描到XML文件的,这种方式一般不推荐。

  • 相关阅读:
    进制
    流程控制
    运算符
    格式化输出
    数据结构-树的遍历
    A1004 Counting Leaves (30分)
    A1106 Lowest Price in Supply Chain (25分)
    A1094 The Largest Generation (25分)
    A1090 Highest Price in Supply Chain (25分)
    A1079 Total Sales of Supply Chain (25分)
  • 原文地址:https://www.cnblogs.com/junbaba/p/13583547.html
Copyright © 2011-2022 走看看