建立Hibernate專案步驟
阿新 • • 發佈:2018-12-21
建立專案(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());
}
}