1. 程式人生 > >mybatis invalid bound statement (not found) 當心檔案確實不存在

mybatis invalid bound statement (not found) 當心檔案確實不存在

前言:本人在將不適用maven的專案轉化為maven專案後,遇到了 invalid bound statement (not found),百思不得其解,因為此前是可以執行的,而轉為maven專案後,可以正常執行maven clean install 命令打包釋出。但是訪問具體某個controller後就會報出  invalid bound statement (not found)。

解決過程:

一、先試了確定常見的幾個易錯點沒錯(網上到處都有的):

1.mapper.xml 檔案裡的namespace對應這mapper.java 的class型別名

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.surpass.logistics.car.dao.CarTypeMapper
" > <resultMap id="BaseResultMap" type="com.surpass.logistics.car.domain.CarType" > <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Sat Apr 09 11:06:46 CST 2016. --> <id column="id" property="id" jdbcType="VARCHAR" /> <result column="car_type" property="carType" jdbcType="VARCHAR" /> <result column="created_by" property="createdBy" jdbcType="VARCHAR" /> <result column="created_on" property="createdOn" jdbcType="TIMESTAMP" /> <result column="updated_by" property="updatedBy" jdbcType="VARCHAR" /> <result column="updated_on" property="updatedOn" jdbcType="TIMESTAMP" /> </resultMap>

package com.surpass.logistics.car.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.surpass.logistics.car.domain.CarType;
import com.surpass.logistics.car.domain.CarTypeExample;

public interface CarTypeMapper {
....
}

2.自己引入的型別必須要對(點了可以跳轉過去)
  <resultMap id="BaseResultMap" type="com.surpass.logistics.car.domain.CarType
" > <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Sat Apr 09 11:06:46 CST 2016. --> <id column="id" property="id" jdbcType="VARCHAR" /> <result column="car_type" property="carType" jdbcType="VARCHAR" /> <result column="created_by" property="createdBy" jdbcType="VARCHAR" /> <result column="created_on" property="createdOn" jdbcType="TIMESTAMP" /> <result column="updated_by" property="updatedBy" jdbcType="VARCHAR" /> <result column="updated_on" property="updatedOn" jdbcType="TIMESTAMP" /> </resultMap>
二、窮途末路,猜想引用路徑的檔案是否確實不存在(格外注意各種工具的打包特色)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
		<property name="mapperLocations" value="classpath:com/surpass/logistics/**/dao/*.xml" />
</bean>
於是到class資料夾下尋找
com/surpass/logistics/car/dao/CarTypeMapper.xml
發現src/main中的所有資原始檔都沒有被打包進來,查詢相關資料發現maven是根據以下片段來打包資源的
<build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>

發現資原始檔的目錄不包含src//main/java

於是改為

<resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
        </resources>
錯誤消失。