首先需要导入hibernate所需要的包到lib或者其他方式导入project中,同时还需要jdbc的数据连接包,如果包版本不同,操作方式上也有所不同。hibernate框架下载地址:
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:guard</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
</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;
}
}