<!-- Generated by MyEclipse Hibernate Tools.?????????????????? -->
<hibernate-configuration>
?<session-factory>
??<property name="connection.username">root</property>
??<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true;characterEncoding=gbk</property>
??<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
??<property name="myeclipse.connection.profile">MySqlDB</property>
??<property name="connection.password">1234</property>
??<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??<mapping resource="com/semovy/business/pojo/Employee.hbm.xml" /><!--對應對象映射文件資源-->.
?</session-factory>
</hibernate-configuration>
三、打開MyEclipse的DB Browser選擇Employee右擊creating hibernate mapping,設置持久類路徑好之后。
便自動產生Employee持久類,與同一目錄下的Employee.hbm.xml兩個文件
1).
package com.semovy.business.pojo;
?
/**
?* Employee generated by MyEclipse - Hibernate Tools
?*/
public class Employee? implements java.io.Serializable {
??? // Fields???
???? /**
? *
? */
?private static final long serialVersionUID = 1L;
?private Integer id;
???? private String name;
??? // Constructors
??? /** default constructor */
??? public Employee() {
??? }
?/** minimal constructor */
??? public Employee(Integer id) {
??????? this.id = id;
??? }
???
??? /** full constructor */
??? public Employee(Integer id, String name) {
??????? this.id = id;
??????? this.name = name;
??? }
??? // Property accessors
??? public Integer getId() {
??????? return this.id;
??? }
???
??? public void setId(Integer id) {
??????? this.id = id;
??? }
??? public String getName() {
??????? return this.name;
??? }
???
??? public void setName(String name) {
??????? this.name = name;
??? }
}
2).Employee.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"<!--
??? Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
??? <class name="com.semovy.business.pojo.Employee" table="employee" ><!---一定棄掉catalog屬性,我是卡在這里好久,運行有異常-->
??????? <id name="id" type="integer">
??????????? <column name="id" />
??????????? <generator class="identity" />
??????? </id>
??????? <property name="name" type="string">
??????????? <column name="name" length="50" />
??????? </property>
??? </class>
</hibernate-mapping>
四,建立Employee持久化類的業務邏輯:
package com.semovy.business.service;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.semovy.framework.base.HibernateSession;
/**
?* this class supply all type of busniss service method for Employees
?* @author Semovy
?* @since 2006-11-08
?* @version 1.0
?*/
public class EmployeeService {
?Session session = null;
?public EmployeeService(){}
?public List findAllEmployees()
?{
??List employees = new ArrayList();
??try
??{
???session = HibernateSession.currentSession();
???Transaction tx = session.beginTransaction();
???employees = session.createQuery("from Employee").list();
???tx.commit();
???return employees;
??}
??catch(Exception ex)
??{
???ex.printStackTrace();
???return null;
??}
??finally
??{
???HibernateSession.closeSession();
??}
?}
}
五,建立jsp頁面listEmployees.jsp來調用測試hibernate.
<%@ page language="java" import="java.util.*,
com.semovy.business.pojo.Employee,com.semovy.business.service.EmployeeService"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
???
??? <title>My JSP 'listEmployees.jsp' starting page</title>
???
??? <meta http-equiv="pragma" content="no-cache">
??? <meta http-equiv="cache-control" content="no-cache">
??? <meta http-equiv="expires" content="0">
??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
??? <meta http-equiv="description" content="This is my page">
???
??? <!--
??? <link rel="stylesheet" type="text/css" href="styles.css">
??? -->
? </head>
?
? <body>
??? This is my JSP page.<br>
??? <%
?
??? EmployeeService es = new EmployeeService();
??? List l = es.findAllEmployees();
??? if(l != null)
??? {
??? ?Iterator it = l.iterator();
??? ?while(it.hasNext())
??? ?{
??? ??Employee em = (Employee)it.next();
??? ??out.print("ID: " + em.getId() + "Name: " + em.getName()+"<br>");
??? ??
??? ?}?
??? }
??? %>
? </body>
</html>
六、發布應用