报错:
```org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.nowcoder.toutiao.dao.NewsDAO.selectByUserIdAndOffset
at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:214) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:48) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) ~[mybatis-3.4.0.jar:3.4.0]
at com.sun.proxy.$Proxy60.selectByUserIdAndOffset(Unknown Source) ~[na:na]
at com.nowcoder.toutiao.servicr.NewsService.getLatestNews(NewsService.java:16) ~[classes/:na]
at com.nowcoder.toutiao.controller.HomeController.index(HomeController.java:30) ~[classes/:na]
其中 NewsService.java
package com.nowcoder.toutiao.servicr;
import com.nowcoder.toutiao.dao.NewsDAO;
import com.nowcoder.toutiao.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NewsService {
@Autowired
private NewsDAO newsDAO;
public List<News> getLatestNews(int userId,int offset,int limit){
return newsDAO.selectByUserIdAndOffset(userId,offset,limit);
}
}
HomeController代码:
package com.nowcoder.toutiao.controller;
import com.nowcoder.toutiao.dao.NewsDAO;
import com.nowcoder.toutiao.model.News;
import com.nowcoder.toutiao.model.ViewObject;
import com.nowcoder.toutiao.servicr.NewsService;
import com.nowcoder.toutiao.servicr.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
public class HomeController{
@Autowired
NewsService newsService;
@Autowired
UserService userService;
@RequestMapping(path={"/","/index"},method={RequestMethod.GET,RequestMethod.POST})
public String index(Model model){
List<News> newsList=newsService.getLatestNews(0,0,10);
List<ViewObject> vos=new ArrayList<>();
for (News news:newsList){
ViewObject vo=new ViewObject();
vo.set("news",news);//vo里面可以放任何东西
vo.set("user",userService.getUser(news.getUserId()));
vos.add(vo);
}
model.addAttribute("vos",vos);
return "home";
}
}
DAOMampper.xml文件出错了
NewsDAO.selectByUserIdAndOffset 这个方法里面的mapper映射sql语句有问题
:就是说,你的Mapper接口,被Spring注入后,却无法正常的使用mapper.xml的sql;
这里的Spring注入后的意思是,你的接口已经成功的被扫描到,但是当Spring尝试注入一个代理(MyBatista实现)的实现类后,却无法正常使用。这里的可能发生的情况有如下几种;
接口已经被扫描到,但是代理对象没有找到,即使尝试注入,也是注入一个错误的对象(可能就是null)
接口已经被扫描到,代理对象找到了,也注入到接口上了,但是调用某个具体方法时,却无法使用(可能别的方法是正常的)
当然,我们不好说是那种情况,毕竟报错的结果是一样的,这里就提供几种排查方法: