All jar packages under the hibernate/lib/required directory are imported under the path path. and a jar package to connect to the database
Hierarchical structure
Hibernate.cfg.xml
<?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 ">
User.java
Package org.zbq.util;
Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.hibernate.service.ServiceRegistryBuilder;
Import Org.zbq.bean.User;
public class Hibernateutil {private static sessionfactory sessionfactory;
static{try{Configuration conf = new Configuration ();
Conf.configure (); serviceregistry sr = new Serviceregistrybuilder (). Applysettings (Conf.getproperties ()). Buildserviceregi
Stry ();
Sessionfactory = Conf.buildsessionfactory (SR);
catch (Exception e) {e.printstacktrace ();
} public static void Save (user user) throws exception{Transaction tr = null;
Session session = Sessionfactory.opensession ();
try{tr = session.begintransaction ();//Tr.begin ();
Session.save (user);
Tr.commit (); catch (exceptioN e) {if (null!= TR) {tr.rollback ();
} throw E;
finally {session.close (); @SuppressWarnings ("unchecked") public static list<user> Listusers () throws exception{list<user> Li
st = NULL;
Transaction tr = null;
Session session = Sessionfactory.opensession ();
try{tr = session.begintransaction ();//Tr.begin ();
String sql = "from User";
Query query = session.createquery (SQL);
List = (list<user>) query.list ();
Tr.commit ();
catch (Exception e) {e.printstacktrace ();
finally {session.close ();
} return list;
}
}
User.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Hibernateutil.java
Package org.zbq.util;
Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.hibernate.service.ServiceRegistryBuilder;
Import Org.zbq.bean.User;
public class Hibernateutil {private static sessionfactory sessionfactory;
static{try{Configuration conf = new Configuration ();
Conf.configure (); serviceregistry sr = new Serviceregistrybuilder (). Applysettings (Conf.getproperties ()). Buildserviceregi
Stry ();
Sessionfactory = Conf.buildsessionfactory (SR);
catch (Exception e) {e.printstacktrace ();
} public static void Save (user user) throws exception{Transaction tr = null;
Session session = Sessionfactory.opensession ();
try{tr = session.begintransaction ();//Tr.begin ();
Session.save (user);
Tr.commit (); catch (exceptioN e) {if (null!= TR) {tr.rollback ();
} throw E;
finally {session.close (); @SuppressWarnings ("unchecked") public static list<user> Listusers () throws exception{list<user> Li
st = NULL;
Transaction tr = null;
Session session = Sessionfactory.opensession ();
try{tr = session.begintransaction ();//Tr.begin ();
String sql = "from User";
Query query = session.createquery (SQL);
List = (list<user>) query.list ();
Tr.commit ();
catch (Exception e) {e.printstacktrace ();
finally {session.close ();
} return list; }
}
Test class
Package org.zbq.test;
Import java.util.List;
Import Org.zbq.bean.User;
Import Org.zbq.util.HibernateUtil;
public class Hibernatutiltest {public
static void Main (string[] args) throws Exception {
User user = new user (); C6/>user.setid (2);
User.setname ("cat");
User.setpass ("intel123");
Hibernateutil.save (user);
list<user> list = Hibernateutil.listusers ();
for (User u:list) {
System.out.println (u.getname () + ":" + U.getpass ());}}}