Eclipse3.0 + myeclipse3.8.1ga + mysql development hibernate

xiaoxiao2021-03-06  69

After a few days of hard work and trouble, I finally developed the use of Hibernate for persistence layers in Eclipse! It can be easily developed in JBX, but in Eclipse is always a problem, it may be too stupid. Today, I finally got this basic problem. In order not to let my beginners walked, I also in order to fulfill me. "Eclipse3.0 myeclipse3.8.1ga tomcat5.0 mysql development JSP" article gives you a commitment to everyone, now the procedure of your steps and the questions you should pay attention to: (Note: My development environment "eclipse3. 0 myeclipse3.8.1ga tomcat5.0 mysql development jsp ") 1. Establish Java Project ---> HibTest; 2. Newly build two directory SRCs and ADOs for HibTest; 3. Right-click Hibtest to add Hibernate properties , The dialog box, establish the PersonSessionFactary to select the directory as / src, all the way to complete! Automatically generate pershibitionFactary.java and hibernate.cfg.xml; 4.Window-> show view-> other-> myeclipse_> dbbrowse, you will display DbBrowse, point New, Create New Profile dialog box, fill in your MySQL corresponding information, OK, then right-click the profile you create, select Open Connection The database and data table in MySQL will appear, then right-click Create Hibernate Mapping File, the dialog box, create the Person class, will automatically generate AbstractPerson .java, Person.java, Person.hbm.xml (these files are placed under / src); 5. Create Test Class Insert.java and QueryHib.java Insert.JavaPackage Ado in the / Dao directory;

Import net.sf.hibelnate.hibernateException; import net.sf.hibernate.Session; import src.person; import src.personsessionFactory;

/ ** * @author Yang Qiang * * / public class Insert {static Session s = null; public static void main (String [] args) throws Exception {try {s = PersonSessionFactory.currentSession (); Person yuj = new Person ( ); Yuj.setname ("sfdhh"); yuj.setaddress ("sfhhf"); Person x = new person (); x.setname ("sfDhhfd"); x.SetAddress ("fshdfhd"); // Persistence S.SAVE (YUJ); S.Save (x); s.flush (); System.out.Print ("Success");} catch (hibernateExcection E) {system.err.println ("Hibernate Exception" E .getMessage ()); throw new runtimeException (e);}}} queryhihib.javaPackage ado; import java.util.ITerator;

Import net.sf.hibelnate.hibernateException; import net.sf.hibernate.query; import net.sf.hibernate.Session;

import src.PersonSessionFactory; import src.Person;. / ** * @author Yang Qiang * * / public class Queryhib {public Iterator getPerson () {/ * * Use the ConnectionFactory to retrieve an open * Hibernate Session * * / Session session = NULL;

try {session = PersonSessionFactory.currentSession (); / * * Build HQL (Hibernate Query Language) query to retrieve a list * of all the items currently stored by Hibernate * / Query query = session.createQuery ( "select person from Person person. "); Return query.iterate ();

} Catch (HibernateException e) {System.err.println ( "Hibernate Exception" e.getMessage ()); throw new RuntimeException (e);} / * * Regardless of whether the above processing resulted in an Exception * or proceeded normally , we want to close the Hibernate session. When * closing the session, we must allow for the possibility of a Hibernate * Exception. * * /} public static void main (String [] args) throws Exception {try {Queryhib q = new QueryHib (); itrator it = q.getPerson (); whose (it.hasnext ()) {person temp = (person) it.next (); system.out.println (Temp.getID ()); system.out .println ()); system.out.println;}}}}}} catch (exception ex) {system.err.println ("Hibernate Exception" EX.getMessage ()); throw New runtimeException (ex);}}} 6. Run Insert.java inserts the database; 7. Run query data on database data;

Post: Is there a problem when it is run, that is because no log4j is configured, you can put the log4j.properties under Hibernate everything in the project! No map, please forgive me!

转载请注明原文地址:https://www.9cbs.com/read-90949.html

New Post(0)