Spring整合MyBatis错误
找了好久并且百度了,按照别人博客的步骤检查了一边还是没发现问题。希望老师能帮忙看看,谢谢。要不然这个作业就做不动了。主要实现在category_add.jsp新建分类,然后新建成功后在category_list.jsp展示所有分类,能获取到category_add.jsp的传值,且能新建一个Category对象保存,就是保存不到数据库中。
Tomcat报错:
Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.imooc.hm.dao.CategoryDao.selectAll
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
at com.sun.proxy.$Proxy16.selectAll(Unknown Source)
at com.imooc.hm.service.Impl.CategoryServiceImpl.showCategory(CategoryServiceImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy18.showCategory(Unknown Source)
at com.imooc.hm.controller.CategoryController.list(CategoryController.java:21)
... 30 more
项目结构图:
CategoryDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.hm.dao.CategoryDao">
<resultMap id="resultMap" type="Category">
<id property="id" column="id" javaType="Integer"></id>
<result property="name" column="name" javaType="String"></result>
<result property="create_time" column="create_time" javaType="java.util.Date"></result>
<result property="update_time" column="update_time" javaType="java.util.Date"></result>
</resultMap>
<insert id="addCategory" parameterType="Category" useGeneratedKeys="true">
insert into category(name,create_time,update_time) values(#{name},#{create_time},#{update_time})
</insert>
<delete id="deleteCategory" parameterType="Integer">
delete from category where id=#{id}
</delete>
<select id="selectById" parameterType="Integer" resultMap="resultMap">
select * from category where id=#{id}
</select>
<select id="selectAll" resultMap="resultMap">
select * from category
</select>
</mapper>
CategoryDao.java
package com.imooc.hm.dao;
import com.imooc.hm.entity.Category;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("categoryDao")
public interface CategoryDao {
void addCategory(Category category);
void deleteCategory(Integer id);
Category selectById(Integer id);
List<Category> selectAll();
}
实体类 Category.java
package com.imooc.hm.entity;
import java.util.Date;
public class Category {
private Integer id;
private String name;
private Date create_time;
private Date update_time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
public Category() {
}
}
CategoryService.java
package com.imooc.hm.service;
import com.imooc.hm.entity.Category;
import java.util.List;
public interface CategoryService {
void newCategory(Category category);
void removeCategory(Integer id);
List<Category> showCategory();
}
CategoryServiceImpl.java
package com.imooc.hm.service.Impl;
import com.imooc.hm.dao.CategoryDao;
import com.imooc.hm.entity.Category;
import com.imooc.hm.service.CategoryService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
@Resource(name = "categoryDao")
private CategoryDao categoryDao;
public void newCategory(Category category) {
System.out.println("开始插入数据库");
categoryDao.addCategory(category);
}
public void removeCategory(Integer id) {
categoryDao.deleteCategory(id);
}
public List<Category> showCategory() {
return categoryDao.selectAll();
}
}
核心控制器 DispatchServlet.java
package com.imooc.hm.global;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DispatchServlet extends GenericServlet {
private ApplicationContext context;
public void init() throws ServletException {
super.init();
context = new ClassPathXmlApplicationContext("spring.xml");
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
/*
staff/add.do login.do
staffController
public void add(HttpServletRequest request, HttpServletResponse response){}
*
*/
String path = request.getServletPath().substring(1);
String beanName = null;
String methodName = null;
int index = path.indexOf('/');
if (index != -1) {
beanName = path.substring(0, index) + "Controller";
methodName = path.substring(index + 1, path.indexOf(".do"));
} else {
beanName = "selfController";
methodName = path.substring(0, path.indexOf(".do"));
}
Object obj = context.getBean(beanName);
try {
Method method = obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
//注意传入的参数request和response,不要都写成request
method.invoke(obj,request,response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
该模块对应的控制器 CategoryController.java
package com.imooc.hm.controller;
import com.imooc.hm.entity.Category;
import com.imooc.hm.service.CategoryService;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Controller("categoryController")
public class CategoryController {
@Resource(name = "categoryService")
private CategoryService categoryService;
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Category> list=categoryService.showCategory();
request.setAttribute("categories",list);
request.getRequestDispatcher("../category_list.jsp").forward(request,response);
}
public void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("../category_add.jsp").forward(request,response);
}
public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
Category category=new Category();
category.setName(name);
category.setCreate_time(new Date());
category.setUpdate_time(new Date());
System.out.println(category);
categoryService.newCategory(category);
response.sendRedirect("list.do");
}
}
相关依赖的注入;
数据库对应的表;
正在回答
有可能是同学创建resources文件夹下的文件路径的时候,用的是点进行分割的,导致其中这个不是好几个文件层级,而是这个文件夹的名字就是“com.imooc.sm.dao”了,导致和CategoryDao不在同一个文件路径下。可以重新创建一下resources文件夹下的目录结构,两种办法:
一是:一层一层的创建
二是:创建文件夹的时候,用/斜杠分割,不要用点分隔
如果没有创建错误,同学可以找一下target文件夹下,发布后的CategoryDao.class 应该和CategoryDao.xml 在一个文件夹下的。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星