当我运行该应用程序时,我的数据没有被放入我的数据库中,我不知道出了什么问题,我会寻求一些帮助。这是我的休眠工具:
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/testdb");
settings.put(Environment.USER, "root");
settings.put(Environment.PASS, "theworldismine");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");
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;
}
这是我的用户控制器:
public class UserController {
private userDao userdao;
public void init() {
userdao = new userDao();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
register(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/registration.jsp").forward(request, response);
}
private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setUsername(username);
user.setPassword(password);
userdao.saveUser(user);
RequestDispatcher dispatcher = request.getRequestDispatcher("register-success.jsp");
dispatcher.forward(request, response);
}
这是我的userDao:
public class userDao {
public void saveUser(User user){
Transaction tx= null;
try{
Session session= HibernateUtil.getSessionFactory().openSession();
session.save(user);
}catch(Exception e){
if(tx!=null){
tx.rollback();
}
e.printStackTrace();
}
}
这是我的web.xml
<servlet>
<servlet-name>validate1</servlet-name>
<servlet-class>validate1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validate1</servlet-name>
<url-pattern>/validate1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file> register.jsp</welcome-file>
</welcome-file-list>
您忘记交易:
try (Session session = HibernateUtil.getSessionFactory().openSession())
{
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}