Hibernate的核心組件 Persistent Object Hibernate的運行過程 Hibernate簡單示例: create table T_register
( id int primary key, userName varchar(30), userPwd varchar(30), sex varchar(10), age int )
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> <form action="servlet/RegisterServlet" method="POST"> 用戶名:<input type="text" name="userName" /><BR> 密 碼:<input type="text" name="userPwd" /><BR> 性 別:<input type="text" name="sex" /><BR> 年 齡:<input type="text" name="age" /><BR> <input type="submit" value="save" /> </form> </body> </html> 設(shè)計持久化類TRegister.java 持久化類
設(shè)計Hibernate配置文件hibernate.cfg.xml
hibernate.cfg.xml
設(shè)計映射文件TRegister.hbm.xml
TRegister.hbm.xml
設(shè)計hibernate基礎(chǔ)類HibernateUtil.java
package hibernate;
設(shè)計控制類/** * hibernate 基礎(chǔ)類 * @author fengyan * date 2007-01-09 02:32 */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { Configuration config = new Configuration().configure("/hibernate/hibernate.cfg.xml"); sessionFactory = config.buildSessionFactory(); } catch(Throwable e) { throw new ExceptionInInitializerError(e); } } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session)session.get(); //Open a new Session,if this Thread has none yet if(s == null || !s.isOpen()) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session)session.get(); session.set(null); if(s != null) s.close(); } } package hibernate.servlet;
/** * @author fengyan * date 2007-01-09 02:44 * 設(shè)計Hibernate控制類 */ import hibernate.HibernateUtil; import hibernate.PO.TRegister; 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; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; public class RegisterServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html;charset=GBK"; public void init() throws ServletException { // Put your code here } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); request.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); String sex = request.getParameter("sex"); int age = Integer.parseInt(request.getParameter("age")); TRegister rg = new TRegister(); rg.setAge(age); rg.setSex(sex); rg.setUserName(userName); rg.setUserPwd(userPwd); Session session = HibernateUtil.currentSession();//生成Session實例 Transaction tx = session.beginTransaction(); try { session.save(rg); //保存持久類對象 tx.commit(); //提交到數(shù)據(jù)庫 session.close(); response.sendRedirect("registerOK.jsp"); } catch(HibernateException e) { e.printStackTrace(); tx.rollback(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } 編譯----->打包----->運行----->OK! |
|
來自: 精髓_感知力 > 《1603資料-Java》