Let's start with a structure like this.
Includes entity classes, implementation classes , Large configurations , small Configurations ( mapping Files ), and rack packages
Entity class We're not going to focus on this.
Let's start by importing the rack packages we need to get in.
we will first create a new folder named Lib in the src root directory.
Bring in the rack, and then put all the rack bags Bulid path below
The meaning of each rack package represents
Then create the Hibernate profile ( Note that it will be in the src root directory )
Create a mapping file ( under the same package as the entity class )
Code presentation
Entity class
Combined with the above run out correctly shown as:
Extended
Query data public static void Select () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg.xml") ; Build Session Factory Sessionfactory Factory=config.buildsessionfactory (); Session session=factory.opensession () was created through session factory. Create transaction Object Transaction tx=session.begintransaction (); Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (5)); System.out.println (News.getntitle ()); Tx.commit (); Session.close (); Modify the private static void modify () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg . xml "); Build Session Factory Sessionfactory Factory=config.buildsessionfactory (); Session session=factory.opensession () was created through session factory. Create transaction Object Transaction tx=session.begintransaction (); Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (6)); Newsdetail news=new newsdetail ("hibernate44", "Happy", "December 22, 2015", "Hibernate content Test", 4); Session.update (news); Tx.commit (); System.out.prIntln (News.getntitle ()); Session.close (); Delete the private static void Del () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg.xml"); Build Session Factory Sessionfactory Factory=config.buildsessionfactory (); Session session=factory.opensession () was created through session factory. Create transaction Object Transaction tx=session.begintransaction (); Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (5)); Session.delete (news); Tx.commit (); System.out.println (News.getntitle ()); Session.close (); }
The first example of hibernate