Eclipse开发环境Hibernate持久层数据库存取测试(5

回到我们刚才提到的ORMService类,我们将在这个类中调用Hibernate的持久化操作,将一个新的Temp类对象

存入数据库中。这一切都是由Hibernate经由我们为temp配置的映射文件,调用其session.save(temp)操作,

在后台执行的数据库插入动作,并不由我们费力的创建显示的connection链接并书写insert语名。

ORMService类的代码如下:

package com.learndiary.website.services;

import javax.servlet.ServletContext;

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.Configuration;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Session;

import net.sf.hibernate.Transaction;

import java.sql.Connection;

import java.io.File;

public class ORMService {

ServletContext context = null;

private static SessionFactory sessionFactory =null;

//用以测试MYSQL数据库连接的方法

public void initSessionFactory(){

try{

    File file = new File("g:\\Eclipse3.1Workspace\\LearnDiaryV1.0\\web\\WEB-INF\\src\\hibernate.cfg.xml");

Configuration cfg = new Configuration().configure(file);

SessionFactory factory = cfg.buildSessionFactory();

sessionFactory = factory;

if(sessionFactory ==null){

System.out.println("sessionFactory is null!");

}

}catch(Exception ex){

ex.printStackTrace();

}

}

public boolean saveTempData(){

boolean success = false;

String name = "binglian";

String value = "coding girl";

String description ="programer";

Session session =null;

Transaction tr =null;

try{

Temp temp = new Temp(name, value, description);

if(sessionFactory !=null){

session = sessionFactory.openSession();

tr  = session.beginTransaction();

Connection con = session.connection();

if(con !=null){

session.save(temp);

session.flush();

}else{

System.out.println("the connection is null!");

}

tr.commit();

success = true;

}else{

System.out.println("the sessionFactory is null");

}

}catch(Exception eg){

try{

if(tr !=null) {tr.rollback();}

}catch(Exception eh){

eh.printStackTrace();

}

eg.printStackTrace();

System.out.println("session save failed!");

}finally{

try{

session.close();

}catch(Exception em){

em.printStackTrace();

}

}

return success;

}

   

       public static void main(String args[]){

ORMService ser = new ORMService();

ser.initSessionFactory();

boolean saved = ser.saveTempData();

System.out.println("data has " + saved + " saved!");

}        

           

Eclipse开发环境Hibernate持久层数据库存取测试(4

再为Temp.java到数据库中的temp表做映射,映射文件名为Temp.hbm.xml,请注意书写正确的映射文件名,

Temp.hbm.xml如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping

PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

  <class name="com.learndiary.website.module.beans.Temp" table="TEMP" >

    <cache usage="read-write"/>

    <id name="id" type="long" column="ID">

      <generator class="increment" />

    </id>

    <property name="name" type="string" >

      <column name="NAME" length="15"/>

    </property>

    <property name="value" type="string" >

      <column name="VALUE" length="30" />

    </property>

    <property name="description" type="string" >

      <column name="DESCRIPTION"  />

    </property>

  </class>

</hibernate-mapping>

   其中:<id name="id" type="long" column="ID">

      <generator class="increment" />

    </id>

   这一段中的  <generator class="increment" />指定temp表的 id的设置,不由应用程序设置,由MYSQL数据库

   中的内置序列号增长器控制生成序列号。即数据库将根据每次插入的记录,自动为其生成序列号。我们在执行

   Hibernate的持久层操作,对新建的Temp类对象进行持久化保存时,也不要为这个新的对象设置其id 值。

Eclipse开发环境Hibernate持久层数据库存取测试(3

以上设置为开发前的预配置。下面为Eclipse开发过程中的类及文件的创建和配置。

首先创建一个新的项目。如果是新建了一个WEB Project, 可以SRC下,设置你所要的包名。此处简化为mypackage.

在这个包名下,创建一个你用以施行持久层测试的测试类,我们把它命名为:ORMService.java ,在此之前,你可

以先创建基于数据库中temp 表的Temp.java类。 Temp.java的代码如下:

/*

 * Created on 2005-7-27

 *本类用以对MYSQL数据库的测试

 *持久层组件为Hibernate

 */

package com.learndiary.website.module.beans;

import java.io.Serializable;

/**

 * @author binglian

 *本类的hbm映射文件为:Temp.hbm

 */

public class Temp implements Serializable{

private Long id;

private String name;

private String value;

private String description;

public Temp(){

}

public Temp( String Vname, String Vvalue, String Vdescription){

this.name = Vname;

this.value =Vvalue;

this.description = Vdescription;

}

/**

* @return Returns the description.

*/

public String getDescription() {

return description;

}

/**

* @param description The description to set.

*/

public void setDescription(String description) {

this.description = description;

}

/**

* @return Returns the id.

*/

public Long getId() {

return id;

}

/**

* @param id The id to set.

*/

public void setId(Long id) {

this.id = id;

}

/**

* @return Returns the name.

*/

public String getName() {

return name;

}

/**

* @param name The name to set.

*/

public void setName(String name) {

this.name = name;

}

/**

* @return Returns the value.

*/

public String getValue() {

return value;

}

/**

* @param value The value to set.

*/

public void setValue(String value) {

this.value = value;

}

}

Eclipse开发环境Hibernate持久层数据库存取测试(2

配置MYSQL驱动程序:将MYSQL驱动的jar包考贝到你所要测试的项目的lib

              目录下,并将其路径设置为系统的环境变量,即设置CLASSPATH值,包含此

              驱动程序的路径,本例中设置如下:

               CLASSPATH值 G:\Eclipse3.1Workspace\LearnDiaryV1.0\web\WEB-INF\lib

              如果你拥有一套JBuilder开发工具,打开它,测试你的MYSQL在开发环境中

              是否可以正确连接。点击JBuilder->tool->DataBase pilot,在窗口中新建

              一个到数据库learndiary的连接。设置驱动程序名和MYSQL服务器链接地址:

              driver name:com.mysql.jdbc.Driver ;    

              database url:dbc:mysql://localhost:3306/learndiary

              如果你的MYSQL数据库位于同一域中的另一台机器上,将localhost替换为

              另一台机器的地址,如192.168.1.12, 3306为默认的访问MYSQL服务的端口

              号。接下来设置user name:root,  选中extended properties复选框,点击

              properties栏,在出现的对话框内选择add new , 点击左侧的name,value,

              依次输入:password, 1234, 双击required.保存退出。在DataBase pielot

              对话框内左侧点击MYSQL的+号,出现数据库连接中的动画。稍后数据库连接

              成功,右击MYSQL连接,选择CLOSE,关闭刚才的连接。退出JBuilder.

Eclipse开发环境下Hibernate持久层数据库存取测�

日期:2005年7月29日

作者:binglian

联系方式:binglianrushui@sohu.com

          QQ:284508610

开发环境配置:Eclipse3.0.2, Hibernate2.0, MYSQL数据库。

              打开MYSQL Command Line Client(MYSQL 服务器的客户端交互程序),

              输入ROOT用户登录密码。即你在安装MYSQL服务器时,为ROOT用户设置

              的密码。显示登录成功。创建数据库,此处命名为:learndiary. 输入

              use learndiary,起用此数据库; 注意以分号结尾。创建表:temp.

              表中包括的字段为:

              id bigint, name char(15), value char(30), description text.

              将hibernate.jar, hibernate-tools.jar包考贝到你所要测试的项目的lib

              目录下。如果你想为你的测试程序加入日志追踪,将common-logging.jar

              也考入lib目录下。

Daughter is growing

1.She can count number from 1 to 10,if she is happy then.But,the status isn't stable;

2.She can resite ancient poems,such as <<静夜思>>,<<鹅>>,<<锄禾>>,etc...,although the pronounciation isn't accurate.This is her mother's result:);

3.She can disguise crying if she isn't happy for something,not the real crying:)

4.Her health status is better and better than before.

give the freedom to user,form group spontaneously

1.with the XML technique,the articles of a user can be process freely;

2.a user can build a group with several goals spontaneously,like QQ group;

3.before a user creating a goal really,a searching process should be taken for avoiding some duplicate goal;

4.Maybe,if a user build a group,a approving process should be taken for avoiding some duplicate or improper group to be found.

a difficut:struts module

For my viewpoint,struts module should be a useful technique.It offer a framework to resolve a important aspect of OOP.

So,I think we can import this technique in our future Learndiary Version for extension goal.And,I have tried to use this in fileSharing trial system(there are problem in this using for my inapprehension on this technique.)

But,from some resource of learning it,I found there are many restricts on this-all of a module:include action,jsp,messageresource,datasource...,are added the module prefix automatically!e.g.,a path of jsp is:/WEB-INF/amodule/test.jsp,it will be /amodule/WEB-INF/amodule/test.jsp.And,the action is so too.

I feel the struts module is a bit complex.