1.1 编写流程
l 1. 导入jar包
l 2. 创建数据库和表
l 3. 编写核心配置文件(hibernate.cfg.xml)--> 配置获得链接等参数
l 4. 编写映射文件hibernate mapping(*.hbm.xml)
l 5 使用api测试
一《先配置hibernate.cfg.xml文件》
在<session-factory中配置>先配置方言
1.ctral +shift+T 输入oracle.di 选择org.hibernate.dialect.OracleDialect
配置驱动路径(根据驱动不同而异)
2.oracle.jdbc.OracleDriver
创建数据库连接
3.jdbc:oracle:thin:@localhost:1521:oracl
用户名
4:
密码
5:
<property name="show_sql">true</property>
<!-- 在控制台打印sql语句时,是否格式化 -->
<property name="format_sql">true</property>
<!-- hbm2dd1.auto:是否自动生成表结构:只有语句时,框架可以自己自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource=""/>
最后的mapping不要忘了写(Xxx.hbm.xml写好了之后)
二、配置Xxx.hbm.xml,选择新建baseXML文件,选择next Create XML file 继续next 找到dtd配置mapping文件
----------------配置配置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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">system</property>
<property name="connection.password">password</property>
<!-- show sql:操作数据库是会在控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- 在控制台打印sql语句时,是否格式化 -->
<property name="format_sql">true</property>
<!-- hbm2dd1.auto:是否自动生成表结构:只有语句时,框架可以自己自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
--------------配置User.hbm.xml (注意这个起名要遵循规范:类名+hbm)
<?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>
<class name="entity.Student" table="MYUSER">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">seq_mysuser</param>
</generator>
</id>
<property name="name" column="USERNAME "/><!-- 如果实体类的类名和数据库的字段名是一致的。数据库的字段名可以不写 -->
<property name="userPwd" />
<property name="nickName" />
<property name="age" />
<property name="birthday" />
</class>
</hibernate-mapping>
实体类‘
package entity;
import java.io.Serializable;
import java.util.Date;
/**
* hibernate的实体类
* 1.实现序列号化接口
* 2.提供无参构造方法
* @author Administrator
*
*/
public class Student implements Serializable {
private Integer id;
private String name;
private String nickName;
private String userPwd;
private Integer age;
private Date birthday;
public Student() { //定义一个无参构造方法,方便set赋值
super();
}
public Student(Integer id, String name, String nickName, String userPwd,
Integer age, Date birthday) {
super();
this.id = id;
this.name = name;
this.nickName = nickName;
this.userPwd = userPwd;
this.age = age;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
更改数据
package Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import entity.Student;
public class TestUpdate {
public static void main(String[] args) throws ParseException {
Configuration cfg=null;
SessionFactory fac=null;
Session session=null;
Transaction tran=null;
try {
//1.打开配置文件
cfg=new Configuration().configure();
//2.根据配置文件构建工程
fac=cfg.buildSessionFactory();
//3.用工厂打开【配置文件
session=fac.openSession();
//4.如果执行数据增,删,改,查
tran=session.beginTransaction();
Student student=(Student)session.load(Student.class, 3);
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd");
student.setBirthday(sm.parse("1992-22-22"));
student.setAge(22);
student.setName("小王ba");
tran.commit();//成功提交事务
} catch (HibernateException e) {
tran.rollback();//失败回滚事务
e.printStackTrace();
}
System.out.println("程序运行结束");
}
}
删除语句
package Test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import entity.Student;
public class TestDelete {
public static void main(String[] args) {
Configuration cfg = null;
SessionFactory fac = null;
Session session = null;
Transaction tran = null;
try {
// 1.加载配置文件
cfg = new Configuration().configure();
// 2.根据配置信息构建session工厂
fac = cfg.buildSessionFactory();
// 3.通过sessio工厂打开session连接
session = fac.openSession();
// 4.如果执行增删改查,则必须以事务环境运行
tran = session.beginTransaction();
Student u = (Student) session.load(Student.class, 5);// 删掉id为5的数据
// 5.执行增删改操作
session.delete(u);
// 若无异常,则提交事务
tran.commit();
} catch (HibernateException e) {
// 发生异常,则回滚事务
e.printStackTrace();
} finally {
// 7.关闭session连接
if (null != session) {
session.close();
}
}
System.out.println("程序运行结束");
}
}
查询语句
package Test;
import java.text.SimpleDateFormat;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import entity.Student;
public class TestQuery {
public static void main(String[] args) {
SimpleDateFormat sf = new SimpleDateFormat();
Configuration cfg = null;
SessionFactory fac = null;
Session session = null;
// 1.读取配置文件信息
cfg = new Configuration().configure();
// 2.根据配置文件构建session工厂
fac = cfg.buildSessionFactory();
// 3.打开session
session = fac.openSession();
// 使用get查询
Student student = (Student) session.get(Student.class, 3);
if (null != student) {
System.out.println("id为:" + student.getId() + "姓名为:"
+ student.getName() + "昵称为:" + student.getNickName()
+ "密码为:" + student.getUserPwd());
} else {
System.out.println("不存在该用户");
}
}
}
增加语句
package Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import entity.Student;
public class TestSave {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-MM");
Configuration cfg = null;
SessionFactory fac = null;
Session session = null;
Transaction tran = null;
try {
// 1.加载配置文件
cfg = new Configuration().configure();
// 2.根据配置信息构建session工厂
fac = cfg.buildSessionFactory();
// 3.通过session工厂打开session连接
session = fac.openSession();
// 4.如果执行数据增,删,改,查,则必须以事务环境运行
tran = session.beginTransaction();
Student st = new Student();
st.setAge(12);
st.setName("小王");
st.setNickName("小王八");
st.setUserPwd("111111");
Date st1 = df.parse("1992-11-20");
st.setBirthday(st1);
// 5执行增删改操作
session.save(st);
// 6.若无异常发生
tran.commit();
} catch (HibernateException e) {
// 6.若异常发生,则回滚事务
tran.rollback();
e.printStackTrace();
} finally {
// 7.关闭连接
if (null != session) {
session.close();
}
}
System.out.println("程序结束了");
}
}
创建表就不写了,注意字段在配置文件的映射关系
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以上操作可以通过工具类简化操作
列如:
package Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import Util.HibernateUtil;
import entity.Student;
public class TestSave {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-MM");
Transaction tran = null;
try {
Session session = HibernateUtil.currentSesion();
// 4.如果执行数据增,删,改,查,则必须以事务环境运行
tran = session.beginTransaction();
Student st = new Student();
st.setAge(12);
st.setName("小李222");
st.setNickName("小李子111");
st.setUserPwd("33333");
Date st1 = df.parse("2002-11-20");
st.setBirthday(st1);
// 5执行增删改操作
session.save(st);
// 6.若无异常发生
tran.commit();
} catch (HibernateException e) {
// 6.若异常发生,则回滚事务
tran.rollback();
e.printStackTrace();
} finally {
// 7.关闭连接
HibernateUtil.closeSession();
}
System.out.println("程序结束了");
}
}