[Javaweb Basic] 005.Hibernate configuration and easy to use

Source: Internet
Author: User

1.Hibernate Introduction

Hibernate is a Java-based, open-source Persistence middleware that does a lightweight encapsulation of JDBC . The ORM Mapping mechanism is responsible for implementing the mapping between Java objects and relational databases , passing SQL statements to the database, and encapsulating the results returned by the database as objects . internally encapsulates JDBC Access to the database, providing an object-oriented database access API to upper-level applications. Manipulate data in the form of objects, improve development efficiency, and do not care about database types (changing the database as long as the value file is modified).

2. Create a DB broswer on the MyEclipse

This is something you can find in Windows---Show View->others. Then right-click to create a new connection object.

The test pass is ready.

3. Quickly create hibernate with MyEclipse

Select the item you want to add hibernate to, right-click->myeclipse->add Hibernate capatilities

Next

Next

Next

Finish by the finish, then see what files you have in your project

4. After the above configuration, we will start to configure the mapping of the database, with the DB browser is much easier

Select the DB browser that you just configured, and then select the table you want to generate

When the selection is complete, it appears

Then you can simply click Finish to finish, then we are done with hibernate configuration.

5. The configuration is complete, let's see how to use it.

Package Com.babybus.sdteam.dao;import Java.sql.sqlexception;import Java.util.arraylist;import java.util.List; Import Org.hibernate.query;import Org.hibernate.session;import Com.babybus.sdteam.hibernate.hibernatesessionfactory;import Com.babybus.sdteam.vo.student;public Class        Studenthibernatedao {Private session session = NULL; Public Studenthibernatedao () {//must not use getsession directly or it will report a repeat error session = Hibernatesessionfactory.getsessionfactory ()    . Opensession (); }/** * Insert data * @param student * @throws SQLException * */public void insertstudent (student s    Tudent) throws SQLException {//Open transaction session.begintransaction ();    Session.save (student);    Commit Transaction Session.gettransaction (). commit (); }/** * Delete data * @param student * @throws SQLException * * * public void deletestudent (int id) throw    s SQLException {//Open transaction session.begintransaction (); Student Student = (Student) session.get (Student.class, ID);    Session.delete (student);    Commit Transaction Session.gettransaction (). commit (); }/** * Modify data * @param student * @throws SQLException * * * public void updatestudent (student stude    NT) throws SQLException {//Open transaction session.begintransaction ();    Session.update (student);    Commit Transaction Session.gettransaction (). commit (); }/** * Query data * @param student * @throws SQLException * */public list<student> Querystudent     (Student Student) throws SQLException {list<student> resultlist = new arraylist<student> ();         HQL query, easy to understand String hql= "from Student as Student where 1=1 and";//use named parameters, recommended, easy to read. The condition is not NULL if (student! = NULL) {//Student name if (Student.getstudentname ()! = null &&!stu Dent.getstudentname (). Trim (). Equals ("")) {hql + = "Studentname = '" + student.getstudentname () + "and         ";    }//ID if (Student.getid ()! = 0)     {HQL + = "id =" + student.getid () + "and";         }//Age if (student.getage ()! = 0) {hql + = "ages =" + student.getage () + "and";         }//Grade if (student.getclassname () = null &&!student.getclassname (). Trim (). Equals (""))         {hql + = "classname = '" + student.getclassname () + "' and";     }} hql = hql.substring (0, Hql.length ()-5);     Query query=session.createquery (HQL);         Resultlist=query.list ();    return resultlist;    } public void CloseSession () {session.close (); }}

6. Problems encountered in the process of writing an example

①. The first thing to note is the *.hbm.xml DTD header .

<?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" >

If you can't find XML, try changing your head

②. If the error cannot find a class , it may conflict with the jar package underneath Tomcat, and can be found and deleted

③. If the report map file is duplicated , then the way to get the session may be problematic , refer to My code above comment

Above we learned the simple hibernate, the so-called Master with the door, practice in the individual, we want to further study, you can go online to see more in-depth tutorial

Conclusion

    • Benefits, mastering the primary application of Hibernate

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )

Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4606174.html

[Javaweb Basic] 005.Hibernate configuration and easy to use

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.