1.新建一个java工程。加入hibernate的jar包
Hibernate不一定应用在web工程中,一个简单的java 工程也可以应用Hibernate

2. 在src目录下新建hibernate配置文件应用程序编写,文件名称为hibernate.cfg.xml
hibernate.cfg.xml
这里使用的数据库为oracle数据库
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectproperty>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orclproperty>
<property name="hibernate.connection.username">scottproperty>
<property name="hibernate.connection.password">adminproperty>
<mapping resource="com/demo/pojo/TbUser.hbm.xml" />
session-factory>
hibernate-configuration>
3.在数据库中建立表:
create table tb_demo
(
id int primary key,
username varchar2(100),
password varchar2(100),
sex varchar2(2),
address varchar2(100)
)
4.在src目录建立pojo 包,新建TbDemo.java实体类。
package com.demo.pojo;
public class TbDemo {
private int id;
private String username;
private String password;
private String sex;
private String address;
public TbDemo() {
super();
}
public TbDemo(String username, String password, String sex, String address) {
super();
this.username = username;
this.password = password;
this.sex = sex;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "TbDemo [address=" + address + ", password=" + password
+ ", sex=" + sex + ", username=" + username + "]";
}
}
5 .在与TbDemo类相同的包下建立TbDemo.hbm.xml文件。文件内容如下:
TbDemo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="com.demo.pojo">
<class name="TbDemo" table="tb_demo">
<id name="id" column="id" type="int">
<generator class="sequence">
<param name="sequence">tb_user_seqparam>
generator>
id>
<property name="username" column="username">property>
<property name="password">property>
<property name="sex">property>
<property name="address">property>
class>
hibernate-mapping>
注意:需要在hibernate.cfg.xml文件中关联此文件。
6.编写工具类,用来获取SessionFactory
package com.demo.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory;
private HibernateSessionFactory() {
}
static {
Configuration cfg = new Configuration();
cfg = cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
/**
* 对外提供获取SessionFactory对象的方法
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
7. 测试类
package com.demo.test;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-11-9 上午11:13:28
* 类说明
*/
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.demo.pojo.TbDemo;
import com.demo.util.HibernateSessionFactory;
public class Test {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateSessionFactory
.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tran = session.beginTransaction();
try {
session.save(new TbDemo("admin", "123456", "男", "地址不详"));
tran.commit();
} catch (HibernateException e) {
e.printStackTrace();
tran.rollback();
}
session.close();
}
}
(编辑:威海站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|