一个软件app/seo关键词推广公司
解决mybatis报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
- 检查mapper.xml文件是否在工程中生效
- 检查dao层接口定义是否正确
- 检查xml中
mapper
标签的namespace
配置是否正确 - 检查xml中语句配置是否正确
1. 检查mapper.xml文件是否在工程中生效
Mybatis的mapper.xml文件生效是由mapperLocations
加载的
SqlSessionFactoryBean.setMapperLocations(Resource... mapperLocations)
mapperLocations
是Spring的资源文件加载方式,可以选择正则匹配的方式查找配置路径的xml文件
Resource mapperLocations= new PathMatchingResourcePatternResolver().getResources("classpath*:sqlmap/*-mapper.xml");
如上规规,你的mybatis的xml文件必须要放在sqlmap
文件夹下且以-mapper.xml
后缀结尾。
2. 检查dao层接口定义是否正确
标准的Dao层接口定义如下:
@Mapper
public interface BookDao {String getBookId(@Param("name") String bookName);List<Book> getBooks(@Param("bookIds") List<String> bookIds);int insertBook(@Param("books") List<Book> books);
}
3. 检查xml中mapper
标签的namespace
配置是否正确
xml文件中的mapper
标签的namespace
与类名是否关联?这里要考虑有没有配置别名,默认都是用全类名唯一性来关联
<?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="org.numb.dao.BookDao">
</mapper>
4. 检查xml中语句配置是否正确
<?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="org.numb.dao.BookDao"><resultMap id="AllColumnMap" type="org.numb.model.Book"><result column="id" jdbcType="VARCHAR" property="id"/><result column="name" jdbcType="VARCHAR" property="bookName"/><result column="price" jdbcType="DECIMAL" property="price"/></resultMap><sql id="all_column">`id`,`name``price`</sql><select id="getBookId" resultType="string">SELECT id FROM book WHERE name = #{name}</select><select id="getBooks" resultMap="AllColumnMap">SELECT * FROM book<where><foreach collection="bookIds" item="id" index="index" separator=","open="id in (" close=")" nullable="true">#{id}</foreach></where></select><insert id="insertBook">INSERT INTO book(<include refid="all_column"/>) VALUES<foreach collections="books" item="book" index="index" separator="," open="(" close=")">#{book}</foreach></insert>
</mapper>