Mybatis的配置方法可以见以下链接

这里说明一下确定配置正确的情况下出现“Invalid bound statement (not found)”问题。

问题描述:

MemberDAO.java文件内容如下

package com.gszh.wmcp.project.weixinweb.dao;

import com.gszh.wmcp.project.weixin.model.Member;

public interface MemberDAO {

public Member getMember(String Openid);
}

package com.gszh.wmcp.project.weixinweb.dao;

import com.gszh.wmcp.project.weixin.model.Member;

public interface MemberDAO {

public Member getMember(String Openid);
}

MemberDAOMapper.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="com.gszh.wmcp.project.weixinweb.dao.MemberDAO">

<select id="getMember" resultType="com.gszh.wmcp.project.weixin.model.Member">
SELECT * FROM t_member
where Openid=#{Openid} limit 1
</select>
</mapper>
<?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.gszh.wmcp.project.weixinweb.dao.MemberDAO">

<select id="getMember" resultType="com.gszh.wmcp.project.weixin.model.Member">
SELECT * FROM t_member
where Openid=#{Openid} limit 1
</select>
</mapper>

调用方法的内容如下:

@Autowired
private MemberDAO memberDAO;

@RequestMapping(value = "/AddMember", method = RequestMethod.POST)
@ResponseBody
public ResultMsg AddMember() {
if (memberDAO.getMember("123") == null) {
// memberDAO.addMember(member);
} else {
// memberDAO.updateMember(member);
}
return resultMsg;
}
@Autowired
private MemberDAO memberDAO;

@RequestMapping(value = "/AddMember", method = RequestMethod.POST)
@ResponseBody
public ResultMsg AddMember() {
if (memberDAO.getMember("123") == null) {
// memberDAO.addMember(member);
} else {
// memberDAO.updateMember(member);
}
return resultMsg;
}

以上配置正确,没有问题。但是系统报错

Invalid bound statement (not found): com.gszh.wmcp.project.weixinweb.dao.MemberDAO.getMember

提示说明没有正确找到方法getMember的xml映射。

反复排查和上网搜索了很久,还是没有找到真正原因。这个问题困扰了整整3天。最后解决了,不得不记录一下,免的下次继续犯错。。

解决方案:

在另一个DAO.java文件中发现了问题,此DAO包含了5个接口方法,但对应的Mapper.xml文件却只有前2个。

package com.gszh.wmcp.project.weixinweb.dao;

import java.util.List;

import com.gszh.wmcp.project.weixin.model.BillSend;

public interface WXwebDAO {

// 获取账单列表
public List<BillSend> getBillSendList();

// 获取指定用户账单列表
public List<BillSend> getBillSendListByArchID(List<String> ids);

// 获取首页列表
public List<WebSlide> getWebSlides(String GroupName);

// 获取页面数据
public WebPage getWebPages(@Param("PageCode") String PageCode);

// 获取文章列表
public List<WebArticle> getArticles(@Param("ClassName") String ClassName);

}

package com.gszh.wmcp.project.weixinweb.dao;

import java.util.List;

import com.gszh.wmcp.project.weixin.model.BillSend;

public interface WXwebDAO {

// 获取账单列表
public List<BillSend> getBillSendList();

// 获取指定用户账单列表
public List<BillSend> getBillSendListByArchID(List<String> ids);

// 获取首页列表
public List<WebSlide> getWebSlides(String GroupName);

// 获取页面数据
public WebPage getWebPages(@Param("PageCode") String PageCode);

// 获取文章列表
public List<WebArticle> getArticles(@Param("ClassName") String ClassName);

}

WXwebDAOMapper.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="com.gszh.wmcp.project.weixinweb.dao.WXwebDAO">
<!-- 微信缴费相关字段信息 -->
<resultMap type="com.gszh.wmcp.project.weixin.model.BillSend" id="BillSend">
<result column="Bill_ID" jdbcType="INTEGER" property="BillID"/>
<result column="Bill_ArchNo" jdbcType="VARCHAR" property="BillArchNo"/>
<result column="Bill_UserName" jdbcType="VARCHAR" property="BillUserName"/>
<result column="Bll_WeixinNo" jdbcType="VARCHAR" property="BllWeixinNo"/>
<result column="Bill_Address" jdbcType="VARCHAR" property="BillAddress"/>
<result column="Bill_Arrears_Count" jdbcType="VARCHAR" property="BillArrearsCount"/>
<result column="Bill_Arrears_fee" jdbcType="VARCHAR" property="BillArrearsfee"/>
<result column="Bill_AddDate" jdbcType="VARCHAR" property="BillAddDate"/>
<result column="Bill_IsSend" jdbcType="VARCHAR" property="BillIsSend"/>
<result column="Bill_Month" jdbcType="VARCHAR" property="BillMonth"/>
</resultMap>

