小配置裏的代碼:java
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity">
<class name="Dog" table="Dog" >
<id name="dogid" column="dogid">
<generator class="native"></generator>//id主鍵 自增
</id>
<property name="dogname" column="dogname"></property>
<property name="dogage" column="dogage" ></property>
</class>
</hibernate-mapping>
實體類:sql
/** * Created by lgy on 2017/9/18. */ public class Dog { private String dogname; private Integer dogage; private Integer dogid; public String getDogname() { return dogname; } public void setDogname(String dogname) { this.dogname = dogname; } public Integer getDogage() { return dogage; } public void setDogage(Integer dogage) { this.dogage = dogage; } public Integer getDogid() { return dogid; } public void setDogid(Integer dogid) { this.dogid = dogid; } }
util:數據庫
public class HibernateUntil { private static Configuration cfg=new Configuration().configure(); private static SessionFactory factory=cfg.buildSessionFactory(); //1.方法返回session 靜態成員變量不能直接使用非靜態成員 //Session依賴於Session工廠,工廠依賴於Configure public static Session getSession(){ return factory.openSession(); } //2.關閉session public static void closeSession(){ getSession().close();
大配置:session
<?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> <!--數據庫驅動--> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!--數據庫url--> <property name="connection.url" >jdbc:oracle:thin:@localhost:1521:orcl</property> <!--數據庫username--> <property name="connection.username" >test</property> <!--數據庫password--> <property name="connection.password" >test</property> <!--hibernate方言--><!--和Spring整合時name屬性前S必須加hibernate,單獨用hibernate框架時,name屬性能夠直接用dialect--> <property name="hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</property> <!--輸出全部語句到控制檯--> <property name="hirbernate.show_sql" >true</property> <!--在log和console中打印出更新 格式化顯示sql--> <property name="hibernate.format_sql" >true</property> <!--hbm2ddl就是**.hbm.xml,生成特定數據庫的sql 從程序中自動建表 updata表示若是底層存在表,name更新表結構 --> <property name="hibernate.hbm2ddl.auto" >update</property> <!--指定當前session範圍和上下文 thread至當前線程用來跟蹤管理--> <property name="current_session_context_class" >thread</property> <mapping resource="cn/happy/entity/Dog.hbm.xml" ></mapping> </session-factory> </hibernate-configuration>
Test測試類:oracle
@Test public void testhibernate() { //configuration Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Transaction tx=session.beginTransaction(); Dog dog=new Dog(); dog.setDogname("小白"); session.save(dog); tx.commit(); System.out.println("add ok!"); session.close();
}
//用get方法查詢數據庫
@Test
private void findStudent() {
//02Hibernate 保存
//讀取大配置文件,獲取鏈接的數據庫信息
Configuration cfg=new Configuration().configure();
//3建立SessionFactory
SessionFactory factory=cfg.buildSessionFactory();
//加工session
Session session=factory.openSession();
//開啓事務
Transaction tx=session.beginTransaction();
//5Hibernate
//根據session的方法作數據操做 檢索
Student student=session.get(Student.class,2);
System.out.println(student.getName());
//提交事務
tx.commit();
//關閉session
session.close();
System.out.println("success ok");
}
//用load方法查詢
@Test
public void update2(){
Configuration cfg=new Configuration().configure();
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Dog dog=session.load(Dog.class,1);
System.out.println("load-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
System.out.println(dog.getDogname());
}
load方法:app
不管get方法查詢,仍是load方法,都要開啓事務,也不要手動提交!!!框架