着急!!!找了好久没看到问题,

着急!!!找了好久没看到问题,

报错

java.io.IOException: Could not find resource /mybatis.xml

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)

at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:160)


持久层

package com.liujia.cake.dao;

import com.liujia.cake.entity.Catalog;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DatalogDao {


   @Insert("<script>" +
           "insert into catalog(title,pid,info) values"+
           "<foreach collection='list' item='catalog' separator=','  >"+
           "(#{catalog.title},#{catalog.pid},#{catalog.values})"+
           "</foreach>"+
           "</script>"
           )
   @Options(useGeneratedKeys = true,keyProperty = "id")
   void batchInsert(List<Catalog> list);

   @Delete("delete from catalog where id=#{id}")
   void delete(int id);

   @Select("select * from catalog where id=#{id}")
   @Results(id="all",value = {
           @Result(column = "id",property = "id" ,id=true),
           @Result(column = "title",property = "title"),
           @Result(column = "pid",property = "pid"),
           @Result(column = "info",property = "info"),
           @Result(column = "id",property = "children",many=@Many(select="selectByPid"))
   })
   Catalog select(int id);

   @Select("select * from catalog where pid=#{pid}")
   @ResultMap("all")
   List<Catalog> selectByPid(int pid);
}


业务层

package com.liujia.cake.biz;

import com.liujia.cake.entity.Catalog;

import java.util.List;

public interface CatalogBiz {
   void add(List<Catalog> list);
   void remove(int id);
   Catalog getRoot();
}


package com.liujia.cake.biz;

import com.liujia.cake.dao.DatalogDao;
import com.liujia.cake.entity.Catalog;
import com.liujia.cake.global.DaoFactory;

import java.io.IOException;
import java.util.List;

public class CatalogBizImpl implements CatalogBiz {
   private DatalogDao datalogDao= DaoFactory.getInstance().getDao(DatalogDao.class);

   public CatalogBizImpl() throws IOException {
   }


   public void add(List<Catalog> list) {
       datalogDao.batchInsert(list);
   }

   public void remove(int id) {
       datalogDao.delete(id);
   }

   public Catalog getRoot() {
       return datalogDao.select(1000);
   }
}


控制器

package com.liujia.cake.controller;

import com.liujia.cake.biz.CatalogBiz;
import com.liujia.cake.biz.CatalogBizImpl;
import com.liujia.cake.entity.Catalog;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CatalogController {
   private CatalogBiz catalogBiz=new CatalogBizImpl();

   public CatalogController() throws IOException {
   }


   //  /admin/Catalog/list.do
   public void  list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       Catalog root=catalogBiz.getRoot();
       request.setAttribute("root",root);
       System.out.println(root);
       request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_list.jsp").forward(request,response);
   }
   //  /admin/Catalog/toAdd.do
   public void  toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       Catalog root=catalogBiz.getRoot();
       request.setAttribute("root",root);
       request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_add.jsp").forward(request,response);
   }

   //  /admin/Catalog/add.do
   public void  add(HttpServletRequest request, HttpServletResponse response) throws IOException {
       String[] titles=request.getParameterValues("title");
       String[] pids=request.getParameterValues("pid");
       String[] infors=request.getParameterValues("infor");
       List<Catalog> list=new ArrayList<Catalog>();
       for(int i=0;i<titles.length;i++){
           Catalog catalog=new Catalog();
           catalog.setTitle(titles[i]);
           catalog.setPid(Integer.parseInt(pids[i]));
           catalog.setInfo(infors[i]);
           list.add(catalog);
       }
       catalogBiz.add(list);
       response.sendRedirect("list.do");
   }
   //  /admin/Catalog/remove.do
   public void  remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
       int id=Integer.parseInt(request.getParameter("id"));
       catalogBiz.remove(id);
       response.sendRedirect("list.do");
   }

}


package com.liujia.cake.global;

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 GlobalController extends GenericServlet {
   public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

       HttpServletRequest request=(HttpServletRequest) servletRequest;
       HttpServletResponse response=(HttpServletResponse) servletResponse;
       String path=request.getServletPath();

       if(path.indexOf("/admin")!=-1){
           path=path.substring(7);
       }else{
           path=path.substring(1);
       }

       int index=path.indexOf("/");
       String className=null;
       String  methodName=null;
       if (index!=-1){
           className="com.liujia.cake.controller."+path.substring(0,index)+"Controller";
           methodName=path.substring(index+1,path.indexOf(".do"));
       }else {
           className="com.liujia.cake.controller.DefaultController";
           methodName=path.substring(0,path.indexOf(".do"));
       }
       System.out.println(className+"-----"+methodName);
       try {
           Class cl=Class.forName(className);
           Object object=cl.newInstance();
           Method method= cl.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
           method.invoke(object,request,response);
           } catch (InvocationTargetException e) {
               e.printStackTrace();
           } catch (InstantiationException e) {
               e.printStackTrace();
           } catch (IllegalAccessException e) {
               e.printStackTrace();
           } catch (NoSuchMethodException e) {
               e.printStackTrace();
           } catch (ClassNotFoundException e) {
            e.printStackTrace();
       }
   }
}


