Oracle Starter Case:
1. Create an entity class student and override the ToString method
Packagecn.happy.entity; Public classStudent { PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @Override PublicString toString () {return"Student [name=" + name + ", age=" + Age + ", id=" + ID + "]"; } PrivateString name; PrivateInteger age; PrivateInteger ID;
}
2. Importing the JAR Package
3. Create a large configuration associated small configuration
<?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" >
<session-factory>
<property name= "Connection.driver_class" >oracle.jdbc.OracleDriver</property>
<property name= "Connection.url" >jdbc:oracle:thin: @localhost:1521:orcl</property>
<property name= "Connection.username" >scott</property>
<property name= "Connection.password" >0123</property>
<!--output all SQL statements to the console. -
<property name= "Hibernate.show_sql" >true</property>
<!--print more beautiful SQL in log and console. -
<property name= "Hibernate.format_sql" >true</property>
<!--dialect---
<property name= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</property>
<property name= "Hbm2ddl.auto" >update</property>
<mapping resource= "Cn/happy/entity/student.hbm.xml"/>
</session-factory>
4. Write small configuration coding= ' utf-8 '?>
<! DOCTYPE hibernate- mappingPublic "-//hibernate/hibernate mapping DTD 3.0//en" "/http WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD "> Package =" Cn.happy.entity "> < Class Name= "Student" table= "Student" > <id name= "id" type= "int" column= "id" > </id> <property name= "Name" type= "string" column= "name"/> <property name= "age" type= "Int." column= "Age"/> </class>
5. Tool Class
Packagecn.happy.entity;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classHibernateutil {Private StaticConfiguration cfg=NewConfiguration (). Configure (); Private StaticSessionfactory factory=cfg.buildsessionfactory (); //01. Method to return to session Public StaticSession getsession () {returnfactory.opensession (); } //02. Close Session Public Static voidclosesession () {getsession (). Close (); } }
6. Test class
//1.2 Revision Students@Test Public voidUpdatetest () {Session session=hibernateutil.getsession (); //object not tracked by context /*Student stu=new Student (); Stu.setid (3); Stu.setname ("Cold Rain Training Camp");*/ //Way Two: How to use the context tracking method//retrieves a record, an entity objectStudent stu= (Student) session.load (Student.class, 3); Stu.setname ("Jin Long refueling!!!!" "); Transaction TX=session.begintransaction (); Session.update (Stu); Tx.commit (); Hibernateutil.closesession (); System.out.println ("Update ok!"); }
Oracle Additions and deletions