springboot整合My_batis 排錯

MICI 1,262 2022-05-25
  • 檢查是否在啟動類配置掃包;
  • 檢查名稱空間是否對應;
  • 是否使用註解,而卻在配置文件中配置成配置文件的方式,把sql寫在了配置文件裡,不去註解寫sql;
  • 是否導入了兩個必要的依賴;
  • 是否導入了連接池;
  • 是否導入了JDBC;
  • 是否引入了MSQL;
  • 如果使用配置文件寫sql必須在配置文件包創建一個和mapper同級的目錄,將他放在那裡面,或者你直接放在mapper也行;
  • 表名在sql裡,別亂想;數據庫名在配置裡,太久沒設置忘完了?
  • 使用配置文件書寫sql的時候如果是springboot需要在配置文件裡如下配置;
    在這裡插入圖片描述
    spring的話在mybatis的配置文件裡指定就好了;
  • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3] with root cause
    這個是因為進行查詢有多個結果,而我們返回的結果接收並不是用集合或者數組進行接收的,而是使用單個的對象進行接收,所以報錯;
    在這裡插入圖片描述
  • mapper文件中
<mapper namespace="com.itheima.mapper.testMapper" >

必須指定對應的mapper代理接口全路徑;

 <select id="findPojo" resultType="com.itheima.pojo.TestPojo">
        SELECT * FROM zxc
    </select>

<mapper namespace="com.itheima.mapper.testMapper" >

必須指定對應的mapper代理接口全路徑;

 <select id="findPojo" resultType="com.itheima.pojo.TestPojo">
        SELECT * FROM zxc
    </select>

id對應mapper中的方法;執行這次查詢的方法


# Spring