这里是Hibernate代码。
package net.javaguides.usermanagement.utl;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import net.javaguides.usermanagement.model.User;
/**
* Java based configuration
* @author ramesh Fadatare
*
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/demo?useSSL=false");
settings.put(Environment.USER, "root");
settings.put(Environment.PASS, "");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(User.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Java Config serviceRegistry created");
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
下面是servlet。
package net.javaguides.usermanagement.web;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.javaguides.usermanagement.dao.UserDao;
import net.javaguides.usermanagement.model.User;
/**
* ControllerServlet.java
* This servlet acts as a page controller for the application, handling all
* requests from the user.
* @email Ramesh Fadatare
*/
@WebServlet("/")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDao userDao;
public void init() {
userDao = new UserDao();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertUser(request, response);
break;
case "/delete":
deleteUser(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateUser(request, response);
break;
default:
listUser(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
private void listUser(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List < User > listUser = userDao.getAllUser();
request.setAttribute("listUser", listUser);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-list.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
dispatcher.forward(request, response);
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
User existingUser = userDao.getUser(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
request.setAttribute("user", existingUser);
dispatcher.forward(request, response);
}
private void insertUser(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String country = request.getParameter("country");
User newUser = new User(name, email, country);
userDao.saveUser(newUser);
response.sendRedirect("list");
}
private void updateUser(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String email = request.getParameter("email");
String country = request.getParameter("country");
User user = new User(id, name, email, country);
userDao.updateUser(user);
response.sendRedirect("list");
}
private void deleteUser(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userDao.deleteUser(id);
response.sendRedirect("list");
}
}
下面是我运行应用程序时的结果。
正如@AndrewJames所指出的,您需要下载JAXB-API库和一个实现,它们是Hibernate所需要的。
虽然您不使用Maven进行依赖关系管理,但您可以通过单击jar
链接下载相应的构件,您可以在构件页面中显示的HTML表的files
行中找到该链接。
您需要下载:
依赖项中已经有javax.activation。它是JAXB-API所必需的。
问题内容: 我有一些使用JAXB API类的代码,这些类作为Java 6/7/8中JDK的一部分提供。当我使用Java 9运行相同的代码时,在运行时出现错误,指示找不到JAXB类。 自Java 6以来,JAXB类已经作为JDK的一部分提供了,那么Java 9为什么不能再找到这些类? 问题答案: JAXB API被认为是Java EE API,因此不再包含在Java SE 9的默认类路径中。在Jav
安装JDK9后,我在运行Scala项目时得到了这个异常。将Scala升级到2.12.2也没有解决我的问题。
我有一些使用JAXB API类的代码,这些代码在Java 6/7/8中作为JDK的一部分提供。当我用Java 9运行相同的代码时,在运行时我得到错误,表明找不到JAXB类。 JAXB类从Java 6开始就作为JDK的一部分提供了,那么为什么Java 9再也找不到这些类了呢?
我试图从Java8迁移到Java11,但我得到了这个错误: 现在,我尝试修复将此依赖项添加到我的pom.xml文件中的问题: 但我总是遇到同样的问题。我还尝试了2.3.0版本。 我查看了我的有效POM,jaxb-api依赖项出现在那里。此外,我打开了maven下载的jar文件,只是为了验证类是否存在。
我有一些使用JAXB API类的代码。当我用Java11运行代码时,在运行时我会得到错误,指示“在执行时丢失了一个必需的类”。
安装android sdk工具时,会发出以下错误: JAVAlang.NoClassDefFoundError:javax/xml/bind/annotation/XmlSchema 为什么会发生这种情况?如何解决? 调试输出: