休眠 – 网络应用程序
使用休眠的 Web 应用程序更容易。 JSP 页面是获取用户输入的最佳方式。这些输入被传递给 servlet,最后,通过使用 hibernate 将其插入到数据库中。这里 JSP 页面用于表示逻辑。 Servlet 类用于控制器层。 DAO 类用于数据库访问代码。
本项目使用的工具和技术:
- JDK 1.6+ 起
- Hibernate 3.6.3.Final 或下一个版本
- 蚀
- MySQL 5.5。或下一个版本
- 雄猫
随着 Hibernate – Web 应用程序的准备工作,该项目将作为一个动态 Web 项目进行准备。以下 jars 需要放在 WEB-INF/lib 文件夹中
从用户获取输入的前端 JSP 页面
注册.jsp
HTML
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
Registration Form
GeekUser Registration Form
Java
import com.gfg.hibernate.dao.GeekUserDAO;
public class GeekUserControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// From register.jsp, the controls
// are passed to this controller servlet
String userName = request.getParameter("userName");
String password = request.getParameter("password1");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String city = request.getParameter("city");
HttpSession session = request.getSession(true);
try {
GeekUserDAO userDAO = new GeekUserDAO();
// Via DAO class addGeelUser method,
// the data is inserted into the table
userDAO.addGeekUser(userName, password, email, phone, city);
// After that it is redirected to the success page
response.sendRedirect("Success");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.gfg.hibernate.bean.GeekUser;
public class GeekUserDAO {
// Method to insert GeekUser in the database
public Integer addGeekUser(String userName,
String password,
String email,
String phone,
String city) {
SessionFactory sessionFactory;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Session session = sessionFactory.openSession();
Transaction tx = null;
Integer employeeID = null;
try {
tx = session.beginTransaction();
GeekUser geekUser = new GeekUser(userName, password, email,phone,city);
employeeID = (Integer) session.save(geekUser);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return employeeID;
}
}
Java
public class GeekUser {
// each and every attribute should have
// the respective column in the database
private int id;
private String geekUserName;
private String password;
private String geekEmail;
private String phone;
private String city;
public GeekUser() {
}
// parameterized constructors are
// necessary to carry the details
public GeekUser(String userName,
String password,
String email,
String phone,
String city) {
this.geekUserName = userName;
this.password = password;
this.geekEmail = email;
this.phone = phone;
this.city = city;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGeekUserName() {
return geekUserName;
}
public void setGeekUserName(String userName) {
this.geekUserName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password1) {
this.password = password1;
}
public String getGeekEmail() {
return geekEmail;
}
public void setGeekEmail(String email) {
this.geekEmail = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SuccessPage extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println("" + "" + ""
+ "GeekUser details are added successfully via Hibernate. " + "
" + ""
+ "");
}
}
XML
XML
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/geeksforgeeks
root
XXXX
org.hibernate.dialect.MySQLDialect
true
true
create
XML
HibernateWebApp
User
User
com.gfg.hibernate.controller.GeekUserControllerServlet
Success
Success
com.gfg.hibernate.controller.SuccessPage
User
/register
Success
/Success
register.jsp
XML
org.hibernate.dialect.SQLServerDialect
com.microsoft.jdbc.Driver
jdbc.Microsoft://localhost/
root
****
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@localhost:1521:dbname
user
***
org.hibernate.dialect.Oracle8iDialect
true
主休眠配置文件 > hibernate.cfg.xml
XML
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/geeksforgeeks
root
XXXX
org.hibernate.dialect.MySQLDialect
true
true
create
由于这是 Web 应用程序,让我们也看看 web.xml,这将有助于从 jsp 到 servlet 的数据流。
XML
HibernateWebApp
User
User
com.gfg.hibernate.controller.GeekUserControllerServlet
Success
Success
com.gfg.hibernate.controller.SuccessPage
User
/register
Success
/Success
register.jsp
项目视频文件:
结论
在本教程中,我们已经看到通过使用休眠的非常好的特性,数据以更简单的方式插入到数据库表中。更重要的是,对于任何类型的数据库,这都是有效的。如果我们要更改不同的数据库,我们需要更改 hibernate.cfg.xml 以支持它
XML
org.hibernate.dialect.SQLServerDialect
com.microsoft.jdbc.Driver
jdbc.Microsoft://localhost/
root
****
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@localhost:1521:dbname
user
***
org.hibernate.dialect.Oracle8iDialect
true