Hibernate的核心組件 Persistent Object Hibernate的運(yùn)行過程 Hibernate簡(jiǎn)單示例: 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è)計(jì)持久化類TRegister.java package hibernate.PO; /** * 持久化類 */ public class TRegister implements java.io.Serializable { // Fields private Integer id; private String userName; private String userPwd; private String sex; private Integer age; // Constructors /** default constructor */ public TRegister() { } /** minimal constructor */ public TRegister(Integer id) { this.id = id; } /** full constructor */ public TRegister(Integer id, String userName, String userPwd, String sex, Integer age) { this.id = id; this.userName = userName; this.userPwd = userPwd; this.sex = sex; this.age = age; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return this.userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } } <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate./hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url"> jdbc:mysql://localhost:3306/study </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">MySQL5.0</property> <property name="connection.password">root</property> <property name="connection.driver_class"> org.gjt.mm.mysql.Driver </property> <property name="show_sql">true</property> <mapping resource="hibernate/PO/TRegister.hbm.xml" /> </session-factory> </hibernate-configuration> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate./hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="hibernate.PO.TRegister" table="t_register"> <id name="id" type="java.lang.Integer"> <column name="id" /> <!-- 我在MYSQL中并沒有設(shè)置該字段遞增,但在Hibernate中依然可以設(shè)置為遞增 --> <generator class="increment" /> </id> <property name="userName" type="java.lang.String"> <column name="userName" length="30" /> </property> <property name="userPwd" type="java.lang.String"> <column name="userPwd" length="30" /> </property> <property name="sex" type="java.lang.String"> <column name="sex" length="10" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> </class> </hibernate-mapping> package hibernate;
設(shè)計(jì)控制類/** * 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è)計(jì)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實(shí)例 Transaction tx = session.beginTransaction(); try { session.save(rg); //保存持久類對(duì)象 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); } } 編譯----->打包----->運(yùn)行----->OK! |
|