服务器:
package servlet.Implement_the_class.Main_interface;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import servlet.mapper.Account;
import servlet.mapper.UserMapper;
import servlet.pojo.Account_and_Password_class;
import servlet.pojo.Email_and_Password_class;
import java.io.IOException;
import java.io.InputStream;
@WebServlet("/demo11")
public class Register extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.delete(3);
// 设置响应内容类型
resp.setContentType("text/plain;charset=UTF-8");
// 响应客户端,通知登录成功
resp.getWriter().write("注册成功!");
// 可以根据需要返回更多信息,例如 JSON 数据等
}
}
服务器日志报错:
09-Jul-2024 11:14:25.025 严重 [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke 在路径为[/java_web_final2_war_exploded]的上下文中,Servlet[servlet.Implement_the_class.Main_interface.Register]的Servlet.service()引发了具有根本原因的异常[Servlet执行抛出一个异常]
java.lang.ClassNotFoundException: org.apache.ibatis.io.Resources
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1014)
at servlet.Implement_the_class.Main_interface.Register.service(Register.java:25)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:716)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:130)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:109)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:663)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:387)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:904)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1746)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1148)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
at java.base/java.lang.Thread.run(Thread.java:842)
然后下面是我的一个正常类能执行mybatis:
package servlet.mybatis_test;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import servlet.mapper.UserMapper;
import servlet.pojo.testdemo1;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*mybatis 增删改查*/
public class testdemo4 {
public static void main(String[] args) throws IOException {
//1.加载mybatis的核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行sql(可以在这里设置自动提交事务)
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3.获取UserMapper接口的代理对象
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//查看所有
List<testdemo1> users = usermapper.selectAll();
System.out.println("这是查询所有:");
System.out.println(users);
System.out.println();
//查看单独
int ids = 1;
testdemo1 selectById = usermapper.selectById(ids);
System.out.println("这是查询单独:");
System.out.println(selectById);
System.out.println();
//条件查询:散装
int id2 = 3;
String username = "王";
String password = "412";
//处理参数(因为username是模糊查询)
username = "%" + username + "%";
List<testdemo1> selectmany = usermapper.selectmany(id2, username, password);
System.out.println("这是多条件和模糊查找");
System.out.println(selectmany);
System.out.println();
//动态查询:
int id3 = 3;
String username3 = "";
String password3 = null;
//处理参数(因为username是模糊查询)
username3 = "%" + username3 + "%";
List<testdemo1> selectmany_dynamic = usermapper.selectmany_dynamic(id3, username3, password3);
System.out.println("动态查询");
System.out.println(selectmany_dynamic);
System.out.println();
//单条件动态查询:(只要有一个匹配了东西下面的就不会做了)
//这个的用处是一个下拉项可能有几个固定选项,从固定选项里面选
int id4 = 1;
String username4 = "cascaca";
String password4 = "djijdadqajdif";
//处理参数(因为username是模糊查询)
username4 = "%" + username4 + "%";
List<testdemo1> single_condition_dynamic = usermapper.Single_condition_dynamic(id4, username4, password4);
System.out.println("单条件动态查询");
System.out.println(single_condition_dynamic);
System.out.println();
//添加
String username5 = "赵六";
String password5 = "23223232";
String gender = "男";
String addr = "广东";
testdemo1 testdemo = new testdemo1();
testdemo.setAddr(addr);
testdemo.setGender(gender);
testdemo.setUsername(username5);
testdemo.setPassword(password5);
System.out.println("新增数据:");
usermapper.add(testdemo);
//获取主键必须要给useGeneratedKeys="true" keyProperty="id"
Integer id = testdemo.getId();
System.out.println(id);
/*提交事务:没有的话数据库不会出来新增的数据*/
/*sqlSession.commit();*/ /*上面开启了自动提交事务所以就不用再写了*/
/*修改*/
int id6=5;
String username6 = "七七";
String password6 = "52253";
String gender6 = "女";
String addr6 = "重庆";
testdemo1 demo2 = new testdemo1();
demo2.setAddr(addr6);
demo2.setGender(gender6);
demo2.setUsername(username6);
demo2.setPassword(password6);
demo2.setId(id6);
System.out.println("修改成功,受影响的行数:");
int count = usermapper.update(demo2);
System.out.println(count);
/*删除*/
int id7=7;
System.out.println("删除成功");
usermapper.delete(id7);
//批量删除
/*int[] ids_={6,8};
System.out.println("批量删除成功");
usermapper.batch_delete(ids_);*/
//4.释放资源
sqlSession.close();
}
}
我所用的服务器是tomcat,现在请帮我找到为什么服务器会报错的原因