The first is the Hibernate.cfg.xml configuration, the file is placed in the SRC directory
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--Database connection Settings--
<property name= "Connection.driver.class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://192.168.1.81:3306/test</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >123456</property>
<!--JDBC Connection pool (use the built-in)--
<property name= "Connection.pool_size" >1</property>
<!--SQL dialect--
<property name= "dialect" >org.hibernate.dialect.MySQL5Dialect</property>
<!--Enable Hibernate ' s automatic session conttext management--
<property name= "Current_session_context_class" >thread</property>
<!--Disable the Second-level cache--and
<property name= "Cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>
<!--Echo all executed SQL to show--
<property name= "Show_sql" >true</property>
<!--Format SQL--
<property name= "Format_sql" >true</property>
<!--Drop and re-create the database schema on startup--
<property name= "Hbm2ddl.auto" >update</property>
<mapping resource= "Com/bjsxt/hibernate/student.hbm.xml"/>
</session-factory>
Student.java file
Package com.bjsxt.hibernate;
public class Student {
Private String uname;
private int age;
Private String sex;
Public String Getuname () {
return uname;
}
public void Setuname (String uname) {
This.uname = uname;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
}
Configuration of the Student.hbm.xml
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
<class name= "com.bjsxt.hibernate.Student" table= "userinfo" >
<id name= "uname" ></id>
<property name= "Age"/>
<property name= "Sex"/>
</class>
Test file for testing
Package com.bjsxt.hibernate;
Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.AnnotationConfiguration;
public class Test {
public static void Main (String args[]) {
Query ();
}
Inserting data
public static void Insert () {
Student s = new Student ();
S.setuname ("Xiaoming");
S.setsex ("female");
S.setage (13);
Sessionfactory sessionfactory = new Annotationconfiguration ()
. Configure (). Buildsessionfactory ();
Session session = Sessionfactory.getcurrentsession ();
Session.begintransaction ();
Session.save (s);
Session.gettransaction (). commit ();
}
Delete data
public static void Delete () {
Student s = new Student ();
S.setuname ("Xiaoming");
Sessionfactory sessionfactory = new Annotationconfiguration ()
. Configure (). Buildsessionfactory ();
Session session = Sessionfactory.getcurrentsession ();
Session.begintransaction ();
Session.delete (s);
;
Session.gettransaction (). commit ();
}
modifying data
public static void Updata () {
Sessionfactory sessionfactory = new Annotationconfiguration ()
. Configure (). Buildsessionfactory ();
Session session = Sessionfactory.getcurrentsession ();
Session.begintransaction ();
Student s = (Student) session.get (Student.class, "xiaoming");
if (s! = null) {
S.setsex ("female");
S.setage (100);
Session.update (s);
;
Session.gettransaction (). commit ();
}
}
Querying data
public static void query () {
Sessionfactory sessionfactory = new Annotationconfiguration ()
. Configure (). Buildsessionfactory ();
Session session = Sessionfactory.getcurrentsession ();
Session.begintransaction ();
Query Query = Session
. CreateQuery ("from Student where uname= ' xiaoming '");
list<student> list = Query.list ();
for (Student s:list) {
System.out.println (S.getage () + "\ n");
}
}
}
Hibernate basic Simple usage