实体类

package com.liujia.cake.entity;

import java.util.ArrayList;
import java.util.List;

public class Catalog {
   private  int id;
   private  String title;
   private  int pid;
   private  String info;
   private  List<Catalog> children = new ArrayList<Catalog>();

   public int getId() {
       return id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public int getPid() {
       return pid;
   }

   public void setPid(int pid) {
       this.pid = pid;
   }

   public String getInfo() {
       return info;
   }

   public void setInfo(String info) {
       this.info = info;
   }

   public List<Catalog> getChildren() {
       return children;
   }

   public void setChildren(List<Catalog> children) {
       this.children = children;
   }

   public Catalog(int id, String title, int pid, String info, List<Catalog> children) {
       this.id = id;
       this.title = title;
       this.pid = pid;
       this.info = info;
       this.children = children;
   }
   public Catalog() {
       this.id = id;
       this.title = title;
       this.pid = pid;
       this.info = info;
       this.children = children;
   }
}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
       <environments default="dev">
           <environment id="dev">
               <transactionManager type="JDBC"></transactionManager>
               <dataSource type="POOLED">
                   <property name="driver" value="com.mysql.jdbc.Driver"/>
                   <property name="url" value="jdbc:mysql://localhost:3306/icake"/>
                   <property name="username" value="root"/>
                   <property name="password" value="259883"/>
               </dataSource>
           </environment>
       </environments>
       <mappers>
           <package name="com.liujia.cake.dao"/>
       </mappers>
</configuration>



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.practive</groupId>
   <artifactId>imoocCake</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>war</packaging>
   <repositories>
       <repository>
           <id>aliyun</id>
           <name>aliyun</name>
           <url>https://maven.aliyun.com/repository/public</url>
       </repository>
   </repositories>
   <build>
       <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
       </resources>
   </build>
   <dependencies>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.40</version>
       </dependency>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.5.5</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>4.0.0</version>
       </dependency>
       <dependency>
           <groupId>jstl</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.10</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-site-plugin</artifactId>
           <version>3.8.2</version>
       </dependency>
   </dependencies>


</project>

package com.liujia.test;

import com.liujia.cake.dao.DatalogDao;
import com.liujia.cake.entity.Catalog;
import com.liujia.cake.global.DaoFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class TestDemo {
   @Test
   public void TestDemo() throws IOException {
       DatalogDao datalogDao= DaoFactory.getInstance().getDao(DatalogDao.class);
       Catalog catalog= datalogDao.select(10000);
       System.out.println(catalog);
/*        SqlSessionFactory sqlSessionFactory= (SqlSessionFactory) DaoFactory.getInstance().getDao(DatalogDao.class);
       System.out.println(sqlSessionFactory);*/
   }
}

http://img1.sycdn.imooc.com//climg/5f5f341d09298eb004010594.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕阿慧 2020-09-14 19:29:30

同学你好,MyBatis底层默认去target->classes找配置文件,如下:

http://img1.sycdn.imooc.com//climg/5f5f541609a397a406150177.jpg

所以可以不加/,也可以使用./mybatis.xml。如果指定/,就不会去classes目录下找,所以会报找不到文件错误。

祝学习愉快~

好帮手慕阿慧 2020-09-14 18:32:25

同学你好,获得org.apache.ibatis.binding.MapperProxy@7c5fa8a1是正常的。持久层获取结果为null,可能是catalog表中没有id为10000的数据。建议同学传入catalog表中存在的id值。

例如,catalog表中数据如下:

http://img1.sycdn.imooc.com//climg/5f5f463e09948a8e02750144.jpg

测试及运行结果如下:

http://img1.sycdn.imooc.com//climg/5f5f46b3093e220c07570280.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

好帮手慕阿慧 2020-09-14 18:01:40

同学你好,建议同学将/mybatis.xml改为./mybatis.xml试试,如下:

http://img1.sycdn.imooc.com//climg/5f5f3f7f09bc481c09330317.jpg

祝学习愉快~

  • 提问者 大圣归来² #1
    dao工厂获取:org.apache.ibatis.binding.MapperProxy@7c5fa8a1,这样算正常吗?就是持久层获取结果为null
    2020-09-14 18:06:17
  • 提问者 大圣归来² #2
    去掉“/”或者加上“.”就正常,为什么?
    2020-09-14 18:37:51
  • 提问者 大圣归来² #3
    业务层出现问题了。获取root为空 public Catalog getRoot(){ Catalog catalog=(Catalog)datalogDao.select(1000); System.out.println("dao工程:"+datalogDao); System.out.println("业务层处理返回结果"+catalog); return catalog; // return datalogDao.select(1000); }
    2020-09-14 18:49:11
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师