着急!!!找了好久没看到问题,
报错
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);*/
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星