Records on hibernate usage and how to simply answer CRUD operations

Source: Internet
Author: User
Tags openid

There are a lot of guidelines on how to use Hibernate now online, but here I just want to make a record of myself so that I can read it as a material.

① the process of importing jar packages using Hibernate is omitted here-----

② Configure an XML file under the src file, named Hibernate.cfg.xml, with the following configuration in its basic content:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hiberna Te.sourceforge.net/hibernate-configuration-3.0.dtd ">
③ with the above configuration, we can implement CRUD operations on the database tables in DAO, the simple code is as follows:

Query operation:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >configuration cfg = new annotationconfiguration (); Sessionfactory SF = Cfg.configure (). Buildsessionfactory (); Session session = Sf.opensession (); Session.begintransaction (); Query query = Session.createquery ("Select Useropenid from At_gzf where Useropenid=:openid"); Query.setstring ("OpenID", OpenID); String Useropenid = (string) query.uniqueresult (); The Uniqueresult () method can be used when returned as a unique character, and the entry class can be used to parse System.out.println (Useropenid) in the same way as the returned object; Session.gettransaction () . commit (); Session.close (); Sf.close ();</span>

In the above write the query operation of the code implementation, in fact, for the modification and deletion operation is the same as above, but only the HQL statement of the difference;


Data insert operation:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >configuration cfg = new annotationconfiguration (); Sessionfactory SF = Cfg.configure (). Buildsessionfactory (); Session Ssone = Sf.opensession (); ssone.begintransaction ()//OK, put the operation into the transaction md_content Content = new Md_content (); Content.setmdhead (Mdhead); content.setmdcontent (mdcontent); Content.setmdid (Mdid); Content.setmdremark (Mdremark); Content.setmdurl (Mdurl); Content.setmdsendtime (System.currenttimemillis () + ""); Ssone.save (content);// Save Object Ssone.gettransaction (). commit ();//Get Transaction and submit ssone.close ();//Session close Sf.close ();//Factory Close </span>
The data insert operation is actually the user data in the form of an object to be encapsulated to commit to save data;

Records on hibernate usage and how to simply answer CRUD operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.