Eclipse SSH Three-frame environment construction < three >

Source: Internet
Author: User

  RELATED Links:

 Eclipse SSH Three-frame environment construction < one >
Eclipse SSH Three-frame environment construction < two >

  Introduction: through the last two articles we have been able to grasp the struts2 and spring environment of the building and simple use, but at the level of access to the database using the traditional JDBC. It is really not ideal to use JDBC in our project development, so today I'm going to show you the more popular persistence layer framework--hibernate in the industry.

  1th Step: We still need to introduce Hibernate's jar package

Find the Files directory hibernate extracted, in the directory under the Lib directory has a required directory, the directory of all the jar package into the Lib directory of our project.

  2nd Step: We have to deal with the configuration file, first we want to configure an entity mapping file in the SRC directory entityname.hbm.xml.

1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >56<className= "entity. Bookcard "table=" Bookcard ">7<id name= "CID" column= "CID" ></id>8<property name= "name" column= "name" type= "string" length= "not-"NULL= "true" ></property>9<property name= "Sex" column= "Sex" ></property>Ten<property name= "Carddate" column= "Carddate" ></property> One<property name= "deposit" column= "deposit" ></property> A</class> -

  parsing:1> header information is not much to say, first write a hibernate-mapping-end mark, in the tag map our corresponding entity class.

2> the value of the name attribute in the class tag corresponds to our entity class (full path), the table property corresponds to our name in the database

The 3>id tag is the primary key configuration information for our database, so the attribute value in the name attribute is the property of our entity class object, and column is the field of the database table.

The Type property in the 4>property tag corresponds to the Java data type of the property, and length is the equivalent of varchar in the database, and the Not-null property specifies whether the field can be empty, and the default can be empty.

  The 3rd step: still write the configuration file, configure the connection database related information hibernate.cfg.xml file

1<?xml version= "1.0" encoding= "Utf-8"?>2<! DOCTYPE hibernate-Configuration Public3"-//hibernate/hibernate Configuration DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >5 67<session-factory name= "foo" >8<!--Configure MySQL connection Parameters--9<property name= "dialect" >org.hibernate.dialect.MySQL5Dialect</property>Ten<property name= "Connection.pool_size" >5</property> One<property name= "Show_sql" >true</property> A<property name= "Format_sql" >true</property> -<property name= "Hbm2ddl.auto" >update</property> -           the<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property> -<property name= "Connection.url" >jdbc:mysql://localhost:3306/carddb</property> -<property name= "Connection.username" >root</property> -<property name= "Connection.password" >123456</property> +           -<mapping resource= "BookCard.hbm.xml"/> +</session-factory> A      at

  parsing:1> configures a hibernate-configuration-end tag, inserting session-factory-and-kinsoku tags inside it.

2> Specify the dialect attribute value in the property tag in the name attribute metabase dialect (different databases and different dialects of the version, see the relevant information)

3> Configure the connection pool size in the property tag by specifying the Connection.pool attribute value in the name attribute (depending on the frequency of access configuration)

4> Specify the Show_sql attribute value in the property tag in the Name property to configure whether the SQL statement is displayed in the console (recommended for true during development, easy to debug)

5> specifying the Format_sql attribute value in the property tag in the name attribute configures whether the SQL statement needs to be formatted (nonsense, of course)

    * 6> in the property tag, specify the Hbm2ddl.auto property value in the name attribute to configure the creation information.  When the value is update, if the database does not have a table corresponding to our entity will be created automatically for us, if present will update my table. When the value is create, each execution will first look for the existence of the database if the table exists that is first deleted when it is created and does not exist immediately.

7> Connection database driver and database URL, user name and password I believe everyone knows, I will not introduce each.

    * 8> Our document also needs to map a resource is our entity mapping file, specified in the mapping tag.

  4th Step: Configure the above files is our hibernate really serve us, write our DAO to operate the database.

1  Public classBookcarddaoimplImplementsBookcarddao {2     3 @Override4      PublicList<bookcard>Getallcardinfo () {5         //Sessionfactory This example can be created by the usual hibernate traditional notation (not recommended)6         //can be given to spring to host7Sessionfactory sessionfactory =NewConfiguration (). Configure (). Buildsessionfactory ();8Session session =sessionfactory.opensession ();9         //later, when using JPA, the Entitymanager is similar to the SessionTenQuery query = Session.createquery ("From Bookcard"); One          A         //These 2 lines of code will later be handed to spring's AOP to help handle -list<bookcard> list =query.getresultlist (); -          the session.close (); - sessionfactory.close (); -         returnlist; -     } +}
View Code

  parsing: Through the above code we can clearly see the usefulness of hibernate, our programmers no longer have to write to get the connection, get the SQL statement execution object and so on, and in the code we do not need to care about which database the project uses, This has greatly improved our efficiency.

 The data in the page has been successfully displayed

 The SQL statements for hibernate executed in the console are also displayed correctly.

Eclipse SSH Three-frame environment construction < three >

Related Article

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.