First Hibernate program

xiaoxiao2021-03-06  42

Use Eclipse

Establish an engineering, the source code directory is the SRC output directory to bin

Basic class fontclass has fields: long recordid, string fontname, timestamp addtime, and corresponding set, GET method

The corresponding database table is

Font

Create Table Font

Recordid Bigint (20) Not Null Auto_Increment,

FontName Varchar (50) Not Null Default 'Lishu',

Addtime TimeStamp,

PRIMARY Key (RecordID)

) TYPE = MyISAM;

Establish an HBM file corresponding to FontClass

Font.hbm.xml

"- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Create a Hibernate.cfg.xml in the SRC

PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

JDBC: MySQL: // localhost / db

com.mysql.jdbc.driver

xxx

xxx

Net.sf.hibernate.dialect.mysqldiaAlaforct

True

Write test case

SessionFactory Factory = New Configuration (). Configure (). BuildSessionFactory ();

Session s = factory.opensession ();

Transaction tx = s.begintransaction ();

MMStemplateFontInfoclass font = new mmstemplateFontInfoclass ();

Font.SetFontName ("Lishu");

S.SAVE (Font);

TX.comMit ();

S.flush (); Query can use list list = s.find ("from fontclass where fontname = 'creation"); here to pay attention to the character set of the database to use GBK or use query = S.CreateQuery ("from fontclass "); // This sentence means that the results of the query are listed from the first few rows to the data Q.SetFirstResult (0); // This sentence means how many data is taken, following SQL Server's TOP method and The same meaning of mysql's Limit method. Q.SETMAXRESULTS (3); // Get a scrollableresults, can be scrolled, if your database supports the free movement of the cursor, you can add, //, that is, if it can determine whether the query results are values, or move to the next line record, etc. Wait. // scrollableresults sc = q.scroll (); // Returns a list interface for traversing the result set list = q.list ();

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

New Post(0)