创建项目(java project / dynamic web project)
添加需要的jar包
一、在项目上单击右键,选择New ——》other
二、点击下面的Hibernation 创建hibernate.cfg.xml文件
三、数据库加载的数据,在Session factory name 处可以为空,不必创建名称
数据库URL后面必须填写数据库项目名称
四、在eclipse页面上面显示hibernate视窗
四、在eclipse页面上面显示hibernate视窗
五、在项目上右键,创建reveng.xml文件 直接点击next
六、不用点击,直接用默认的方式,点击next
七、创建映射类对应的hbm.xml文件
八、反向生成对应的类
下图完成之后点击apply,然后选择上面的Exporters
选择下图的Domain code 和 Hibemate XML Mappings
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="sessionFactory">
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.default_schema">scott</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<mapping resource="com/db/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
user 类
package com.db.entity;
public class User {
private Long id;
private String name;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(Long id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public User() {
super();
}
}
user的映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-1 17:27:33 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.db.entity.User" table="USERS">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>
对数据库对应数据表进行增删改操作:
package com.db.test;
import java.util.List;
import javax.jws.soap.SOAPBinding.Use;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import com.db.entity.User;
public class HibernateTest1 {
public static void main(String[] args) {
//add();
//delete();
//update();
//doQueryAll();
doQuery();
}
//增加
public static void add(){
Configuration con=new Configuration().configure();
//创建SessionFactory
SessionFactory sessionFactory=con.buildSessionFactory();
//打开一个会话 与数据库
Session session=sessionFactory.openSession();
User user=new User(98L,"gg","123456");
Transaction ts=session.beginTransaction();
session.save(user);
ts.commit();
session.close();
sessionFactory.close();
}
//删除
public static void delete(){
Configuration con=new Configuration().configure();
//创建SessionFactory
SessionFactory sessionFactory=con.buildSessionFactory();
//打开一个会话 与数据库
Session session=sessionFactory.openSession();
User user=(User) session.get(User.class, 98L);
//事务开始
Transaction tx=session.beginTransaction();
session.delete(user);
//提交
tx.commit();
session.close();
sessionFactory.close();
}
//修改
public static void update(){
Configuration conn=new Configuration().configure();
//单例模式 创建SessionFactory
Session session=conn.buildSessionFactory().openSession();
//事务开始
User user=(User) session.get(User.class, 14L);
//修改什么就重新设置属性值
user.setName("xiaoming");
Transaction tx=session.beginTransaction();
//提交
session.update(user);
tx.commit();
session.close();
}
//查询所有数据
public static void doQueryAll(){
Configuration conn=new Configuration().configure();
//打开session连接
Session session=conn.buildSessionFactory().openSession();
//事务开始
Transaction tx=session.beginTransaction();
//创建查询
Query query= session.createQuery("from User"); //from 后面跟映射的类名
List<User> list=query.list();
//提交
tx.commit();
//关闭session
session.close();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName()+" "+list.get(i).getPassword());
}
}
public static void doQuery(){
Configuration conn=new Configuration().configure();
//打开session连接
Session session=conn.buildSessionFactory().openSession();
//事务开始
Transaction tx=session.beginTransaction();
//创建查询
User user= (User) session.get(User.class,14L );
//提交
tx.commit();
//关闭session
session.close();
System.out.println(user.getName()+" "+user.getPassword());
}
}