程序人生

java EE hibernate框架使用案例

时间:2017/4/23 10:31:08  作者:www.solgle.com  来源:solgle.com  查看:763  评论:0
内容摘要:首先需要导入hibernate所需要的包到lib或者其他方式导入project中,同时还需要jdbc的数据连接包,如果包版本不同,操作方式上也有所不同。hibernate框架下载地址:http://hibernate.org/orm/downloads/所需主要的jar包antl...
首先需要导入hibernate所需要的包到lib或者其他方式导入project中,同时还需要jdbc的数据连接包,如果包版本不同,操作方式上也有所不同。hibernate框架下载地址:http://hibernate.org/orm/downloads/
所需主要的jar包
antlr-2.7.7.jar
classmate-1.3.0.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.3.Final.jar
javassist-3.20.0-GA.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.1.Final.jar


在src路径下面,我需要定义hibernate的配置文件,用于数据库的连接配置信息hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>
 <session-factory>
 <property name="show_sql">true</property>
  <property name="format_sql">true</property> 
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:guard</property>
  <property name="connection.username">mysource</property>
  <property name="connection.password">111111</property>
  <property name="connection.isolation">2</property>
  <property name="hbm2ddl.auto">none</property> 
 
  <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
  <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  <mapping resource="./hbnado/Student.hbm.xml"/> <!-- 可指定路径 -->
 
 </session-factory>
</hibernate-configuration>

本文出自:http://www.solgle.com/news/359.html
定义一个hbnado包及使用的方法
package hbnado;
 
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.text.SimpleDateFormat;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
 
import beans.Student;
 
public class AdoStudent {
 
 SessionFactory factory = null;
      
      Session session = null;  
 
    private void openSession(){
    //获得一个数据库session 
System.out.println("create cfg begin");
    Configuration cfg = new Configuration().configure();
System.out.println("create cfg end");
factory = cfg.buildSessionFactory();   
    this.session=factory.openSession();
    }
    
    private void closeSession(){
    if(this.session!=null || this.session.isOpen()){
    this.session.close();
    this.factory.close();
    }
    }
    
protected void saveStuent() {  
   try{
    openSession();
            //事务控制开始  
            session.beginTransaction();               
            Student student = new Student();              
            //student.setSno("204");
            //System.out.println("sno:“+student.getSno());
            student.setSname("dddll");
            student.setSsex("男");
            student.setSclass("x0022");            
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = (Date) sdf.parse("2008-08-08");
            student.setSbirthday(date);
 
            session.save(student);  
            System.out.println("sno:"+student.getSno());
            
            //提交修改
            session.getTransaction().commit();  
            System.out.println("数据提交中");
              
        }catch(Exception e){  
            e.printStackTrace();  
            //事务回滚
            session.getTransaction().rollback();  
        }finally{  
        closeSession(); 
        }    
    }  
 
protected void queryStudent(){
Student student=new Student();
student.setSno("202");
openSession();
 
//Query  query = session.createQuery("select sno,sname from Student",Student.class);
   Query query = session.createSQLQuery("select sno,sname from Student");
 
   List list=query.list();
Iterator  it=list.iterator();
//System.out.println("rs:"+it.next().getClass());
   while(it.hasNext()){
    Object[] stu=(Object[])it.next();
    System.out.print("sno:"+stu[0]);
    System.out.println(" sname:"+stu[1]);
   
   }
 
closeSession();
}
 
protected void updateStudent(){
try{
openSession();
Student student=new Student();
student.setSno("201");
       student.setSname("10101");
       student.setSsex("男");
       student.setSclass("20220");                    
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       Date date = (Date) sdf.parse("1990-08-08");
       student.setSbirthday(date);
       
session.beginTransaction();
//session.update(student);
//session.delete(student);
session.getTransaction().commit();
closeSession();
}catch(Exception e){
session.getTransaction().rollback();
e.printStackTrace();
}
closeSession();
}
 
public static void main(String args[]){
 
    AdoStudent adoStu=new AdoStudent();
    adoStu.queryStudent();
    //System.out.println("数据操作完成");
}
}

同时就该包中配置映射信息Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
      
<hibernate-mapping package="beans">  
    
    <class name="Student" table="student">    
    <!--使用数据库序列      
        <id name="sno" type="long" > 
           <column name="sno" not-null="true" sql-type="NUMBER" unique="true"/>
  <generator class="sequence">
    <param name="sequence_name">seq_mysource</param>    
  </generator>
        </id>
         -->          
         <!-- hibernate不生成,由程序生成   
        <id name="sno" type="string" > 
           <column name="sno" not-null="true" sql-type="VARCHAR2" unique="true"/>
  <generator class="assigned" /> 
        </id> -->      
        <!-- 由hibernate生成,比较长  -->
        <id name="sno" type="string" > 
           <column name="sno" not-null="true" sql-type="VARCHAR2" unique="true"/>
  <generator class="uuid" /> 
        </id>    
        <property name="sname" type="string" column="sname" /> 
        <property name="ssex" type="string" column="ssex" />          
        <property name="sbirthday" type="date" column="sbirthday" />  
        <property name="sclass" type="string" column="sclass" />  
    </class>  
    
</hibernate-mapping>  

 
其他实体类型说明
package beans;
 
import java.util.Date;
 
public class Student {
protected String sno;
protected String sname;
protected String ssex;
protected Date sbirthday;
protected String sclass;
    
    
public String getSno() {
return sno;
}
 
/* public void setSno(Integer sno) {
this.sno = String.valueOf(sno);
}
public void setSno(long sno) {
this.sno = String.valueOf(sno);
}
*/
public void setSno(String sno) {
this.sno = sno;
}
 
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public Date getSbirthday() {
return sbirthday;
}
public void setSbirthday(Date sbirthday) {
this.sbirthday = sbirthday;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
    
 
}
 

标签:hibernate hibernate框架使用案例 java框架 

solgle.com 版权所有,欢迎分享!!!

相关评论
 img1 img2 img3 img4 img5 img6 img7 img8 img9 img10
评论者:      验证码:  点击获取验证码
   Copyright © 2013-2028 solgle.com,All rights reserved.[solgle.com] 公安机关备案号:51010802000219
Email:solgle@solgle.com; weixin:cd1008610000 ICP:蜀ICP备14011070号-1