<!-- 查询微信库账单列表 2019-07-29 create -->
<select id="getBillSendList" resultMap="BillSend" parameterType = "map">
SELECT tbs.Bill_UserName, tbs.Bill_ArchNo, tbs.Bll_WeixinNo, tbs.Bill_Address,
tbs.Bill_Arrears_Count, tbs.Bill_Arrears_fee, tbs.Bill_AddDate,
tbs.Bill_IsSend, tbs.Bill_Month,tbs.Bill_ID
FROM T_BillSend tbs
WHERE tbs.Bill_IsSend='0'
</select>

<!-- 查询微信库账单列表 2019-08-13 create -->
<select id="getBillSendListByArchID" resultMap="BillSend">
SELECT tbs.Bill_UserName, tbs.Bill_ArchNo, tbs.Bll_WeixinNo, tbs.Bill_Address,
tbs.Bill_Arrears_Count, tbs.Bill_Arrears_fee, tbs.Bill_AddDate,
tbs.Bill_IsSend, tbs.Bill_Month,tbs.Bill_ID
FROM T_BillSend tbs
WHERE tbs.Bill_IsSend='0' and tbs.Bill_ArchNo in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>
<?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.gszh.wmcp.project.weixinweb.dao.WXwebDAO">
<!-- 微信缴费相关字段信息 -->
<resultMap type="com.gszh.wmcp.project.weixin.model.BillSend" id="BillSend">
<result column="Bill_ID" jdbcType="INTEGER" property="BillID"/>
<result column="Bill_ArchNo" jdbcType="VARCHAR" property="BillArchNo"/>
<result column="Bill_UserName" jdbcType="VARCHAR" property="BillUserName"/>
<result column="Bll_WeixinNo" jdbcType="VARCHAR" property="BllWeixinNo"/>
<result column="Bill_Address" jdbcType="VARCHAR" property="BillAddress"/>
<result column="Bill_Arrears_Count" jdbcType="VARCHAR" property="BillArrearsCount"/>
<result column="Bill_Arrears_fee" jdbcType="VARCHAR" property="BillArrearsfee"/>
<result column="Bill_AddDate" jdbcType="VARCHAR" property="BillAddDate"/>
<result column="Bill_IsSend" jdbcType="VARCHAR" property="BillIsSend"/>
<result column="Bill_Month" jdbcType="VARCHAR" property="BillMonth"/>
</resultMap>

<!-- 查询微信库账单列表 2019-07-29 create -->
<select id="getBillSendList" resultMap="BillSend" parameterType = "map">
SELECT tbs.Bill_UserName, tbs.Bill_ArchNo, tbs.Bll_WeixinNo, tbs.Bill_Address,
tbs.Bill_Arrears_Count, tbs.Bill_Arrears_fee, tbs.Bill_AddDate,
tbs.Bill_IsSend, tbs.Bill_Month,tbs.Bill_ID
FROM T_BillSend tbs
WHERE tbs.Bill_IsSend='0'
</select>

<!-- 查询微信库账单列表 2019-08-13 create -->
<select id="getBillSendListByArchID" resultMap="BillSend">
SELECT tbs.Bill_UserName, tbs.Bill_ArchNo, tbs.Bll_WeixinNo, tbs.Bill_Address,
tbs.Bill_Arrears_Count, tbs.Bill_Arrears_fee, tbs.Bill_AddDate,
tbs.Bill_IsSend, tbs.Bill_Month,tbs.Bill_ID
FROM T_BillSend tbs
WHERE tbs.Bill_IsSend='0' and tbs.Bill_ArchNo in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>

某个DAO和Mapper.xml文件没有一一对应,导致影响了其他mapper的正常映射,但系统的报错并不能定位到出错的mapper文件,还需自己去一一检查,才能找到问题所在。这个漏写的xml方法,应该是我当时没有完成的工作,隔几天再打开的时候发现运行报错,走了不少弯路。谨记。