欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单的手工hibernate程序示例

程序员文章站 2022-05-18 12:55:09
本文讲述了简单的手工hibernate程序示例。分享给大家供大家参考。具体如下: 今天学习了下hibernate,写了个小的手工程序,总结下, 首先创建数据库表: 复...

本文讲述了简单的手工hibernate程序示例。分享给大家供大家参考。具体如下:

今天学习了下hibernate,写了个小的手工程序,总结下,

首先创建数据库表:

复制代码 代码如下:
create table increment_testr(id bigint not null, name char(10), primary key(id));

eclipse下,新建工程。

新建数据库表的映射,这里使用手工方式完成:

incrementtester.java

public class incrementtester {
  private long id;
  private string name;
  public incrementtester(){}
  public incrementtester(string name){
    this.name = name;
  }
  public long getid(){
    return this.id;
  }
  private void setid(long id){
    this.id = id;
  }
  public string getname(){
    return this.name;
  }
  public void setname(string name){
    this.name = name;
  }
}

对应编写映射xml文件

incrementtester.hbm.xml

public "-//hibernate/hibernate mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
private

实现具体功能的类bussinessservice

import java.lang.reflect.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class bussinessservice {
  public static sessionfactory sessionfactory;
  static{
    try{
      configuration config = new configuration().configure();
      sessionfactory = config.buildsessionfactory();
    }catch(exception e){
      e.printstacktrace();
    }
  }
  public void findallobjects(string classname){
    session session = sessionfactory.opensession();
    transaction tx = null;
    try{
      tx = session.begintransaction();
      list objects = session.createquery("from "+classname).list();
      for(iterator it = objects.iterator();it.hasnext();){
        long id = new long(0);
        incrementtester xx = (incrementtester)it.next();
        id=xx.getid();
        system.out.println("id of "+classname+":"+id+" name: "+xx.getname());
      }
      tx.commit();
    }catch(exception e){
      e.printstacktrace();
    }finally{
      session.close();
    }
  }
  public void saveobject(object object){
    session session = sessionfactory.opensession();
    transaction tx = null;
    try{
      tx = session.begintransaction();
      session.save(object);
      tx.commit();
    }catch(exception e){
      e.printstacktrace();
      if(tx != null){
        tx.rollback();
      }
    }finally{
      session.close();
    }
  }
  public void deleteallobject(string classname){
    session session = sessionfactory.opensession();
    transaction tx = null;
    try{
      tx = session.begintransaction();
      query query=session.createquery("delete from "+classname);
      query.executeupdate();
      tx.commit();
    }catch(exception e){
      e.printstacktrace();
      if(tx!=null){
        tx.rollback();
      }
    }finally{
      session.close();
    }
  }
}

实现主函数:

public class test {
  /**
   * @param args
   */
  public static void main(string[] args) {
    // todo auto-generated method stub
    string name="incrementtester";
    bussinessservice aa = new bussinessservice();
    aa.deleteallobject(name);
    object o1 = null;
    try {
      o1 = class.forname(name).newinstance();
      ((incrementtester)o1).setname("caijie");
      aa.saveobject(o1);
      o1 = class.forname(name).newinstance();
      ((incrementtester)o1).setname("gufeng");
      aa.saveobject(o1);
    } catch (exception e) {
      e.printstacktrace();
    }
    aa.findallobjects(name);
  }
}

hibernate配置文件:hibernate.cfg.xml
 
public "-//hibernate/hibernate configuration dtd//en"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.mysqldialect
com.mysql.jdbc.driver
jdbc:mysql://localhost:3306/test
root
root
true

增加相应的库,运行后成功得到结果:

hibernate: delete from increment_testr
hibernate: select max(id) from increment_testr
hibernate: insert into increment_testr (name, id) values (?, ?)
hibernate: insert into increment_testr (name, id) values (?, ?)
hibernate: select incrementt0_.id as id0_, incrementt0_.name as name0_ from increment_testr incrementt0_
id of incrementtester:1 name: caijie
id of incrementtester:2 name: gufeng

希望本文所述对大家的jsp程序设计有所帮助。