Hibernate (link database is much more convenient)!

Source: Internet
Author: User

First let's take a look at the configuration file, I first use struts to get controller, do not understand struts can go to Baidu a bit here will not explain:

After that we need to do a hibernate configuration file content as follows (here is the MySQL database link):

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/Library name </property>
<property name= "Connection.username" > Username </property>
<property name= "Connection.password" > Password </property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</property>
<property name= "Show_sql" >true</property>
<property name= "Hbm2ddl.auto" >update</property>
<mapping resource= "Mapping/jueaction.hbm.xml" ></mapping>
<mapping resource= "Mapping/quan.hbm.xml" ></mapping>
<mapping resource= "Mapping/user.hbm.xml" ></mapping>
</session-factory>
Wait until hibernate this configuration file is written well we also need to write each class's configuration file, the use is to organize the relationship between the classes:
<?xml version= ' 1.0 '?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Com.linyi.bean. (Your class name)" table= "(table name in your database)" >
Fields in the class that correspond to the table
<id name= "id" column= "id" >
Set this field as the primary key
<generator class= "native"/>
</id>
<property name= "name" column= "name"/>
The following is the table with the appearance of the relationship, such as one-to-many, many-to-a, many-to-many (here is not a detailed introduction is what, do not understand can go to ask degrees Niang)
I'm writing a lot-to-many relationship here.
<set name= "The name of the field used to find the relationship" table= "the table name of the intermediate relationship table" cascade= "Save-update" >
<key column= "field names used to maintain relationships in the relationship table" ></key>
<many-to-many column= The field name "Class=" Com.linyi.bean of another table relationship is maintained in the relational table. (table associated with this field) "Lazy=" false "></many-to-many>
</set>
</class>
These profiles are written according to your class, how much you need to write, when configured we can use Hibernate, and then let's look at how convenient query data is:
Here is just a test public class Test1 {
    Write the method you need to call
Public list< class name > Test1 () {
This section of the direct examination of the past is, although some methods are outdated, but still good use, do not ask why I use outdated methods, because I am a rookie, others will not.
Configuration con = new configuration (). Configure ();
Sessionfactory sessionfactory = Con.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Transaction Transaction = Session.begintransaction ();
Here we can use the HQL statement to query, it seems to be simple and exciting?
This is a conditional query, if you do not need the condition, where you do not need Oh!
String hql = "from Com.linyi.bean. (Note that you can use the class name here.) WHERE name =: Name";
Query query = session.createquery (HQL);
This is used to assign a value to a variable in the statement, you can write it yourself, or you can pass it from other classes.
Query.setparameter ("name", "Admin1");
We can get to the list collection of this class Oh!
list< class name > variable name = query.list ();
Transaction.commit ();
Session.close ();
return variable name;
}
}
The guide package needs to be noted here
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
These are the packages that Hibernate needs to use, and you can download them on your website.
Such a set down is not very simple, here only to show you the ordinary query, there are other deletions, modifications, additions and the like you can go
Hibernate official online to find Oh! Very simple, convenient a lot of wood there?

Hibernate (link database is much more convenient)!

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.