欢迎来到代码驿站!

JSP代码

当前位置:首页 > 软件编程 > JSP代码

简单的手工hibernate程序示例

时间:2020-11-22 22:37:14|栏目:JSP代码|点击:

本文讲述了简单的手工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程序设计有所帮助。

上一篇:Java代码编写的一般性指导

栏    目:JSP代码

下一篇:JSP使用JDBC连接MYSQL数据库的方法

本文标题:简单的手工hibernate程序示例

本文地址:http://www.codeinn.net/misctech/25111.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有